< prev index next >

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

Print this page
rev 1564 : 7090158: Networking Libraries don't build with javac -Werror
7125055: ContentHandler.getContent API changed in error
Summary: Minor changes to networking java files to remove warnings
Reviewed-by: chegar, weijun, hawtin, alanb
Contributed-by: kurchi.subhra.hazra@oracle.com, sasha_bu@hotmail.com


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.lang.reflect.Method;
  29 import java.lang.reflect.Modifier;
  30 import java.io.File;
  31 import java.io.FilePermission;
  32 import java.io.InputStream;
  33 import java.io.IOException;
  34 import java.net.URL;
  35 import java.net.URLConnection;
  36 import java.net.URLStreamHandlerFactory;
  37 import java.util.Enumeration;
  38 import java.util.NoSuchElementException;
  39 import java.util.StringTokenizer;
  40 import java.util.jar.Manifest;
  41 import java.util.jar.Attributes;
  42 import java.util.jar.Attributes.Name;
  43 import java.security.CodeSigner;
  44 import java.security.PrivilegedAction;
  45 import java.security.PrivilegedExceptionAction;
  46 import java.security.AccessController;
  47 import java.security.AccessControlContext;
  48 import java.security.SecureClassLoader;
  49 import java.security.CodeSource;
  50 import java.security.Permission;
  51 import java.security.PermissionCollection;
  52 import sun.misc.Resource;
  53 import sun.misc.URLClassPath;
  54 import sun.net.www.ParseUtil;
  55 import sun.security.util.SecurityConstants;
  56 


 186      * @return the search path of URLs for loading classes and resources.
 187      */
 188     public URL[] getURLs() {
 189         return ucp.getURLs();
 190     }
 191 
 192     /**
 193      * Finds and loads the class with the specified name from the URL search
 194      * path. Any URLs referring to JAR files are loaded and opened as needed
 195      * until the class is found.
 196      *
 197      * @param name the name of the class
 198      * @return the resulting class
 199      * @exception ClassNotFoundException if the class could not be found
 200      */
 201     protected Class<?> findClass(final String name)
 202          throws ClassNotFoundException
 203     {
 204         try {
 205             return AccessController.doPrivileged(
 206                 new PrivilegedExceptionAction<Class>() {
 207                     public Class run() throws ClassNotFoundException {
 208                         String path = name.replace('.', '/').concat(".class");
 209                         Resource res = ucp.getResource(path, false);
 210                         if (res != null) {
 211                             try {
 212                                 return defineClass(name, res);
 213                             } catch (IOException e) {
 214                                 throw new ClassNotFoundException(name, e);
 215                             }
 216                         } else {
 217                             throw new ClassNotFoundException(name);
 218                         }
 219                     }
 220                 }, acc);
 221         } catch (java.security.PrivilegedActionException pae) {
 222             throw (ClassNotFoundException) pae.getException();
 223         }
 224     }
 225 
 226     /*
 227      * Defines a Class using the class bytes obtained from the specified
 228      * Resource. The resulting Class must be resolved before it can be
 229      * used.
 230      */
 231     private Class defineClass(String name, Resource res) throws IOException {
 232         int i = name.lastIndexOf('.');
 233         URL url = res.getCodeSourceURL();
 234         if (i != -1) {
 235             String pkgname = name.substring(0, i);
 236             // Check if package already loaded.
 237             Package pkg = getPackage(pkgname);
 238             Manifest man = res.getManifest();
 239             if (pkg != null) {
 240                 // Package found, so check package sealing.
 241                 if (pkg.isSealed()) {
 242                     // Verify that code source URL is the same.
 243                     if (!pkg.isSealed(url)) {
 244                         throw new SecurityException(
 245                             "sealing violation: package " + pkgname + " is sealed");
 246                     }
 247 
 248                 } else {
 249                     // Make sure we are not attempting to seal the package
 250                     // at this code source URL.
 251                     if ((man != null) && isSealed(pkgname, man)) {


 587                 }
 588 
 589                 public String getOriginalHostName(InetAddress ia) {
 590                     return ia.holder.getOriginalHostName();
 591                 }
 592             }
 593         );
 594     }
 595 }
 596 
 597 final class FactoryURLClassLoader extends URLClassLoader {
 598 
 599     FactoryURLClassLoader(URL[] urls, ClassLoader parent) {
 600         super(urls, parent);
 601     }
 602 
 603     FactoryURLClassLoader(URL[] urls) {
 604         super(urls);
 605     }
 606 
 607     public final synchronized Class loadClass(String name, boolean resolve)
 608         throws ClassNotFoundException
 609     {
 610         // First check if we have permission to access the package. This
 611         // should go away once we've added support for exported packages.
 612         SecurityManager sm = System.getSecurityManager();
 613         if (sm != null) {
 614             int i = name.lastIndexOf('.');
 615             if (i != -1) {
 616                 sm.checkPackageAccess(name.substring(0, i));
 617             }
 618         }
 619         return super.loadClass(name, resolve);
 620     }
 621 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 


  28 import java.io.File;
  29 import java.io.FilePermission;
  30 import java.io.InputStream;
  31 import java.io.IOException;



  32 import java.util.Enumeration;
  33 import java.util.NoSuchElementException;
  34 import java.util.StringTokenizer;
  35 import java.util.jar.Manifest;
  36 import java.util.jar.Attributes;
  37 import java.util.jar.Attributes.Name;
  38 import java.security.CodeSigner;
  39 import java.security.PrivilegedAction;
  40 import java.security.PrivilegedExceptionAction;
  41 import java.security.AccessController;
  42 import java.security.AccessControlContext;
  43 import java.security.SecureClassLoader;
  44 import java.security.CodeSource;
  45 import java.security.Permission;
  46 import java.security.PermissionCollection;
  47 import sun.misc.Resource;
  48 import sun.misc.URLClassPath;
  49 import sun.net.www.ParseUtil;
  50 import sun.security.util.SecurityConstants;
  51 


 181      * @return the search path of URLs for loading classes and resources.
 182      */
 183     public URL[] getURLs() {
 184         return ucp.getURLs();
 185     }
 186 
 187     /**
 188      * Finds and loads the class with the specified name from the URL search
 189      * path. Any URLs referring to JAR files are loaded and opened as needed
 190      * until the class is found.
 191      *
 192      * @param name the name of the class
 193      * @return the resulting class
 194      * @exception ClassNotFoundException if the class could not be found
 195      */
 196     protected Class<?> findClass(final String name)
 197          throws ClassNotFoundException
 198     {
 199         try {
 200             return AccessController.doPrivileged(
 201                 new PrivilegedExceptionAction<Class<?>>() {
 202                     public Class<?> run() throws ClassNotFoundException {
 203                         String path = name.replace('.', '/').concat(".class");
 204                         Resource res = ucp.getResource(path, false);
 205                         if (res != null) {
 206                             try {
 207                                 return defineClass(name, res);
 208                             } catch (IOException e) {
 209                                 throw new ClassNotFoundException(name, e);
 210                             }
 211                         } else {
 212                             throw new ClassNotFoundException(name);
 213                         }
 214                     }
 215                 }, acc);
 216         } catch (java.security.PrivilegedActionException pae) {
 217             throw (ClassNotFoundException) pae.getException();
 218         }
 219     }
 220 
 221     /*
 222      * Defines a Class using the class bytes obtained from the specified
 223      * Resource. The resulting Class must be resolved before it can be
 224      * used.
 225      */
 226     private Class<?> defineClass(String name, Resource res) throws IOException {
 227         int i = name.lastIndexOf('.');
 228         URL url = res.getCodeSourceURL();
 229         if (i != -1) {
 230             String pkgname = name.substring(0, i);
 231             // Check if package already loaded.
 232             Package pkg = getPackage(pkgname);
 233             Manifest man = res.getManifest();
 234             if (pkg != null) {
 235                 // Package found, so check package sealing.
 236                 if (pkg.isSealed()) {
 237                     // Verify that code source URL is the same.
 238                     if (!pkg.isSealed(url)) {
 239                         throw new SecurityException(
 240                             "sealing violation: package " + pkgname + " is sealed");
 241                     }
 242 
 243                 } else {
 244                     // Make sure we are not attempting to seal the package
 245                     // at this code source URL.
 246                     if ((man != null) && isSealed(pkgname, man)) {


 582                 }
 583 
 584                 public String getOriginalHostName(InetAddress ia) {
 585                     return ia.holder.getOriginalHostName();
 586                 }
 587             }
 588         );
 589     }
 590 }
 591 
 592 final class FactoryURLClassLoader extends URLClassLoader {
 593 
 594     FactoryURLClassLoader(URL[] urls, ClassLoader parent) {
 595         super(urls, parent);
 596     }
 597 
 598     FactoryURLClassLoader(URL[] urls) {
 599         super(urls);
 600     }
 601 
 602     public final synchronized Class<?> loadClass(String name, boolean resolve)
 603         throws ClassNotFoundException
 604     {
 605         // First check if we have permission to access the package. This
 606         // should go away once we've added support for exported packages.
 607         SecurityManager sm = System.getSecurityManager();
 608         if (sm != null) {
 609             int i = name.lastIndexOf('.');
 610             if (i != -1) {
 611                 sm.checkPackageAccess(name.substring(0, i));
 612             }
 613         }
 614         return super.loadClass(name, resolve);
 615     }
 616 }
< prev index next >