[Patch] java.net.URLClassLoader

Michael Koch konqueror@gmx.de
Thu Nov 18 20:26:00 GMT 2004


Hi list,


I just commited the attached patch to make java.net.URLClassLoader more
merged with GNU classpath.


Michael


2004-11-18  Michael Koch  <konqueror@gmx.de>

	* java/net/URLClassLoader.java: Whitespace cleanup.

-------------- next part --------------
Index: java/net/URLClassLoader.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/URLClassLoader.java,v
retrieving revision 1.20
diff -u -r1.20 URLClassLoader.java
--- java/net/URLClassLoader.java	16 Nov 2004 11:30:12 -0000	1.20
+++ java/net/URLClassLoader.java	18 Nov 2004 18:58:06 -0000
@@ -35,6 +35,7 @@
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
+
 package java.net;
 
 import java.io.ByteArrayOutputStream;
@@ -248,9 +249,9 @@
     {
       Certificate[] certs = getCertificates();
       if (certs == null)
-	return loader.noCertCodeSource;
+        return loader.noCertCodeSource;
       else
-	return new CodeSource(loader.baseURL, certs);
+        return new CodeSource(loader.baseURL, certs);
     }
 
     /**
@@ -305,15 +306,15 @@
       JarFile jarfile = null;
       try
         {
-	  baseJarURL =
-	    new URL(null, jarURL, classloader.getURLStreamHandler("jar"));
+          baseJarURL =
+            new URL(null, jarURL, classloader.getURLStreamHandler("jar"));
 
-	  jarfile =
-	    ((JarURLConnection) baseJarURL.openConnection()).getJarFile();
+          jarfile =
+            ((JarURLConnection) baseJarURL.openConnection()).getJarFile();
         }
       catch (IOException ioe)
         {
-	  /* ignored */
+          /* ignored */
         }
 
       this.baseJarURL = baseJarURL;
@@ -324,27 +325,27 @@
     Resource getResource(String name)
     {
       if (jarfile == null)
-	return null;
+        return null;
 
       if (name.startsWith("/"))
-	name = name.substring(1);
+        name = name.substring(1);
 
       JarEntry je = jarfile.getJarEntry(name);
       if (je != null)
-	return new JarURLResource(this, name, je);
+        return new JarURLResource(this, name, je);
       else
-	return null;
+        return null;
     }
 
     Manifest getManifest()
     {
       try
         {
-	  return (jarfile == null) ? null : jarfile.getManifest();
+          return (jarfile == null) ? null : jarfile.getManifest();
         }
       catch (IOException ioe)
         {
-	  return null;
+          return null;
         }
     }
   }
@@ -378,14 +379,14 @@
     {
       try
         {
-	  return new URL(((JarURLLoader) loader).baseJarURL, name,
-	                 loader.classloader.getURLStreamHandler("jar"));
+          return new URL(((JarURLLoader) loader).baseJarURL, name,
+                         loader.classloader.getURLStreamHandler("jar"));
         }
       catch (MalformedURLException e)
         {
-	  InternalError ie = new InternalError();
-	  ie.initCause(e);
-	  throw ie;
+          InternalError ie = new InternalError();
+          ie.initCause(e);
+          throw ie;
         }
     }
   }
