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