1 7 8 package java.lang; 9 10 import java.io.InputStream ; 11 import java.util.Enumeration ; 12 13 import java.util.StringTokenizer ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.FileNotFoundException ; 17 import java.io.IOException ; 18 import java.net.URL ; 19 import java.net.MalformedURLException ; 20 import java.security.AccessController ; 21 import java.security.PrivilegedAction ; 22 23 import java.util.jar.JarInputStream ; 24 import java.util.jar.Manifest ; 25 import java.util.jar.Attributes ; 26 import java.util.jar.Attributes.Name; 27 import java.util.jar.JarException ; 28 import java.util.Map ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 32 import sun.net.www.ParseUtil; 33 34 import java.lang.annotation.Annotation ; 35 36 89 public class Package implements java.lang.reflect.AnnotatedElement { 90 96 public String getName() { 97 return pkgName; 98 } 99 100 101 105 public String getSpecificationTitle() { 106 return specTitle; 107 } 108 109 118 public String getSpecificationVersion() { 119 return specVersion; 120 } 121 122 128 public String getSpecificationVendor() { 129 return specVendor; 130 } 131 132 136 public String getImplementationTitle() { 137 return implTitle; 138 } 139 140 149 public String getImplementationVersion() { 150 return implVersion; 151 } 152 153 158 public String getImplementationVendor() { 159 return implVendor; 160 } 161 162 167 public boolean isSealed() { 168 return sealBase != null; 169 } 170 171 178 public boolean isSealed(URL url) { 179 return url.equals(sealBase); 180 } 181 182 204 public boolean isCompatibleWith(String desired) 205 throws NumberFormatException 206 { 207 if (specVersion == null || specVersion.length() < 1) { 208 throw new NumberFormatException ("Empty version string"); 209 } 210 211 String [] sa = specVersion.split("\\.", -1); 212 int [] si = new int[sa.length]; 213 for (int i = 0; i < sa.length; i++) { 214 si[i] = Integer.parseInt(sa[i]); 215 if (si[i] < 0) 216 throw NumberFormatException.forInputString("" + si[i]); 217 } 218 219 String [] da = desired.split("\\.", -1); 220 int [] di = new int[da.length]; 221 for (int i = 0; i < da.length; i++) { 222 di[i] = Integer.parseInt(da[i]); 223 if (di[i] < 0) 224 throw NumberFormatException.forInputString("" + di[i]); 225 } 226 227 int len = Math.max(di.length, si.length); 228 for (int i = 0; i < len; i++) { 229 int d = (i < di.length ? di[i] : 0); 230 int s = (i < si.length ? si[i] : 0); 231 if (s < d) 232 return false; 233 if (s > d) 234 return true; 235 } 236 return true; 237 } 238 239 255 public static Package getPackage(String name) { 256 ClassLoader l = ClassLoader.getCallerClassLoader(); 257 if (l != null) { 258 return l.getPackage(name); 259 } else { 260 return getSystemPackage(name); 261 } 262 } 263 264 276 public static Package [] getPackages() { 277 ClassLoader l = ClassLoader.getCallerClassLoader(); 278 if (l != null) { 279 return l.getPackages(); 280 } else { 281 return getSystemPackages(); 282 } 283 } 284 285 302 static Package getPackage(Class c) { 303 String name = c.getName(); 304 int i = name.lastIndexOf('.'); 305 if (i != -1) { 306 name = name.substring(0, i); 307 ClassLoader cl = c.getClassLoader(); 308 if (cl != null) { 309 return cl.getPackage(name); 310 } else { 311 return getSystemPackage(name); 312 } 313 } else { 314 return null; 315 } 316 } 317 318 322 public int hashCode(){ 323 return pkgName.hashCode(); 324 } 325 326 333 public String toString() { 334 String spec = specTitle; 335 String ver = specVersion; 336 if (spec != null && spec.length() > 0) 337 spec = ", " + spec; 338 else 339 spec = ""; 340 if (ver != null && ver.length() > 0) 341 ver = ", version " + ver; 342 else 343 ver = ""; 344 return "package " + pkgName + spec + ver; 345 } 346 347 private Class <?> getPackageInfo() { 348 if (packageInfo == null) { 349 try { 350 packageInfo = Class.forName(pkgName + ".package-info", false, loader); 351 } catch (ClassNotFoundException ex) { 352 class PackageInfoProxy {} 354 packageInfo = PackageInfoProxy.class; 355 } 356 } 357 return packageInfo; 358 } 359 360 public <A extends Annotation > A getAnnotation(Class <A> annotationClass) { 361 return getPackageInfo().getAnnotation(annotationClass); 362 } 363 364 public boolean isAnnotationPresent( 365 Class <? extends Annotation > annotationClass) 366 { 367 return getPackageInfo().isAnnotationPresent(annotationClass); 368 } 369 370 public Annotation [] getAnnotations() { 371 return getPackageInfo().getAnnotations(); 372 } 373 374 public Annotation [] getDeclaredAnnotations() { 375 return getPackageInfo().getDeclaredAnnotations(); 376 } 377 378 390 Package(String name, 391 String spectitle, String specversion, String specvendor, 392 String impltitle, String implversion, String implvendor, 393 URL sealbase, ClassLoader loader) 394 { 395 pkgName = name; 396 implTitle = impltitle; 397 implVersion = implversion; 398 implVendor = implvendor; 399 specTitle = spectitle; 400 specVersion = specversion; 401 specVendor = specvendor; 402 sealBase = sealbase; 403 this.loader = loader; 404 } 405 406 413 private Package(String name, Manifest man, URL url, ClassLoader loader) { 414 String path = name.replace('.', '/').concat("/"); 415 String sealed = null; 416 String specTitle= null; 417 String specVersion= null; 418 String specVendor= null; 419 String implTitle= null; 420 String implVersion= null; 421 String implVendor= null; 422 URL sealBase= null; 423 Attributes attr = man.getAttributes(path); 424 if (attr != null) { 425 specTitle = attr.getValue(Name.SPECIFICATION_TITLE); 426 specVersion = attr.getValue(Name.SPECIFICATION_VERSION); 427 specVendor = attr.getValue(Name.SPECIFICATION_VENDOR); 428 implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE); 429 implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION); 430 implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR); 431 sealed = attr.getValue(Name.SEALED); 432 } 433 attr = man.getMainAttributes(); 434 if (attr != null) { 435 if (specTitle == null) { 436 specTitle = attr.getValue(Name.SPECIFICATION_TITLE); 437 } 438 if (specVersion == null) { 439 specVersion = attr.getValue(Name.SPECIFICATION_VERSION); 440 } 441 if (specVendor == null) { 442 specVendor = attr.getValue(Name.SPECIFICATION_VENDOR); 443 } 444 if (implTitle == null) { 445 implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE); 446 } 447 if (implVersion == null) { 448 implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION); 449 } 450 if (implVendor == null) { 451 implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR); 452 } 453 if (sealed == null) { 454 sealed = attr.getValue(Name.SEALED); 455 } 456 } 457 if ("true".equalsIgnoreCase(sealed)) { 458 sealBase = url; 459 } 460 pkgName = name; 461 this.specTitle = specTitle; 462 this.specVersion = specVersion; 463 this.specVendor = specVendor; 464 this.implTitle = implTitle; 465 this.implVersion = implVersion; 466 this.implVendor = implVendor; 467 this.sealBase = sealBase; 468 this.loader = loader; 469 } 470 471 474 static Package getSystemPackage(String name) { 475 synchronized (pkgs) { 476 Package pkg = (Package )pkgs.get(name); 477 if (pkg == null) { 478 name = name.replace('.', '/').concat("/"); 479 String fn = getSystemPackage0(name); 480 if (fn != null) { 481 pkg = defineSystemPackage(name, fn); 482 } 483 } 484 return pkg; 485 } 486 } 487 488 491 static Package [] getSystemPackages() { 492 String [] names = getSystemPackages0(); 494 synchronized (pkgs) { 495 for (int i = 0; i < names.length; i++) { 496 defineSystemPackage(names[i], getSystemPackage0(names[i])); 497 } 498 return (Package [])pkgs.values().toArray(new Package [pkgs.size()]); 499 } 500 } 501 502 private static Package defineSystemPackage(final String iname, 503 final String fn) 504 { 505 return (Package ) AccessController.doPrivileged(new PrivilegedAction () { 506 public Object run() { 507 String name = iname; 508 URL url = (URL )urls.get(fn); 510 if (url == null) { 511 File file = new File (fn); 513 try { 514 url = ParseUtil.fileToEncodedURL(file); 515 } catch (MalformedURLException e) { 516 } 517 if (url != null) { 518 urls.put(fn, url); 519 if (file.isFile()) { 521 mans.put(fn, loadManifest(fn)); 522 } 523 } 524 } 525 name = name.substring(0, name.length() - 1).replace('/', '.'); 527 Package pkg; 528 Manifest man = (Manifest )mans.get(fn); 529 if (man != null) { 530 pkg = new Package (name, man, url, null); 531 } else { 532 pkg = new Package (name, null, null, null, 533 null, null, null, null, null); 534 } 535 pkgs.put(name, pkg); 536 return pkg; 537 } 538 }); 539 } 540 541 544 private static Manifest loadManifest(String fn) { 545 try { 546 FileInputStream fis = new FileInputStream (fn); 547 JarInputStream jis = new JarInputStream (fis, false); 548 Manifest man = jis.getManifest(); 549 jis.close(); 550 return man; 551 } catch (IOException e) { 552 return null; 553 } 554 } 555 556 private static Map pkgs = new HashMap (31); 558 559 private static Map urls = new HashMap (10); 561 562 private static Map mans = new HashMap (10); 564 565 private static native String getSystemPackage0(String name); 566 private static native String [] getSystemPackages0(); 567 568 571 private final String pkgName; 572 private final String specTitle; 573 private final String specVersion; 574 private final String specVendor; 575 private final String implTitle; 576 private final String implVersion; 577 private final String implVendor; 578 private final URL sealBase; 579 private transient final ClassLoader loader; 580 private transient Class packageInfo; 581 } 582 | Popular Tags |