< prev index next >

src/java.base/share/classes/java/net/URLClassLoader.java

Print this page




 342         return ucp.getURLs();
 343     }
 344 
 345     /**
 346      * Finds and loads the class with the specified name from the URL search
 347      * path. Any URLs referring to JAR files are loaded and opened as needed
 348      * until the class is found.
 349      *
 350      * @param name the name of the class
 351      * @return the resulting class
 352      * @exception ClassNotFoundException if the class could not be found,
 353      *            or if the loader is closed.
 354      * @exception NullPointerException if {@code name} is {@code null}.
 355      */
 356     protected Class<?> findClass(final String name)
 357         throws ClassNotFoundException
 358     {
 359         final Class<?> result;
 360         try {
 361             result = AccessController.doPrivileged(
 362                 new PrivilegedExceptionAction<Class<?>>() {
 363                     public Class<?> run() throws ClassNotFoundException {
 364                         String path = name.replace('.', '/').concat(".class");
 365                         Resource res = ucp.getResource(path, false);
 366                         if (res != null) {
 367                             try {
 368                                 return defineClass(name, res);
 369                             } catch (IOException e) {
 370                                 throw new ClassNotFoundException(name, e);
 371                             }
 372                         } else {
 373                             return null;
 374                         }
 375                     }
 376                 }, acc);
 377         } catch (java.security.PrivilegedActionException pae) {
 378             throw (ClassNotFoundException) pae.getException();
 379         }
 380         if (result == null) {
 381             throw new ClassNotFoundException(name);
 382         }


 547         if (sealed == null) {
 548             if ((attr = man.getMainAttributes()) != null) {
 549                 sealed = attr.getValue(Name.SEALED);
 550             }
 551         }
 552         return "true".equalsIgnoreCase(sealed);
 553     }
 554 
 555     /**
 556      * Finds the resource with the specified name on the URL search path.
 557      *
 558      * @param name the name of the resource
 559      * @return a {@code URL} for the resource, or {@code null}
 560      * if the resource could not be found, or if the loader is closed.
 561      */
 562     public URL findResource(final String name) {
 563         /*
 564          * The same restriction to finding classes applies to resources
 565          */
 566         URL url = AccessController.doPrivileged(
 567             new PrivilegedAction<URL>() {
 568                 public URL run() {
 569                     return ucp.findResource(name, true);
 570                 }
 571             }, acc);
 572 
 573         return url != null ? ucp.checkURL(url) : null;
 574     }
 575 
 576     /**
 577      * Returns an Enumeration of URLs representing all of the resources
 578      * on the URL search path having the specified name.
 579      *
 580      * @param name the resource name
 581      * @exception IOException if an I/O exception occurs
 582      * @return an {@code Enumeration} of {@code URL}s
 583      *         If the loader is closed, the Enumeration will be empty.
 584      */
 585     public Enumeration<URL> findResources(final String name)
 586         throws IOException
 587     {
 588         final Enumeration<URL> e = ucp.findResources(name, true);
 589 
 590         return new Enumeration<URL>() {
 591             private URL url = null;
 592 
 593             private boolean next() {
 594                 if (url != null) {
 595                     return true;
 596                 }
 597                 do {
 598                     URL u = AccessController.doPrivileged(
 599                         new PrivilegedAction<URL>() {
 600                             public URL run() {
 601                                 if (!e.hasMoreElements())
 602                                     return null;
 603                                 return e.nextElement();
 604                             }
 605                         }, acc);
 606                     if (u == null)
 607                         break;
 608                     url = ucp.checkURL(u);
 609                 } while (url == null);
 610                 return url != null;
 611             }
 612 
 613             public URL nextElement() {
 614                 if (!next()) {
 615                     throw new NoSuchElementException();
 616                 }
 617                 URL u = url;
 618                 url = null;
 619                 return u;


 687              * permission to connect to and accept from the remote host
 688              * after we've made sure the host is the correct one and is valid.
 689              */
 690             URL locUrl = url;
 691             if (urlConnection instanceof JarURLConnection) {
 692                 locUrl = ((JarURLConnection)urlConnection).getJarFileURL();
 693             }
 694             String host = locUrl.getHost();
 695             if (host != null && (host.length() > 0))
 696                 p = new SocketPermission(host,
 697                                          SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION);
 698         }
 699 
 700         // make sure the person that created this class loader
 701         // would have this permission
 702 
 703         if (p != null) {
 704             final SecurityManager sm = System.getSecurityManager();
 705             if (sm != null) {
 706                 final Permission fp = p;
 707                 AccessController.doPrivileged(new PrivilegedAction<Void>() {
 708                     public Void run() throws SecurityException {
 709                         sm.checkPermission(fp);
 710                         return null;
 711                     }
 712                 }, acc);
 713             }
 714             perms.add(p);
 715         }
 716         return perms;
 717     }
 718 
 719     /**
 720      * Creates a new instance of URLClassLoader for the specified
 721      * URLs and parent class loader. If a security manager is
 722      * installed, the {@code loadClass} method of the URLClassLoader
 723      * returned by this method will invoke the
 724      * {@code SecurityManager.checkPackageAccess} method before
 725      * loading the class.
 726      *
 727      * @param urls the URLs to search for classes and resources
 728      * @param parent the parent class loader for delegation
 729      * @exception  NullPointerException if {@code urls} is {@code null}.
 730      * @return the resulting class loader
 731      */
 732     public static URLClassLoader newInstance(final URL[] urls,
 733                                              final ClassLoader parent) {
 734         // Save the caller's context
 735         final AccessControlContext acc = AccessController.getContext();
 736         // Need a privileged block to create the class loader
 737         URLClassLoader ucl = AccessController.doPrivileged(
 738             new PrivilegedAction<URLClassLoader>() {
 739                 public URLClassLoader run() {
 740                     return new FactoryURLClassLoader(urls, parent, acc);
 741                 }
 742             });
 743         return ucl;
 744     }
 745 
 746     /**
 747      * Creates a new instance of URLClassLoader for the specified
 748      * URLs and default parent class loader. If a security manager is
 749      * installed, the {@code loadClass} method of the URLClassLoader
 750      * returned by this method will invoke the
 751      * {@code SecurityManager.checkPackageAccess} before
 752      * loading the class.
 753      *
 754      * @param urls the URLs to search for classes and resources
 755      * @exception  NullPointerException if {@code urls} is {@code null}.
 756      * @return the resulting class loader
 757      */
 758     public static URLClassLoader newInstance(final URL[] urls) {
 759         // Save the caller's context
 760         final AccessControlContext acc = AccessController.getContext();
 761         // Need a privileged block to create the class loader
 762         URLClassLoader ucl = AccessController.doPrivileged(
 763             new PrivilegedAction<URLClassLoader>() {
 764                 public URLClassLoader run() {
 765                     return new FactoryURLClassLoader(urls, acc);
 766                 }
 767             });
 768         return ucl;
 769     }
 770 
 771     static {
 772         sun.misc.SharedSecrets.setJavaNetAccess (
 773             new sun.misc.JavaNetAccess() {
 774                 public URLClassPath getURLClassPath (URLClassLoader u) {
 775                     return u.ucp;
 776                 }
 777             }
 778         );
 779         ClassLoader.registerAsParallelCapable();
 780     }
 781 }
 782 
 783 final class FactoryURLClassLoader extends URLClassLoader {




 342         return ucp.getURLs();
 343     }
 344 
 345     /**
 346      * Finds and loads the class with the specified name from the URL search
 347      * path. Any URLs referring to JAR files are loaded and opened as needed
 348      * until the class is found.
 349      *
 350      * @param name the name of the class
 351      * @return the resulting class
 352      * @exception ClassNotFoundException if the class could not be found,
 353      *            or if the loader is closed.
 354      * @exception NullPointerException if {@code name} is {@code null}.
 355      */
 356     protected Class<?> findClass(final String name)
 357         throws ClassNotFoundException
 358     {
 359         final Class<?> result;
 360         try {
 361             result = AccessController.doPrivileged(
 362                 new PrivilegedExceptionAction<>() {
 363                     public Class<?> run() throws ClassNotFoundException {
 364                         String path = name.replace('.', '/').concat(".class");
 365                         Resource res = ucp.getResource(path, false);
 366                         if (res != null) {
 367                             try {
 368                                 return defineClass(name, res);
 369                             } catch (IOException e) {
 370                                 throw new ClassNotFoundException(name, e);
 371                             }
 372                         } else {
 373                             return null;
 374                         }
 375                     }
 376                 }, acc);
 377         } catch (java.security.PrivilegedActionException pae) {
 378             throw (ClassNotFoundException) pae.getException();
 379         }
 380         if (result == null) {
 381             throw new ClassNotFoundException(name);
 382         }


 547         if (sealed == null) {
 548             if ((attr = man.getMainAttributes()) != null) {
 549                 sealed = attr.getValue(Name.SEALED);
 550             }
 551         }
 552         return "true".equalsIgnoreCase(sealed);
 553     }
 554 
 555     /**
 556      * Finds the resource with the specified name on the URL search path.
 557      *
 558      * @param name the name of the resource
 559      * @return a {@code URL} for the resource, or {@code null}
 560      * if the resource could not be found, or if the loader is closed.
 561      */
 562     public URL findResource(final String name) {
 563         /*
 564          * The same restriction to finding classes applies to resources
 565          */
 566         URL url = AccessController.doPrivileged(
 567             new PrivilegedAction<>() {
 568                 public URL run() {
 569                     return ucp.findResource(name, true);
 570                 }
 571             }, acc);
 572 
 573         return url != null ? ucp.checkURL(url) : null;
 574     }
 575 
 576     /**
 577      * Returns an Enumeration of URLs representing all of the resources
 578      * on the URL search path having the specified name.
 579      *
 580      * @param name the resource name
 581      * @exception IOException if an I/O exception occurs
 582      * @return an {@code Enumeration} of {@code URL}s
 583      *         If the loader is closed, the Enumeration will be empty.
 584      */
 585     public Enumeration<URL> findResources(final String name)
 586         throws IOException
 587     {
 588         final Enumeration<URL> e = ucp.findResources(name, true);
 589 
 590         return new Enumeration<>() {
 591             private URL url = null;
 592 
 593             private boolean next() {
 594                 if (url != null) {
 595                     return true;
 596                 }
 597                 do {
 598                     URL u = AccessController.doPrivileged(
 599                         new PrivilegedAction<>() {
 600                             public URL run() {
 601                                 if (!e.hasMoreElements())
 602                                     return null;
 603                                 return e.nextElement();
 604                             }
 605                         }, acc);
 606                     if (u == null)
 607                         break;
 608                     url = ucp.checkURL(u);
 609                 } while (url == null);
 610                 return url != null;
 611             }
 612 
 613             public URL nextElement() {
 614                 if (!next()) {
 615                     throw new NoSuchElementException();
 616                 }
 617                 URL u = url;
 618                 url = null;
 619                 return u;


 687              * permission to connect to and accept from the remote host
 688              * after we've made sure the host is the correct one and is valid.
 689              */
 690             URL locUrl = url;
 691             if (urlConnection instanceof JarURLConnection) {
 692                 locUrl = ((JarURLConnection)urlConnection).getJarFileURL();
 693             }
 694             String host = locUrl.getHost();
 695             if (host != null && (host.length() > 0))
 696                 p = new SocketPermission(host,
 697                                          SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION);
 698         }
 699 
 700         // make sure the person that created this class loader
 701         // would have this permission
 702 
 703         if (p != null) {
 704             final SecurityManager sm = System.getSecurityManager();
 705             if (sm != null) {
 706                 final Permission fp = p;
 707                 AccessController.doPrivileged(new PrivilegedAction<>() {
 708                     public Void run() throws SecurityException {
 709                         sm.checkPermission(fp);
 710                         return null;
 711                     }
 712                 }, acc);
 713             }
 714             perms.add(p);
 715         }
 716         return perms;
 717     }
 718 
 719     /**
 720      * Creates a new instance of URLClassLoader for the specified
 721      * URLs and parent class loader. If a security manager is
 722      * installed, the {@code loadClass} method of the URLClassLoader
 723      * returned by this method will invoke the
 724      * {@code SecurityManager.checkPackageAccess} method before
 725      * loading the class.
 726      *
 727      * @param urls the URLs to search for classes and resources
 728      * @param parent the parent class loader for delegation
 729      * @exception  NullPointerException if {@code urls} is {@code null}.
 730      * @return the resulting class loader
 731      */
 732     public static URLClassLoader newInstance(final URL[] urls,
 733                                              final ClassLoader parent) {
 734         // Save the caller's context
 735         final AccessControlContext acc = AccessController.getContext();
 736         // Need a privileged block to create the class loader
 737         URLClassLoader ucl = AccessController.doPrivileged(
 738             new PrivilegedAction<>() {
 739                 public URLClassLoader run() {
 740                     return new FactoryURLClassLoader(urls, parent, acc);
 741                 }
 742             });
 743         return ucl;
 744     }
 745 
 746     /**
 747      * Creates a new instance of URLClassLoader for the specified
 748      * URLs and default parent class loader. If a security manager is
 749      * installed, the {@code loadClass} method of the URLClassLoader
 750      * returned by this method will invoke the
 751      * {@code SecurityManager.checkPackageAccess} before
 752      * loading the class.
 753      *
 754      * @param urls the URLs to search for classes and resources
 755      * @exception  NullPointerException if {@code urls} is {@code null}.
 756      * @return the resulting class loader
 757      */
 758     public static URLClassLoader newInstance(final URL[] urls) {
 759         // Save the caller's context
 760         final AccessControlContext acc = AccessController.getContext();
 761         // Need a privileged block to create the class loader
 762         URLClassLoader ucl = AccessController.doPrivileged(
 763             new PrivilegedAction<>() {
 764                 public URLClassLoader run() {
 765                     return new FactoryURLClassLoader(urls, acc);
 766                 }
 767             });
 768         return ucl;
 769     }
 770 
 771     static {
 772         sun.misc.SharedSecrets.setJavaNetAccess (
 773             new sun.misc.JavaNetAccess() {
 774                 public URLClassPath getURLClassPath (URLClassLoader u) {
 775                     return u.ucp;
 776                 }
 777             }
 778         );
 779         ClassLoader.registerAsParallelCapable();
 780     }
 781 }
 782 
 783 final class FactoryURLClassLoader extends URLClassLoader {


< prev index next >