| 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 ( |