@@ -411,32 +412,32 @@
     {
       try
         {
-	  URL url =
-	    new URL(baseURL, name, classloader.getURLStreamHandler(protocol));
-	  URLConnection connection = url.openConnection();
-
-	  // Open the connection and check the stream
-	  // just to be sure it exists.
-	  int length = connection.getContentLength();
-	  InputStream stream = connection.getInputStream();
-
-	  // We can do some extra checking if it is a http request
-	  if (connection instanceof HttpURLConnection)
-	    {
-	      int response =
-		((HttpURLConnection) connection).getResponseCode();
-	      if (response / 100 != 2)
-		return null;
-	    }
-
-	  if (stream != null)
-	    return new RemoteResource(this, name, url, stream, length);
-	  else
-	    return null;
+          URL url =
+            new URL(baseURL, name, classloader.getURLStreamHandler(protocol));
+          URLConnection connection = url.openConnection();
+
+          // Open the connection and check the stream
+          // just to be sure it exists.
+          int length = connection.getContentLength();
+          InputStream stream = connection.getInputStream();
+
+          // We can do some extra checking if it is a http request
+          if (connection instanceof HttpURLConnection)
+            {
+              int response =
+                ((HttpURLConnection) connection).getResponseCode();
+              if (response / 100 != 2)
+                return null;
+            }
+
+          if (stream != null)
+            return new RemoteResource(this, name, url, stream, length);
+          else
+            return null;
         }
       catch (IOException ioe)
         {
-	  return null;
+          return null;
         }
     }
   }
@@ -551,7 +552,7 @@
     {
       File file = new File(dir, name);
       if (file.exists() && ! file.isDirectory())
-	return new FileResource(this, name, file);
+        return new FileResource(this, name, file);
       return null;
     }
   }
@@ -580,14 +581,14 @@
     {
       try
         {
-	  return new URL(loader.baseURL, name,
-	                 loader.classloader.getURLStreamHandler("file"));
+          return new URL(loader.baseURL, name,
+                         loader.classloader.getURLStreamHandler("file"));
         }
       catch (MalformedURLException e)
         {
-	  InternalError ie = new InternalError();
-	  ie.initCause(e);
-	  throw ie;
+          InternalError ie = new InternalError();
+          ie.initCause(e);
+          throw ie;
         }
     }
   }
@@ -711,12 +712,12 @@
     addURLs(urls);
 
     // If this factory is still not in factoryCache, add it,
-    //   since we only support three protocols so far, 5 is enough 
+    //   since we only support three protocols so far, 5 is enough
     //   for cache initial size
     synchronized (factoryCache)
       {
-	if (factory != null && factoryCache.get(factory) == null)
-	  factoryCache.put(factory, new HashMap(5));
+        if (factory != null && factoryCache.get(factory) == null)
+          factoryCache.put(factory, new HashMap(5));
       }
   }
 
@@ -735,32 +736,33 @@
   {
     synchronized (urlloaders)
       {
-	if (newUrl == null)
-	  return; // Silently ignore...
+        if (newUrl == null)
+          return; // Silently ignore...
 
-	// Check global cache to see if there're already url loader
-	// for this url.
-	URLLoader loader = (URLLoader) urlloaders.get(newUrl);
-	if (loader == null)
-	  {
-	    String file = newUrl.getFile();
-	    String protocol = newUrl.getProtocol();
-	    // Check that it is not a directory
+        // Check global cache to see if there're already url loader
+        // for this url.
+        URLLoader loader = (URLLoader) urlloaders.get(newUrl);
+        if (loader == null)
+          {
+            String file = newUrl.getFile();
+            String protocol = newUrl.getProtocol();
+
+            // Check that it is not a directory
 	    if ("gcjlib".equals(protocol))
 	      loader = new SoURLLoader(this, newUrl);
 	    else if (! (file.endsWith("/") || file.endsWith(File.separator)))
-	      loader = new JarURLLoader(this, newUrl);
-	    else if ("file".equals(protocol))
-	      loader = new FileURLLoader(this, newUrl);
-	    else
-	      loader = new RemoteURLLoader(this, newUrl);
-
-	    // Cache it.
-	    urlloaders.put(newUrl, loader);
-	  }
+              loader = new JarURLLoader(this, newUrl);
+            else if ("file".equals(protocol))
+              loader = new FileURLLoader(this, newUrl);
+            else
+              loader = new RemoteURLLoader(this, newUrl);
+
+            // Cache it.
+            urlloaders.put(newUrl, loader);
+          }
 
-	urls.add(newUrl);
-	urlinfos.add(loader);
+        urls.add(newUrl);
+        urlinfos.add(loader);
       }
   }
 
