| 1 | /* java.lang.Package - Everything you ever wanted to know about a package.
|
|---|
| 2 | Copyright (C) 2000, 2001 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 | package java.lang;
|
|---|
| 39 |
|
|---|
| 40 | import java.net.URL;
|
|---|
| 41 | import java.util.NoSuchElementException;
|
|---|
| 42 | import java.util.StringTokenizer;
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Everything you ever wanted to know about a package. This class makes it
|
|---|
| 46 | * possible to attach specification and implementation information to a
|
|---|
| 47 | * package as explained in the
|
|---|
| 48 | * <a href="http://java.sun.com/products/jdk/1.3/docs/guide/versioning/spec/VersioningSpecification.html#PackageVersionSpecification">Package Versioning Specification</a>
|
|---|
| 49 | * section of the
|
|---|
| 50 | * <a href="http://java.sun.com/products/jdk/1.3/docs/guide/versioning/spec/VersioningSpecification.html">Product Versioning Specification</a>.
|
|---|
| 51 | * It also allows packages to be sealed with respect to the originating URL.
|
|---|
| 52 | * <p>
|
|---|
| 53 | * The most useful method is the <code>isCompatibleWith()</code> method that
|
|---|
| 54 | * compares a desired version of a specification with the version of the
|
|---|
| 55 | * specification as implemented by a package. A package is considered
|
|---|
| 56 | * compatible with another version if the version of the specification is
|
|---|
| 57 | * equal or higher then the requested version. Version numbers are represented
|
|---|
| 58 | * as strings of positive numbers separated by dots (e.g. "1.2.0").
|
|---|
| 59 | * The first number is called the major number, the second the minor,
|
|---|
| 60 | * the third the micro, etc. A version is considered higher then another
|
|---|
| 61 | * version if it has a bigger major number then the another version or when
|
|---|
| 62 | * the major numbers of the versions are equal if it has a bigger minor number
|
|---|
| 63 | * then the other version, etc. (If a version has no minor, micro, etc numbers
|
|---|
| 64 | * then they are considered the be 0.)
|
|---|
| 65 | *
|
|---|
| 66 | * @since 1.2
|
|---|
| 67 | * @author Mark Wielaard ([email protected])
|
|---|
| 68 | */
|
|---|
| 69 | public class Package
|
|---|
| 70 | {
|
|---|
| 71 | /** The name of the Package */
|
|---|
| 72 | final private String name;
|
|---|
| 73 |
|
|---|
| 74 | /** The name if the implementation */
|
|---|
| 75 | final private String implTitle;
|
|---|
| 76 | /** The vendor that wrote this implementation */
|
|---|
| 77 | final private String implVendor;
|
|---|
| 78 | /** The version of this implementation */
|
|---|
| 79 | final private String implVersion;
|
|---|
| 80 |
|
|---|
| 81 | /** The name of the specification */
|
|---|
| 82 | final private String specTitle;
|
|---|
| 83 | /** The name of the specification designer */
|
|---|
| 84 | final private String specVendor;
|
|---|
| 85 | /** The version of this specification */
|
|---|
| 86 | final private String specVersion;
|
|---|
| 87 |
|
|---|
| 88 | /** If sealed the origin of the package classes, otherwise null */
|
|---|
| 89 | final private URL sealed;
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * A package local constructor for the Package class.
|
|---|
| 93 | * All parameters except the <code>name</code> of the package may be
|
|---|
| 94 | * <code>null</code>.
|
|---|
| 95 | * There are no public constructors defined for Package this is a package
|
|---|
| 96 | * local constructor that is used by java.lang.Classloader.definePackage().
|
|---|
| 97 | *
|
|---|
| 98 | * @param name The name of the Package
|
|---|
| 99 | * @param specTitle The name of the specification
|
|---|
| 100 | * @param specVendor The name of the specification designer
|
|---|
| 101 | * @param specVersion The version of this specification
|
|---|
| 102 | * @param implTitle The name of the implementation
|
|---|
| 103 | * @param implVendor The vendor that wrote this implementation
|
|---|
| 104 | * @param implVersion The version of this implementation
|
|---|
| 105 | * @param sealed If sealed the origin of the package classes
|
|---|
| 106 | */
|
|---|
| 107 | Package(String name,
|
|---|
| 108 | String specTitle, String specVendor, String specVersion,
|
|---|
| 109 | String implTitle, String implVendor, String implVersion, URL sealed)
|
|---|
| 110 | {
|
|---|
| 111 | if (name == null)
|
|---|
| 112 | throw new IllegalArgumentException("null Package name");
|
|---|
| 113 |
|
|---|
| 114 | this.name = name;
|
|---|
| 115 |
|
|---|
| 116 | this.implTitle = implTitle;
|
|---|
| 117 | this.implVendor = implVendor;
|
|---|
| 118 | this.implVersion = implVersion;
|
|---|
| 119 |
|
|---|
| 120 | this.specTitle = specTitle;
|
|---|
| 121 | this.specVendor = specVendor;
|
|---|
| 122 | this.specVersion = specVersion;
|
|---|
| 123 |
|
|---|
| 124 | this.sealed = sealed;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /**
|
|---|
| 128 | * Returns the Package name.
|
|---|
| 129 | */
|
|---|
| 130 | public String getName()
|
|---|
| 131 | {
|
|---|
| 132 | return name;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /**
|
|---|
| 136 | * Returns the name of the implementation or null if unknown.
|
|---|
| 137 | */
|
|---|
| 138 | public String getImplementationTitle()
|
|---|
| 139 | {
|
|---|
| 140 | return implTitle;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /**
|
|---|
| 144 | * Returns the vendor that wrote this implementation or null if unknown.
|
|---|
| 145 | */
|
|---|
| 146 | public String getImplementationVendor()
|
|---|
| 147 | {
|
|---|
| 148 | return implVendor;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /**
|
|---|
| 152 | * Returns the version of this implementation or null if unknown.
|
|---|
| 153 | */
|
|---|
| 154 | public String getImplementationVersion()
|
|---|
| 155 | {
|
|---|
| 156 | return implVersion;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * Returns the name of the specification or null if unknown.
|
|---|
| 161 | */
|
|---|
| 162 | public String getSpecificationTitle()
|
|---|
| 163 | {
|
|---|
| 164 | return specTitle;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /**
|
|---|
| 168 | * Returns the name of the specification designer or null if unknown.
|
|---|
| 169 | */
|
|---|
| 170 | public String getSpecificationVendor()
|
|---|
| 171 | {
|
|---|
| 172 | return specVendor;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /**
|
|---|
| 176 | * Returns the version of the specification or null if unknown.
|
|---|
| 177 | */
|
|---|
| 178 | public String getSpecificationVersion()
|
|---|
| 179 | {
|
|---|
| 180 | return specVersion;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /**
|
|---|
| 184 | * Returns true if this Package is sealed.
|
|---|
| 185 | */
|
|---|
| 186 | public boolean isSealed()
|
|---|
| 187 | {
|
|---|
| 188 | return (sealed != null);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /**
|
|---|
| 192 | * Returns true if this Package is sealed and the origin of the classes is
|
|---|
| 193 | * the given URL.
|
|---|
| 194 | *
|
|---|
| 195 | * @param url
|
|---|
| 196 | */
|
|---|
| 197 | public boolean isSealed(URL url)
|
|---|
| 198 | {
|
|---|
| 199 | return url.equals(sealed);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | /**
|
|---|
| 203 | * Checks if the version of the specification is higher or at least as high
|
|---|
| 204 | * as the desired version.
|
|---|
| 205 | * @param version the (minimal) desired version of the specification
|
|---|
| 206 | * @exception NumberFormatException when either version or the
|
|---|
| 207 | * specification version is not a correctly formatted version number
|
|---|
| 208 | * @exception NullPointerException if the supplied version or the
|
|---|
| 209 | * Package specification version is null.
|
|---|
| 210 | */
|
|---|
| 211 | public boolean isCompatibleWith(String version) throws NumberFormatException
|
|---|
| 212 | {
|
|---|
| 213 | StringTokenizer versionTokens = new StringTokenizer(version, ".");
|
|---|
| 214 | StringTokenizer specTokens = new StringTokenizer(specVersion, ".");
|
|---|
| 215 | try
|
|---|
| 216 | {
|
|---|
| 217 | while (versionTokens.hasMoreElements())
|
|---|
| 218 | {
|
|---|
| 219 | int vers = Integer.parseInt(versionTokens.nextToken());
|
|---|
| 220 | int spec = Integer.parseInt(specTokens.nextToken());
|
|---|
| 221 | if (spec < vers)
|
|---|
| 222 | return false;
|
|---|
| 223 | else if (spec > vers)
|
|---|
| 224 | return true;
|
|---|
| 225 | // They must be equal, next Token please!
|
|---|
| 226 | }
|
|---|
| 227 | }
|
|---|
| 228 | catch (NoSuchElementException e)
|
|---|
| 229 | {
|
|---|
| 230 | // this must have been thrown by spec.netToken() so return false
|
|---|
| 231 | return false;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | // They must have been exactly the same version.
|
|---|
| 235 | // Or the specVersion has more subversions. That is also good.
|
|---|
| 236 | return true;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | /**
|
|---|
| 240 | * Returns the named package if it is known by the callers class loader.
|
|---|
| 241 | * It may return null if the package is unknown, when there is no
|
|---|
| 242 | * information on that particular package available or when the callers
|
|---|
| 243 | * classloader is null.
|
|---|
| 244 | * @param name the name of the desired package
|
|---|
| 245 | */
|
|---|
| 246 | public static Package getPackage(String name)
|
|---|
| 247 | {
|
|---|
| 248 | // Get the caller's classloader
|
|---|
| 249 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 250 | Class c = sm.getClassContext()[1];
|
|---|
| 251 | ClassLoader cl = c.getClassLoader();
|
|---|
| 252 |
|
|---|
| 253 | if (cl != null)
|
|---|
| 254 | return cl.getPackage(name);
|
|---|
| 255 | else
|
|---|
| 256 | return null;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | /**
|
|---|
| 260 | * Returns all the packages that are known to the callers class loader.
|
|---|
| 261 | * It may return an empty array if the classloader of the caller is null.
|
|---|
| 262 | */
|
|---|
| 263 | public static Package[] getPackages()
|
|---|
| 264 | {
|
|---|
| 265 | // Get the caller's classloader
|
|---|
| 266 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 267 | Class c = sm.getClassContext()[1];
|
|---|
| 268 | ClassLoader cl = c.getClassLoader();
|
|---|
| 269 |
|
|---|
| 270 | if (cl != null)
|
|---|
| 271 | return cl.getPackages();
|
|---|
| 272 | else
|
|---|
| 273 | return new Package[0];
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | /**
|
|---|
| 277 | * Returns the hashCode of the name of this package.
|
|---|
| 278 | */
|
|---|
| 279 | public int hashCode()
|
|---|
| 280 | {
|
|---|
| 281 | return name.hashCode();
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | /**
|
|---|
| 285 | * Returns a string representation of this package name, specification,
|
|---|
| 286 | * implementation and class origin if sealed.
|
|---|
| 287 | */
|
|---|
| 288 | public String toString()
|
|---|
| 289 | {
|
|---|
| 290 | return "package: " + name +
|
|---|
| 291 | " spec: " + specTitle +
|
|---|
| 292 | " version: " + specVersion +
|
|---|
| 293 | " vendor: " + specVendor +
|
|---|
| 294 | " implementation: " + implTitle +
|
|---|
| 295 | " version: " + implVersion +
|
|---|
| 296 | " vendor: " + implVendor + " sealed: " + sealed;
|
|---|
| 297 | }
|
|---|
| 298 | }
|
|---|