Save This Page
Home » openjdk-7 » sun » misc » [javadoc | source]
    1   /*
    2    * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Oracle designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Oracle in the LICENSE file that accompanied this code.
   10    *
   11    * This code is distributed in the hope that it will be useful, but WITHOUT
   12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14    * version 2 for more details (a copy is included in the LICENSE file that
   15    * accompanied this code).
   16    *
   17    * You should have received a copy of the GNU General Public License version
   18    * 2 along with this work; if not, write to the Free Software Foundation,
   19    * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20    *
   21    * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   22    * or visit www.oracle.com if you need additional information or have any
   23    * questions.
   24    */
   25   
   26   package sun.misc;
   27   
   28   import java.util.StringTokenizer;
   29   import java.util.jar.Attributes;
   30   import java.util.jar.Attributes.Name;
   31   import java.util.ResourceBundle;
   32   import java.util.MissingResourceException;
   33   import java.text.MessageFormat;
   34   import java.lang.Character;
   35   
   36   
   37   /**
   38    * This class holds all necessary information to install or
   39    * upgrade a extension on the user's disk
   40    *
   41    * @author  Jerome Dochez
   42    */
   43   public class ExtensionInfo {
   44   
   45       /**
   46        * <p>
   47        * public static values returned by the isCompatible method
   48        * </p>
   49        */
   50       public static final int COMPATIBLE = 0;
   51       public static final int REQUIRE_SPECIFICATION_UPGRADE = 1;
   52       public static final int REQUIRE_IMPLEMENTATION_UPGRADE = 2;
   53       public static final int REQUIRE_VENDOR_SWITCH = 3;
   54       public static final int INCOMPATIBLE = 4;
   55   
   56       /**
   57        * <p>
   58        * attributes fully describer an extension. The underlying described
   59        * extension may be installed and requested.
   60        * <p>
   61        */
   62       public String title;
   63       public String name;
   64       public String specVersion;
   65       public String specVendor;
   66       public String implementationVersion;
   67       public String vendor;
   68       public String vendorId;
   69       public String url;
   70   
   71       // For I18N support
   72       private static final ResourceBundle rb =
   73           ResourceBundle.getBundle("sun.misc.resources.Messages");
   74   
   75   
   76       /**
   77        * <p>
   78        * Create a new uninitialized extension information object
   79        * </p>
   80        */
   81       public ExtensionInfo() {
   82       }
   83   
   84       /**
   85        * <p>
   86        * Create and initialize an extension information object.
   87        * The initialization uses the attributes passed as being
   88        * the content of a manifest file to load the extension
   89        * information from.
   90        * Since manifest file may contain information on several
   91        * extension they may depend on, the extension key parameter
   92        * is prepanded to the attribute name to make the key used
   93        * to retrieve the attribute from the manifest file
   94        * <p>
   95        * @param extensionKey unique extension key in the manifest
   96        * @param attr Attributes of a manifest file
   97        */
   98       public ExtensionInfo(String extensionKey, Attributes attr)
   99           throws NullPointerException
  100       {
  101           String s;
  102           if (extensionKey!=null) {
  103               s = extensionKey + "-";
  104           } else {
  105               s ="";
  106           }
  107   
  108           String attrKey = s + Name.EXTENSION_NAME.toString();
  109           name = attr.getValue(attrKey);
  110           if (name != null)
  111               name = name.trim();
  112   
  113           attrKey = s + Name.SPECIFICATION_TITLE.toString();
  114           title = attr.getValue(attrKey);
  115           if (title != null)
  116               title = title.trim();
  117   
  118           attrKey = s + Name.SPECIFICATION_VERSION.toString();
  119           specVersion = attr.getValue(attrKey);
  120           if (specVersion != null)
  121               specVersion = specVersion.trim();
  122   
  123           attrKey = s + Name.SPECIFICATION_VENDOR.toString();
  124           specVendor = attr.getValue(attrKey);
  125           if (specVendor != null)
  126               specVendor = specVendor.trim();
  127   
  128           attrKey = s + Name.IMPLEMENTATION_VERSION.toString();
  129           implementationVersion = attr.getValue(attrKey);
  130           if (implementationVersion != null)
  131               implementationVersion = implementationVersion.trim();
  132   
  133           attrKey = s + Name.IMPLEMENTATION_VENDOR.toString();
  134           vendor = attr.getValue(attrKey);
  135           if (vendor != null)
  136               vendor = vendor.trim();
  137   
  138           attrKey = s + Name.IMPLEMENTATION_VENDOR_ID.toString();
  139           vendorId = attr.getValue(attrKey);
  140           if (vendorId != null)
  141               vendorId = vendorId.trim();
  142   
  143           attrKey =s + Name.IMPLEMENTATION_URL.toString();
  144           url = attr.getValue(attrKey);
  145           if (url != null)
  146               url = url.trim();
  147       }
  148   
  149       /**
  150        * <p>
  151        * @return true if the extension described by this extension information
  152        * is compatible with the extension described by the extension
  153        * information passed as a parameter
  154        * </p>
  155        *
  156        * @param the requested extension information to compare to
  157        */
  158       public int isCompatibleWith(ExtensionInfo ei) {
  159   
  160           if (name == null || ei.name == null)
  161               return INCOMPATIBLE;
  162           if (name.compareTo(ei.name)==0) {
  163               // is this true, if not spec version is specified, we consider
  164               // the value as being "any".
  165               if (specVersion == null || ei.specVersion == null)
  166                   return COMPATIBLE;
  167   
  168               int version = compareExtensionVersion(specVersion, ei.specVersion);
  169               if (version<0) {
  170                   // this extension specification is "older"
  171                   if (vendorId != null && ei.vendorId !=null) {
  172                       if (vendorId.compareTo(ei.vendorId)!=0) {
  173                           return REQUIRE_VENDOR_SWITCH;
  174                       }
  175                   }
  176                   return REQUIRE_SPECIFICATION_UPGRADE;
  177               } else {
  178                   // the extension spec is compatible, let's look at the
  179                   // implementation attributes
  180                   if (vendorId != null && ei.vendorId != null) {
  181                       // They care who provides the extension
  182                       if (vendorId.compareTo(ei.vendorId)!=0) {
  183                           // They want to use another vendor implementation
  184                           return REQUIRE_VENDOR_SWITCH;
  185                       } else {
  186                           // Vendor matches, let's see the implementation version
  187                           if (implementationVersion != null && ei.implementationVersion != null) {
  188                               // they care about the implementation version
  189                               version = compareExtensionVersion(implementationVersion, ei.implementationVersion);
  190                               if (version<0) {
  191                                   // This extension is an older implementation
  192                                   return REQUIRE_IMPLEMENTATION_UPGRADE;
  193                               }
  194                           }
  195                       }
  196                   }
  197                   // All othe cases, we consider the extensions to be compatible
  198                   return COMPATIBLE;
  199               }
  200           }
  201           return INCOMPATIBLE;
  202       }
  203   
  204       /**
  205        * <p>
  206        * helper method to print sensible information on the undelying described
  207        * extension
  208        * </p>
  209        */
  210       public String toString() {
  211           return "Extension : title(" + title + "), name(" + name + "), spec vendor(" +
  212               specVendor + "), spec version(" + specVersion + "), impl vendor(" +
  213               vendor + "), impl vendor id(" + vendorId + "), impl version(" +
  214               implementationVersion + "), impl url(" + url + ")";
  215       }
  216   
  217       /*
  218        * <p>
  219        * helper method to compare two versions.
  220        * version are in the x.y.z.t pattern.
  221        * </p>
  222        * @param source version to compare to
  223        * @param target version used to compare against
  224        * @return < 0 if source < version
  225        *         > 0 if source > version
  226        *         = 0 if source = version
  227        */
  228       private int compareExtensionVersion(String source, String target)
  229           throws NumberFormatException
  230       {
  231           source = source.toLowerCase();
  232           target = target.toLowerCase();
  233   
  234           return strictCompareExtensionVersion(source, target);
  235       }
  236   
  237   
  238       /*
  239        * <p>
  240        * helper method to compare two versions.
  241        * version are in the x.y.z.t pattern.
  242        * </p>
  243        * @param source version to compare to
  244        * @param target version used to compare against
  245        * @return < 0 if source < version
  246        *         > 0 if source > version
  247        *         = 0 if source = version
  248        */
  249       private int strictCompareExtensionVersion(String source, String target)
  250           throws NumberFormatException
  251       {
  252           if (source.equals(target))
  253               return 0;
  254   
  255           StringTokenizer stk = new StringTokenizer(source, ".,");
  256           StringTokenizer ttk = new StringTokenizer(target, ".,");
  257   
  258           // Compare number
  259           int n = 0, m = 0, result = 0;
  260   
  261           // Convert token into meaning number for comparision
  262           if (stk.hasMoreTokens())
  263               n = convertToken(stk.nextToken().toString());
  264   
  265           // Convert token into meaning number for comparision
  266           if (ttk.hasMoreTokens())
  267               m = convertToken(ttk.nextToken().toString());
  268   
  269           if (n > m)
  270               return 1;
  271           else if (m > n)
  272               return -1;
  273           else
  274           {
  275               // Look for index of "." in the string
  276               int sIdx = source.indexOf(".");
  277               int tIdx = target.indexOf(".");
  278   
  279               if (sIdx == -1)
  280                   sIdx = source.length() - 1;
  281   
  282               if (tIdx == -1)
  283                   tIdx = target.length() - 1;
  284   
  285               return strictCompareExtensionVersion(source.substring(sIdx + 1),
  286                                                    target.substring(tIdx + 1));
  287           }
  288       }
  289   
  290       private int convertToken(String token)
  291       {
  292           if (token == null || token.equals(""))
  293               return 0;
  294   
  295           int charValue = 0;
  296           int charVersion = 0;
  297           int patchVersion = 0;
  298           int strLength = token.length();
  299           int endIndex = strLength;
  300           char lastChar;
  301   
  302           Object[] args = {name};
  303           MessageFormat mf = new MessageFormat(rb.getString("optpkg.versionerror"));
  304           String versionError = mf.format(args);
  305   
  306           // Look for "-" for pre-release
  307           int prIndex = token.indexOf("-");
  308   
  309           // Look for "_" for patch release
  310           int patchIndex = token.indexOf("_");
  311   
  312           if (prIndex == -1 && patchIndex == -1)
  313           {
  314               // This is a FCS release
  315               try {
  316                   return Integer.parseInt(token) * 100;
  317               } catch (NumberFormatException e) {
  318                   System.out.println(versionError);
  319                   return 0;
  320               }
  321           }
  322           else if (patchIndex != -1)
  323           {
  324               // This is a patch (update) release
  325               int prversion;
  326               try {
  327                   // Obtain the version
  328                   prversion = Integer.parseInt(token.substring(0, patchIndex));
  329   
  330                   // Check to see if the patch version is in the n.n.n_nnl format (special release)
  331                   lastChar = token.charAt(strLength-1);
  332                   if (Character.isLetter(lastChar)) {
  333                       // letters a-z have values from 10-35
  334                       charValue = Character.getNumericValue(lastChar);
  335                       endIndex = strLength-1;
  336   
  337                       // Obtain the patch version id
  338                       patchVersion = Integer.parseInt(token.substring(patchIndex+1, endIndex));
  339   
  340                       if (charValue >= Character.getNumericValue('a') && charValue <= Character.getNumericValue('z')) {
  341                           // This is a special release
  342                           charVersion = (patchVersion * 100) + charValue;
  343                       } else {
  344                           // character is not a a-z letter, ignore
  345                           charVersion = 0;
  346                           System.out.println(versionError);
  347                       }
  348                   } else {
  349                       // This is a regular update release. Obtain the patch version id
  350                       patchVersion = Integer.parseInt(token.substring(patchIndex+1, endIndex));
  351                   }
  352               } catch (NumberFormatException e) {
  353                   System.out.println(versionError);
  354                   return 0;
  355               }
  356               return prversion * 100 + (patchVersion + charVersion);
  357           }
  358           else
  359           {
  360               //This is a milestone release, either a early access, alpha, beta, or RC
  361   
  362               // Obtain the version
  363               int mrversion;
  364               try {
  365                   mrversion = Integer.parseInt(token.substring(0, prIndex));
  366               } catch (NumberFormatException e) {
  367                   System.out.println(versionError);
  368                   return 0;
  369               }
  370   
  371               // Obtain the patch version string, including the milestone + version
  372               String prString = token.substring(prIndex + 1);
  373   
  374               // Milestone version
  375               String msVersion = "";
  376               int delta = 0;
  377   
  378               if (prString.indexOf("ea") != -1)
  379               {
  380                   msVersion = prString.substring(2);
  381                   delta = 50;
  382               }
  383               else if (prString.indexOf("alpha") != -1)
  384               {
  385                   msVersion = prString.substring(5);
  386                   delta = 40;
  387               }
  388               else if (prString.indexOf("beta") != -1)
  389               {
  390                   msVersion = prString.substring(4);
  391                   delta = 30;
  392               }
  393               else if (prString.indexOf("rc") != -1)
  394               {
  395                   msVersion = prString.substring(2);
  396                   delta = 20;
  397               }
  398   
  399               if (msVersion == null || msVersion.equals(""))
  400               {
  401                   // No version after the milestone, assume 0
  402                   return mrversion * 100 - delta ;
  403               }
  404               else
  405               {
  406                   // Convert the milestone version
  407                   try {
  408                       return mrversion * 100 - delta + Integer.parseInt(msVersion);
  409                   } catch (NumberFormatException e) {
  410                       System.out.println(versionError);
  411                       return 0;
  412                   }
  413               }
  414           }
  415       }
  416   }

Save This Page
Home » openjdk-7 » sun » misc » [javadoc | source]