@@ -849,8 +851,8 @@
     // construct the class (and watch out for those nasty IOExceptions)
     try
       {
-	byte[] data;
-	InputStream in = resource.getInputStream();
+        byte[] data;
+        InputStream in = resource.getInputStream();
 	int length = resource.getLength();
 	if (length != -1)
 	  {
@@ -881,50 +883,50 @@
 		  out.write(b, 0, l);
 	      }
 	    data = out.toByteArray();
-	  }
-	final byte[] classData = data;
+          }
+        final byte[] classData = data;
 
-	// Now get the CodeSource
-	final CodeSource source = resource.getCodeSource();
+        // Now get the CodeSource
+        final CodeSource source = resource.getCodeSource();
 
-	// Find out package name
-	String packageName = null;
-	int lastDot = className.lastIndexOf('.');
-	if (lastDot != -1)
-	  packageName = className.substring(0, lastDot);
-
-	if (packageName != null && getPackage(packageName) == null)
-	  {
-	    // define the package
-	    Manifest manifest = resource.loader.getManifest();
-	    if (manifest == null)
-	      definePackage(packageName, null, null, null, null, null, null,
-	                    null);
-	    else
-	      definePackage(packageName, manifest, resource.loader.baseURL);
-	  }
-
-	// And finally construct the class!
-	SecurityManager sm = System.getSecurityManager();
-	if (sm != null && securityContext != null)
-	  {
+        // Find out package name
+        String packageName = null;
+        int lastDot = className.lastIndexOf('.');
+        if (lastDot != -1)
+          packageName = className.substring(0, lastDot);
+
+        if (packageName != null && getPackage(packageName) == null)
+          {
+            // define the package
+            Manifest manifest = resource.loader.getManifest();
+            if (manifest == null)
+              definePackage(packageName, null, null, null, null, null, null,
+                            null);
+            else
+              definePackage(packageName, manifest, resource.loader.baseURL);
+          }
+
+        // And finally construct the class!
+        SecurityManager sm = System.getSecurityManager();
+        if (sm != null && securityContext != null)
+          {
 	    return (Class)AccessController.doPrivileged
-	      (new PrivilegedAction()
-		{
-		  public Object run()
-		  {
-		    return defineClass(className, classData,
-				       0, classData.length,
-				       source);
-		  }
-		}, securityContext);
-	  }
-	else
+              (new PrivilegedAction()
+                {
+                  public Object run()
+                  {
+                    return defineClass(className, classData,
+                                       0, classData.length,
+                                       source);
+                  }
+                }, securityContext);
+          }
+        else
 	  return defineClass(className, classData, 0, classData.length, source);
       }
     catch (IOException ioe)
       {
-	throw new ClassNotFoundException(className, ioe);
+        throw new ClassNotFoundException(className, ioe);
       }
   }
 
@@ -940,13 +942,13 @@
     int max = urls.size();
     for (int i = 0; i < max; i++)
       {
-	URLLoader loader = (URLLoader) urlinfos.elementAt(i);
-	if (loader == null)
-	  continue;
-
-	Resource resource = loader.getResource(resourceName);
-	if (resource != null)
-	  return resource;
+        URLLoader loader = (URLLoader) urlinfos.elementAt(i);
+        if (loader == null)
+          continue;
+
+        Resource resource = loader.getResource(resourceName);
+        if (resource != null)
+          return resource;
       }
     return null;
   }
