1 7 package java.lang; 8 9 import java.io.InputStream ; 10 import java.io.IOException ; 11 import java.io.File ; 12 import java.lang.reflect.Constructor ; 13 import java.lang.reflect.InvocationTargetException ; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.security.AccessController ; 17 import java.security.AccessControlContext ; 18 import java.security.CodeSource ; 19 import java.security.Policy ; 20 import java.security.PrivilegedAction ; 21 import java.security.PrivilegedActionException ; 22 import java.security.PrivilegedExceptionAction ; 23 import java.security.ProtectionDomain ; 24 import java.util.Enumeration ; 25 import java.util.Hashtable ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Set ; 29 import java.util.Stack ; 30 import java.util.Map ; 31 import java.util.Vector ; 32 import sun.misc.ClassFileTransformer; 33 import sun.misc.CompoundEnumeration; 34 import sun.misc.Resource; 35 import sun.misc.URLClassPath; 36 import sun.misc.VM; 37 import sun.reflect.Reflection; 38 import sun.security.util.SecurityConstants; 39 40 142 public abstract class ClassLoader { 143 144 private static native void registerNatives(); 145 static { 146 registerNatives(); 147 } 148 149 private boolean initialized = false; 153 154 private ClassLoader parent; 156 157 private Hashtable package2certs = new Hashtable (11); 159 160 java.security.cert.Certificate [] nocerts; 162 163 private Vector classes = new Vector (); 166 167 private Set domains = new HashSet (); 169 170 void addClass(Class c) { 172 classes.addElement(c); 173 } 174 175 private HashMap packages = new HashMap (); 178 179 198 protected ClassLoader(ClassLoader parent) { 199 SecurityManager security = System.getSecurityManager(); 200 if (security != null) { 201 security.checkCreateClassLoader(); 202 } 203 this.parent = parent; 204 initialized = true; 205 } 206 207 222 protected ClassLoader() { 223 SecurityManager security = System.getSecurityManager(); 224 if (security != null) { 225 security.checkCreateClassLoader(); 226 } 227 this.parent = getSystemClassLoader(); 228 initialized = true; 229 } 230 231 232 234 250 public Class <?> loadClass(String name) throws ClassNotFoundException { 251 return loadClass(name, false); 252 } 253 254 291 protected synchronized Class <?> loadClass(String name, boolean resolve) 292 throws ClassNotFoundException 293 { 294 Class c = findLoadedClass(name); 296 if (c == null) { 297 try { 298 if (parent != null) { 299 c = parent.loadClass(name, false); 300 } else { 301 c = findBootstrapClass0(name); 302 } 303 } catch (ClassNotFoundException e) { 304 c = findClass(name); 307 } 308 } 309 if (resolve) { 310 resolveClass(c); 311 } 312 return c; 313 } 314 315 private synchronized Class loadClassInternal(String name) 317 throws ClassNotFoundException 318 { 319 return loadClass(name); 320 } 321 322 private void checkPackageAccess(Class cls, ProtectionDomain pd) { 323 final SecurityManager sm = System.getSecurityManager(); 324 if (sm != null) { 325 final String name = cls.getName(); 326 final int i = name.lastIndexOf('.'); 327 if (i != -1) { 328 AccessController.doPrivileged(new PrivilegedAction () { 329 public Object run() { 330 sm.checkPackageAccess(name.substring(0, i)); 331 return null; 332 } 333 }, new AccessControlContext (new ProtectionDomain [] {pd})); 334 } 335 } 336 domains.add(pd); 337 } 338 339 357 protected Class <?> findClass(String name) throws ClassNotFoundException { 358 throw new ClassNotFoundException (name); 359 } 360 361 396 @Deprecated 397 protected final Class <?> defineClass(byte[] b, int off, int len) 398 throws ClassFormatError 399 { 400 return defineClass(null, b, off, len, null); 401 } 402 403 462 protected final Class <?> defineClass(String name, byte[] b, int off, int len) 463 throws ClassFormatError 464 { 465 return defineClass(name, b, off, len, null); 466 } 467 468 472 private ProtectionDomain preDefineClass(String name, 473 ProtectionDomain protectionDomain) 474 { 475 if (!checkName(name)) 476 throw new NoClassDefFoundError ("IllegalName: " + name); 477 478 if ((name != null) && name.startsWith("java.")) { 479 throw new SecurityException ("Prohibited package name: " + 480 name.substring(0, name.lastIndexOf('.'))); 481 } 482 if (protectionDomain == null) { 483 protectionDomain = getDefaultDomain(); 484 } 485 486 if (name != null) 487 checkCerts(name, protectionDomain.getCodeSource()); 488 489 return protectionDomain; 490 } 491 492 private String defineClassSourceLocation(ProtectionDomain protectionDomain) 493 { 494 CodeSource cs = protectionDomain.getCodeSource(); 495 String source = null; 496 if (cs != null && cs.getLocation() != null) { 497 source = cs.getLocation().toString(); 498 } 499 return source; 500 } 501 502 private Class defineTransformedClass(String name, byte[] b, int off, int len, 503 ProtectionDomain protectionDomain, 504 ClassFormatError cfe, String source) 505 throws ClassFormatError 506 { 507 Object [] transformers = ClassFileTransformer.getTransformers(); 511 Class c = null; 512 513 for (int i = 0; transformers != null && i < transformers.length; i++) { 514 try { 515 byte[] tb = ((ClassFileTransformer) transformers[i]).transform(b, off, len); 517 c = defineClass1(name, tb, 0, tb.length, protectionDomain, source); 518 break; 519 } catch (ClassFormatError cfe2) { 520 } 522 } 523 524 if (c == null) 528 throw cfe; 529 530 return c; 531 } 532 533 private void postDefineClass(Class c, ProtectionDomain protectionDomain) 534 { 535 if (protectionDomain.getCodeSource() != null) { 536 java.security.cert.Certificate certs[] = 537 protectionDomain.getCodeSource().getCertificates(); 538 if (certs != null) 539 setSigners(c, certs); 540 } 541 } 542 543 609 protected final Class <?> defineClass(String name, byte[] b, int off, int len, 610 ProtectionDomain protectionDomain) 611 throws ClassFormatError 612 { 613 check(); 614 protectionDomain = preDefineClass(name, protectionDomain); 615 616 Class c = null; 617 String source = defineClassSourceLocation(protectionDomain); 618 619 try { 620 c = defineClass1(name, b, off, len, protectionDomain, source); 621 } catch (ClassFormatError cfe) { 622 c = defineTransformedClass(name, b, off, len, protectionDomain, cfe, source); 623 } 624 625 postDefineClass(c, protectionDomain); 626 return c; 627 } 628 629 691 protected final Class <?> defineClass(String name, java.nio.ByteBuffer b, 692 ProtectionDomain protectionDomain) 693 throws ClassFormatError 694 { 695 check(); 696 697 int len = b.remaining(); 698 699 if (!b.isDirect()) { 701 if (b.hasArray()) { 702 return defineClass(name, b.array(), 703 b.position() + b.arrayOffset(), len, 704 protectionDomain); 705 } else { 706 byte[] tb = new byte[len]; 708 b.get(tb); return defineClass(name, tb, 0, len, protectionDomain); 710 } 711 } 712 713 protectionDomain = preDefineClass(name, protectionDomain); 714 715 Class c = null; 716 String source = defineClassSourceLocation(protectionDomain); 717 718 try { 719 c = defineClass2(name, b, b.position(), len, protectionDomain, source); 720 } catch (ClassFormatError cfe) { 721 byte[] tb = new byte[len]; 722 b.get(tb); c = defineTransformedClass(name, tb, 0, len, protectionDomain, cfe, source); 724 } 725 726 postDefineClass(c, protectionDomain); 727 return c; 728 } 729 730 private native Class defineClass0(String name, byte[] b, int off, int len, 731 ProtectionDomain pd); 732 733 private native Class defineClass1(String name, byte[] b, int off, int len, 734 ProtectionDomain pd, String source); 735 736 private native Class defineClass2(String name, java.nio.ByteBuffer b, 737 int off, int len, ProtectionDomain pd, 738 String source); 739 740 private boolean checkName(String name) { 742 if ((name == null) || (name.length() == 0)) 743 return true; 744 if ((name.indexOf('/') != -1) 745 || (!VM.allowArraySyntax() && (name.charAt(0) == '['))) 746 return false; 747 return true; 748 } 749 750 private synchronized void checkCerts(String name, CodeSource cs) { 751 int i = name.lastIndexOf('.'); 752 String pname = (i == -1) ? "" : name.substring(0, i); 753 java.security.cert.Certificate [] pcerts = 754 (java.security.cert.Certificate []) package2certs.get(pname); 755 if (pcerts == null) { 756 if (cs != null) { 760 pcerts = cs.getCertificates(); 761 } 762 if (pcerts == null) { 763 if (nocerts == null) 764 nocerts = new java.security.cert.Certificate [0]; 765 pcerts = nocerts; 766 } 767 package2certs.put(pname, pcerts); 768 } else { 769 java.security.cert.Certificate [] certs = null; 770 if (cs != null) { 771 certs = cs.getCertificates(); 772 } 773 774 if (!compareCerts(pcerts, certs)) { 775 throw new SecurityException ("class \""+ name + 776 "\"'s signer information does not match signer information of other classes in the same package"); 777 } 778 } 779 } 780 781 785 private boolean compareCerts(java.security.cert.Certificate [] pcerts, 786 java.security.cert.Certificate [] certs) 787 { 788 if ((certs == null) || (certs.length == 0)) { 790 return pcerts.length == 0; 791 } 792 793 if (certs.length != pcerts.length) 795 return false; 796 797 boolean match; 800 for (int i = 0; i < certs.length; i++) { 801 match = false; 802 for (int j = 0; j < pcerts.length; j++) { 803 if (certs[i].equals(pcerts[j])) { 804 match = true; 805 break; 806 } 807 } 808 if (!match) return false; 809 } 810 811 for (int i = 0; i < pcerts.length; i++) { 813 match = false; 814 for (int j = 0; j < certs.length; j++) { 815 if (pcerts[i].equals(certs[j])) { 816 match = true; 817 break; 818 } 819 } 820 if (!match) return false; 821 } 822 823 return true; 824 } 825 826 843 protected final void resolveClass(Class <?> c) { 844 check(); 845 resolveClass0(c); 846 } 847 848 private native void resolveClass0(Class c); 849 850 872 protected final Class <?> findSystemClass(String name) 873 throws ClassNotFoundException 874 { 875 check(); 876 ClassLoader system = getSystemClassLoader(); 877 if (system == null) { 878 if (!checkName(name)) 879 throw new ClassNotFoundException (name); 880 return findBootstrapClass(name); 881 } 882 return system.loadClass(name); 883 } 884 885 private Class findBootstrapClass0(String name) 886 throws ClassNotFoundException 887 { 888 check(); 889 if (!checkName(name)) 890 throw new ClassNotFoundException (name); 891 return findBootstrapClass(name); 892 } 893 894 private native Class findBootstrapClass(String name) 895 throws ClassNotFoundException ; 896 897 private void check() { 899 if (!initialized) { 900 throw new SecurityException ("ClassLoader object not initialized"); 901 } 902 } 903 904 918 protected final Class <?> findLoadedClass(String name) { 919 check(); 920 if (!checkName(name)) 921 return null; 922 return findLoadedClass0(name); 923 } 924 925 private native final Class findLoadedClass0(String name); 926 927 939 protected final void setSigners(Class <?> c, Object [] signers) { 940 check(); 941 c.setSigners(signers); 942 } 943 944 945 947 969 public URL getResource(String name) { 970 URL url; 971 if (parent != null) { 972 url = parent.getResource(name); 973 } else { 974 url = getBootstrapResource(name); 975 } 976 if (url == null) { 977 url = findResource(name); 978 } 979 return url; 980 } 981 982 1008 public Enumeration <URL > getResources(String name) throws IOException { 1009 Enumeration [] tmp = new Enumeration [2]; 1010 if (parent != null) { 1011 tmp[0] = parent.getResources(name); 1012 } else { 1013 tmp[0] = getBootstrapResources(name); 1014 } 1015 tmp[1] = findResources(name); 1016 1017 return new CompoundEnumeration(tmp); 1018 } 1019 1020 1032 protected URL findResource(String name) { 1033 return null; 1034 } 1035 1036 1053 protected Enumeration <URL > findResources(String name) throws IOException { 1054 return new CompoundEnumeration(new Enumeration [0]); 1055 } 1056 1057 1070 public static URL getSystemResource(String name) { 1071 ClassLoader system = getSystemClassLoader(); 1072 if (system == null) { 1073 return getBootstrapResource(name); 1074 } 1075 return system.getResource(name); 1076 } 1077 1078 1098 public static Enumeration <URL > getSystemResources(String name) 1099 throws IOException 1100 { 1101 ClassLoader system = getSystemClassLoader(); 1102 if (system == null) { 1103 return getBootstrapResources(name); 1104 } 1105 return system.getResources(name); 1106 } 1107 1108 1111 private static URL getBootstrapResource(String name) { 1112 URLClassPath ucp = getBootstrapClassPath(); 1113 Resource res = ucp.getResource(name); 1114 return res != null ? res.getURL() : null; 1115 } 1116 1117 1120 private static Enumeration getBootstrapResources(String name) 1121 throws IOException 1122 { 1123 final Enumeration e = getBootstrapClassPath().getResources(name); 1124 return new Enumeration () { 1125 public Object nextElement() { 1126 return ((Resource)e.nextElement()).getURL(); 1127 } 1128 public boolean hasMoreElements() { 1129 return e.hasMoreElements(); 1130 } 1131 }; 1132 } 1133 1134 static URLClassPath getBootstrapClassPath() { 1136 if (bootstrapClassPath == null) { 1137 bootstrapClassPath = sun.misc.Launcher.getBootstrapClassPath(); 1138 } 1139 return bootstrapClassPath; 1140 } 1141 1142 private static URLClassPath bootstrapClassPath; 1143 1144 1158 public InputStream getResourceAsStream(String name) { 1159 URL url = getResource(name); 1160 try { 1161 return url != null ? url.openStream() : null; 1162 } catch (IOException e) { 1163 return null; 1164 } 1165 } 1166 1167 1180 public static InputStream getSystemResourceAsStream(String name) { 1181 URL url = getSystemResource(name); 1182 try { 1183 return url != null ? url.openStream() : null; 1184 } catch (IOException e) { 1185 return null; 1186 } 1187 } 1188 1189 1190 1192 1217 public final ClassLoader getParent() { 1218 if (parent == null) 1219 return null; 1220 SecurityManager sm = System.getSecurityManager(); 1221 if (sm != null) { 1222 ClassLoader ccl = getCallerClassLoader(); 1223 if (ccl != null && !isAncestor(ccl)) { 1224 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); 1225 } 1226 } 1227 return parent; 1228 } 1229 1230 1285 public static ClassLoader getSystemClassLoader() { 1286 initSystemClassLoader(); 1287 if (scl == null) { 1288 return null; 1289 } 1290 SecurityManager sm = System.getSecurityManager(); 1291 if (sm != null) { 1292 ClassLoader ccl = getCallerClassLoader(); 1293 if (ccl != null && ccl != scl && !scl.isAncestor(ccl)) { 1294 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); 1295 } 1296 } 1297 return scl; 1298 } 1299 1300 private static synchronized void initSystemClassLoader() { 1301 if (!sclSet) { 1302 if (scl != null) 1303 throw new IllegalStateException ("recursive invocation"); 1304 sun.misc.Launcher l = sun.misc.Launcher.getLauncher(); 1305 if (l != null) { 1306 Throwable oops = null; 1307 scl = l.getClassLoader(); 1308 try { 1309 PrivilegedExceptionAction a; 1310 a = new SystemClassLoaderAction(scl); 1311 scl = (ClassLoader ) AccessController.doPrivileged(a); 1312 } catch (PrivilegedActionException pae) { 1313 oops = pae.getCause(); 1314 if (oops instanceof InvocationTargetException ) { 1315 oops = oops.getCause(); 1316 } 1317 } 1318 if (oops != null) { 1319 if (oops instanceof Error ) { 1320 throw (Error ) oops; 1321 } else { 1322 throw new Error (oops); 1324 } 1325 } 1326 } 1327 sclSet = true; 1328 } 1329 } 1330 1331 boolean isAncestor(ClassLoader cl) { 1334 ClassLoader acl = this; 1335 do { 1336 acl = acl.parent; 1337 if (cl == acl) { 1338 return true; 1339 } 1340 } while (acl != null); 1341 return false; 1342 } 1343 1344 static ClassLoader getCallerClassLoader() { 1349 Class caller = Reflection.getCallerClass(3); 1351 if (caller == null) { 1353 return null; 1354 } 1355 return caller.getClassLoader0(); 1357 } 1358 1359 private static ClassLoader scl; 1361 1362 private static boolean sclSet; 1364 1365 1366 1368 1409 protected Package definePackage(String name, String specTitle, 1410 String specVersion, String specVendor, 1411 String implTitle, String implVersion, 1412 String implVendor, URL sealBase) 1413 throws IllegalArgumentException 1414 { 1415 synchronized (packages) { 1416 Package pkg = getPackage(name); 1417 if (pkg != null) { 1418 throw new IllegalArgumentException (name); 1419 } 1420 pkg = new Package (name, specTitle, specVersion, specVendor, 1421 implTitle, implVersion, implVendor, 1422 sealBase, this); 1423 packages.put(name, pkg); 1424 return pkg; 1425 } 1426 } 1427 1428 1440 protected Package getPackage(String name) { 1441 synchronized (packages) { 1442 Package pkg = (Package )packages.get(name); 1443 if (pkg == null) { 1444 if (parent != null) { 1445 pkg = parent.getPackage(name); 1446 } else { 1447 pkg = Package.getSystemPackage(name); 1448 } 1449 if (pkg != null) { 1450 packages.put(name, pkg); 1451 } 1452 } 1453 return pkg; 1454 } 1455 } 1456 1457 1466 protected Package [] getPackages() { 1467 Map map; 1468 synchronized (packages) { 1469 map = (Map )packages.clone(); 1470 } 1471 Package [] pkgs; 1472 if (parent != null) { 1473 pkgs = parent.getPackages(); 1474 } else { 1475 pkgs = Package.getSystemPackages(); 1476 } 1477 if (pkgs != null) { 1478 for (int i = 0; i < pkgs.length; i++) { 1479 String pkgName = pkgs[i].getName(); 1480 if (map.get(pkgName) == null) { 1481 map.put(pkgName, pkgs[i]); 1482 } 1483 } 1484 } 1485 return (Package [])map.values().toArray(new Package [map.size()]); 1486 } 1487 1488 1489 1491 1508 protected String findLibrary(String libname) { 1509 return null; 1510 } 1511 1512 1528 static class NativeLibrary { 1529 long handle; 1531 private int jniVersion; 1533 private Class fromClass; 1536 String name; 1538 1539 native void load(String name); 1540 native long find(String name); 1541 native void unload(); 1542 1543 public NativeLibrary(Class fromClass, String name) { 1544 this.name = name; 1545 this.fromClass = fromClass; 1546 } 1547 1548 protected void finalize() { 1549 synchronized (loadedLibraryNames) { 1550 if (fromClass.getClassLoader() != null && handle != 0) { 1551 1552 int size = loadedLibraryNames.size(); 1553 for (int i = 0; i < size; i++) { 1554 if (name.equals(loadedLibraryNames.elementAt(i))) { 1555 loadedLibraryNames.removeElementAt(i); 1556 break; 1557 } 1558 } 1559 1560 ClassLoader.nativeLibraryContext.push(this); 1561 try { 1562 unload(); 1563 } finally { 1564 ClassLoader.nativeLibraryContext.pop(); 1565 } 1566 } 1567 } 1568 } 1569 static Class getFromClass() { 1572 return ((NativeLibrary) 1573 (ClassLoader.nativeLibraryContext.peek())).fromClass; 1574 } 1575 } 1576 1577 private ProtectionDomain defaultDomain = null; 1580 1581 private synchronized ProtectionDomain getDefaultDomain() { 1583 if (defaultDomain == null) { 1584 CodeSource cs = 1585 new CodeSource (null, (java.security.cert.Certificate []) null); 1586 defaultDomain = new ProtectionDomain (cs, null, this, null); 1587 } 1588 return defaultDomain; 1589 } 1590 1591 private static Vector loadedLibraryNames = new Vector (); 1593 private static Vector systemNativeLibraries = new Vector (); 1595 private Vector nativeLibraries = new Vector (); 1597 1598 private static Stack nativeLibraryContext = new Stack (); 1600 1601 static private String usr_paths[]; 1603 static private String sys_paths[]; 1604 1605 private static String [] initializePath(String propname) { 1606 String ldpath = System.getProperty(propname, ""); 1607 String ps = File.pathSeparator; 1608 int ldlen = ldpath.length(); 1609 int i, j, n; 1610 i = ldpath.indexOf(ps); 1612 n = 0; 1613 while (i >= 0) { 1614 n++; 1615 i = ldpath.indexOf(ps, i + 1); 1616 } 1617 1618 String [] paths = new String [n + 1]; 1620 1621 n = i = 0; 1623 j = ldpath.indexOf(ps); 1624 while (j >= 0) { 1625 if (j - i > 0) { 1626 paths[n++] = ldpath.substring(i, j); 1627 } else if (j - i == 0) { 1628 paths[n++] = "."; 1629 } 1630 i = j + 1; 1631 j = ldpath.indexOf(ps, i); 1632 } 1633 paths[n] = ldpath.substring(i, ldlen); 1634 return paths; 1635 } 1636 1637 static void loadLibrary(Class fromClass, String name, 1639 boolean isAbsolute) { 1640 ClassLoader loader = 1641 (fromClass == null) ? null : fromClass.getClassLoader(); 1642 if (sys_paths == null) { 1643 usr_paths = initializePath("java.library.path"); 1644 sys_paths = initializePath("sun.boot.library.path"); 1645 } 1646 if (isAbsolute) { 1647 if (loadLibrary0(fromClass, new File (name))) { 1648 return; 1649 } 1650 throw new UnsatisfiedLinkError ("Can't load library: " + name); 1651 } 1652 if (loader != null) { 1653 String libfilename = loader.findLibrary(name); 1654 if (libfilename != null) { 1655 File libfile = new File (libfilename); 1656 if (!libfile.isAbsolute()) { 1657 throw new UnsatisfiedLinkError ( 1658 "ClassLoader.findLibrary failed to return an absolute path: " + libfilename); 1659 } 1660 if (loadLibrary0(fromClass, libfile)) { 1661 return; 1662 } 1663 throw new UnsatisfiedLinkError ("Can't load " + libfilename); 1664 } 1665 } 1666 for (int i = 0 ; i < sys_paths.length ; i++) { 1667 File libfile = new File (sys_paths[i], System.mapLibraryName(name)); 1668 if (loadLibrary0(fromClass, libfile)) { 1669 return; 1670 } 1671 } 1672 if (loader != null) { 1673 for (int i = 0 ; i < usr_paths.length ; i++) { 1674 File libfile = new File (usr_paths[i], 1675 System.mapLibraryName(name)); 1676 if (loadLibrary0(fromClass, libfile)) { 1677 return; 1678 } 1679 } 1680 } 1681 throw new UnsatisfiedLinkError ("no " + name + " in java.library.path"); 1683 } 1684 1685 private static boolean loadLibrary0(Class fromClass, final File file) { 1686 Boolean exists = (Boolean ) 1687 AccessController.doPrivileged(new PrivilegedAction () { 1688 public Object run() { 1689 return new Boolean (file.exists()); 1690 } 1691 }); 1692 if (!exists.booleanValue()) { 1693 return false; 1694 } 1695 String name; 1696 try { 1697 name = file.getCanonicalPath(); 1698 } catch (IOException e) { 1699 return false; 1700 } 1701 ClassLoader loader = 1702 (fromClass == null) ? null : fromClass.getClassLoader(); 1703 Vector libs = 1704 loader != null ? loader.nativeLibraries : systemNativeLibraries; 1705 synchronized (libs) { 1706 int size = libs.size(); 1707 for (int i = 0; i < size; i++) { 1708 NativeLibrary lib = (NativeLibrary)libs.elementAt(i); 1709 if (name.equals(lib.name)) { 1710 return true; 1711 } 1712 } 1713 1714 synchronized (loadedLibraryNames) { 1715 if (loadedLibraryNames.contains(name)) { 1716 throw new UnsatisfiedLinkError 1717 ("Native Library " + 1718 name + 1719 " already loaded in another classloader"); 1720 } 1721 1733 int n = nativeLibraryContext.size(); 1734 for (int i = 0; i < n; i++) { 1735 NativeLibrary lib = (NativeLibrary) 1736 nativeLibraryContext.elementAt(i); 1737 if (name.equals(lib.name)) { 1738 if (loader == lib.fromClass.getClassLoader()) { 1739 return true; 1740 } else { 1741 throw new UnsatisfiedLinkError 1742 ("Native Library " + 1743 name + 1744 " is being loaded in another classloader"); 1745 } 1746 } 1747 } 1748 NativeLibrary lib = new NativeLibrary(fromClass, name); 1749 nativeLibraryContext.push(lib); 1750 try { 1751 lib.load(name); 1752 } finally { 1753 nativeLibraryContext.pop(); 1754 } 1755 if (lib.handle != 0) { 1756 loadedLibraryNames.addElement(name); 1757 libs.addElement(lib); 1758 return true; 1759 } 1760 return false; 1761 } 1762 } 1763 } 1764 1765 static long findNative(ClassLoader loader, String name) { 1767 Vector libs = 1768 loader != null ? loader.nativeLibraries : systemNativeLibraries; 1769 synchronized (libs) { 1770 int size = libs.size(); 1771 for (int i = 0; i < size; i++) { 1772 NativeLibrary lib = (NativeLibrary)libs.elementAt(i); 1773 long entry = lib.find(name); 1774 if (entry != 0) 1775 return entry; 1776 } 1777 } 1778 return 0; 1779 } 1780 1781 1782 1784 private boolean defaultAssertionStatus = false; 1786 1787 private Map packageAssertionStatus = null; 1793 1794 Map classAssertionStatus = null; 1799 1800 1815 public synchronized void setDefaultAssertionStatus(boolean enabled) { 1816 if (classAssertionStatus == null) 1817 initializeJavaAssertionMaps(); 1818 1819 defaultAssertionStatus = enabled; 1820 } 1821 1822 1859 public synchronized void setPackageAssertionStatus(String packageName, 1860 boolean enabled) 1861 { 1862 if (packageAssertionStatus == null) 1863 initializeJavaAssertionMaps(); 1864 1865 packageAssertionStatus.put(packageName, Boolean.valueOf(enabled)); 1866 } 1867 1868 1891 public synchronized void setClassAssertionStatus(String className, 1892 boolean enabled) 1893 { 1894 if (classAssertionStatus == null) 1895 initializeJavaAssertionMaps(); 1896 1897 classAssertionStatus.put(className, Boolean.valueOf(enabled)); 1898 } 1899 1900 1910 public synchronized void clearAssertionStatus() { 1911 1915 classAssertionStatus = new HashMap (); 1916 packageAssertionStatus = new HashMap (); 1917 1918 defaultAssertionStatus = false; 1919 } 1920 1921 1943 synchronized boolean desiredAssertionStatus(String className) { 1944 Boolean result; 1945 1946 1949 result = (Boolean )classAssertionStatus.get(className); 1951 if (result != null) 1952 return result.booleanValue(); 1953 1954 int dotIndex = className.lastIndexOf("."); 1956 if (dotIndex < 0) { result = (Boolean )packageAssertionStatus.get(null); 1958 if (result != null) 1959 return result.booleanValue(); 1960 } 1961 while(dotIndex > 0) { 1962 className = className.substring(0, dotIndex); 1963 result = (Boolean )packageAssertionStatus.get(className); 1964 if (result != null) 1965 return result.booleanValue(); 1966 dotIndex = className.lastIndexOf(".", dotIndex-1); 1967 } 1968 1969 return defaultAssertionStatus; 1971 } 1972 1973 private void initializeJavaAssertionMaps() { 1975 1977 classAssertionStatus = new HashMap (); 1978 packageAssertionStatus = new HashMap (); 1979 AssertionStatusDirectives directives = retrieveDirectives(); 1980 1981 for(int i = 0; i < directives.classes.length; i++) 1982 classAssertionStatus.put(directives.classes[i], 1983 Boolean.valueOf(directives.classEnabled[i])); 1984 1985 for(int i = 0; i < directives.packages.length; i++) 1986 packageAssertionStatus.put(directives.packages[i], 1987 Boolean.valueOf(directives.packageEnabled[i])); 1988 1989 defaultAssertionStatus = directives.deflt; 1990 } 1991 1992 private static native AssertionStatusDirectives retrieveDirectives(); 1994} 1995 1996 1997class SystemClassLoaderAction implements PrivilegedExceptionAction { 1998 private ClassLoader parent; 1999 2000 SystemClassLoaderAction(ClassLoader parent) { 2001 this.parent = parent; 2002 } 2003 2004 public Object run() throws Exception { 2005 ClassLoader sys; 2006 Constructor ctor; 2007 Class c; 2008 Class cp[] = { ClassLoader .class }; 2009 Object params[] = { parent }; 2010 2011 String cls = System.getProperty("java.system.class.loader"); 2012 if (cls == null) { 2013 return parent; 2014 } 2015 2016 c = Class.forName(cls, true, parent); 2017 ctor = c.getDeclaredConstructor(cp); 2018 sys = (ClassLoader ) ctor.newInstance(params); 2019 Thread.currentThread().setContextClassLoader(sys); 2020 return sys; 2021 } 2022} 2023 | Popular Tags |