@@ -982,15 +984,15 @@
     URLStreamHandler handler;
     synchronized (factoryCache)
       {
-	// Check if there're handler for the same protocol in cache.
-	HashMap cache = (HashMap) factoryCache.get(factory);
-	handler = (URLStreamHandler) cache.get(protocol);
-	if (handler == null)
-	  {
-	    // Add it to cache.
-	    handler = factory.createURLStreamHandler(protocol);
-	    cache.put(protocol, handler);
-	  }
+        // Check if there're handler for the same protocol in cache.
+        HashMap cache = (HashMap) factoryCache.get(factory);
+        handler = (URLStreamHandler) cache.get(protocol);
+        if (handler == null)
+          {
+            // Add it to cache.
+            handler = factory.createURLStreamHandler(protocol);
+            cache.put(protocol, handler);
+          }
       }
     return handler;
   }
@@ -1011,10 +1013,10 @@
     int max = urls.size();
     for (int i = 0; i < max; i++)
       {
-	URLLoader loader = (URLLoader) urlinfos.elementAt(i);
-	Resource resource = loader.getResource(resourceName);
-	if (resource != null)
-	  resources.add(resource.getURL());
+        URLLoader loader = (URLLoader) urlinfos.elementAt(i);
+        Resource resource = loader.getResource(resourceName);
+        if (resource != null)
+          resources.add(resource.getURL());
       }
     return resources.elements();
   }
@@ -1051,28 +1053,28 @@
     String protocol = url.getProtocol();
     if (protocol.equals("file"))
       {
-	String file = url.getFile();
+        String file = url.getFile();
 
-	// If the file end in / it must be an directory.
-	if (file.endsWith("/") || file.endsWith(File.separator))
-	  {
-	    // Grant permission to read everything in that directory and
-	    // all subdirectories.
-	    permissions.add(new FilePermission(file + "-", "read"));
-	  }
-	else
-	  {
-	    // It is a 'normal' file.
-	    // Grant permission to access that file.
-	    permissions.add(new FilePermission(file, "read"));
-	  }
+        // If the file end in / it must be an directory.
+        if (file.endsWith("/") || file.endsWith(File.separator))
+          {
+            // Grant permission to read everything in that directory and
+            // all subdirectories.
+            permissions.add(new FilePermission(file + "-", "read"));
+          }
+        else
+          {
+            // It is a 'normal' file.
+            // Grant permission to access that file.
+            permissions.add(new FilePermission(file, "read"));
+          }
       }
     else
       {
-	// Grant permission to connect to and accept connections from host
-	String host = url.getHost();
-	if (host != null)
-	  permissions.add(new SocketPermission(host, "connect,accept"));
+        // Grant permission to connect to and accept connections from host
+        String host = url.getHost();
+        if (host != null)
+          permissions.add(new SocketPermission(host, "connect,accept"));
       }
 
     return permissions;
@@ -1130,24 +1132,24 @@
       return new URLClassLoader(urls, parent);
     else
       {
-	final Object securityContext = sm.getSecurityContext();
-
-	// XXX - What to do with anything else then an AccessControlContext?
-	if (! (securityContext instanceof AccessControlContext))
-	  throw new SecurityException("securityContext must be AccessControlContext: "
-	                              + securityContext);
+        final Object securityContext = sm.getSecurityContext();
 
-	URLClassLoader loader =
-	  (URLClassLoader) AccessController.doPrivileged(new PrivilegedAction()
-	      {
-		public Object run()
-		{
-		  return new URLClassLoader(parent,
-		                            (AccessControlContext) securityContext);
-		}
-	      });
-	loader.addURLs(urls);
-	return loader;
+        // XXX - What to do with anything else then an AccessControlContext?
+        if (! (securityContext instanceof AccessControlContext))
+          throw new SecurityException("securityContext must be AccessControlContext: "
+                                      + securityContext);
+
+        URLClassLoader loader =
+          (URLClassLoader) AccessController.doPrivileged(new PrivilegedAction()
+              {
+                public Object run()
+                {
+                  return new URLClassLoader(parent,
+                                            (AccessControlContext) securityContext);
+                }
+              });
+        loader.addURLs(urls);
+        return loader;
       }
   }
 }


More information about the Java-patches mailing list