This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch: URLClassLoader change


Mark, what do you think of the appended patch?

The JDK uses absolute (actually canonical -- I tested) paths in its
CodeSource objects.  I think this makes sense; this is what the
appended implements.  It is kind of ugly since baseURL can't be final
any more.

I tried this program:

    public class pd
    {
      public static void main(String[] args)
      {
        System.out.println(pd.class.getProtectionDomain().getCodeSource());
      }
    }

I compared gij to java using this.  I also put it in a .jar file and
tried that, and also tried it with a symlink in the path to the jar.

Tom

2002-12-09  Tom Tromey  <tromey@redhat.com>

	* java/net/URLClassLoader.java (URLLoader.baseURL): No longer
	final.
	(URLLoader.noCertCodeSource): Likewise.
	(URLLoader(URLClassLoader)): New constructor.
	(URLLoader.setBaseURL): New method.
	(URLLoader(URLClassLoader,URL)): Use it.
	(JarURLLoader): Use canonical path.
	(JarURLResource.getURL): Use chained exception.
	(FileResource.getURL): Likewise.
	(FileURLLoader): Use canonical path.
	(FileURLLoader.getResource): Likewise.
	(addURL): Indentation fix.

Index: java/net/URLClassLoader.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/URLClassLoader.java,v
retrieving revision 1.11
diff -u -r1.11 URLClassLoader.java
--- java/net/URLClassLoader.java 9 Dec 2002 00:04:00 -0000 1.11
+++ java/net/URLClassLoader.java 9 Dec 2002 23:49:29 -0000
@@ -177,7 +177,7 @@
     /**
      * The base URL from which all resources are loaded.
      */
-    final URL baseURL;
+    URL baseURL;
 
     /**
      * A <code>CodeSource</code> without any associated certificates.
@@ -186,11 +186,22 @@
      * then it is safe to share the associated <code>CodeSource</code>
      * between them since <code>CodeSource</code> is immutable.
      */
-    final CodeSource noCertCodeSource;
+    CodeSource noCertCodeSource;
+
+    // Constructor used when we plan to initialize the fields by hand.
+    URLLoader(URLClassLoader classloader)
+    {
+      this.classloader = classloader;
+    }
 
     URLLoader(URLClassLoader classloader, URL baseURL)
     {
       this.classloader = classloader;
+      setBaseURL(baseURL);
+    }
+
+    void setBaseURL(URL baseURL)
+    {
       this.baseURL = baseURL;
       this.noCertCodeSource = new CodeSource(baseURL, null);
     }
@@ -212,7 +223,7 @@
       return null;
     }
   }
-    
+
   /** 
    * A <code>Resource</code> represents a resource in some
    * <code>URLLoader</code>. It also contains all information (e.g.,
@@ -282,8 +293,23 @@
 
     public JarURLLoader(URLClassLoader classloader, URL baseURL)
     {
-      super(classloader, baseURL);
-        
+      super(classloader);
+
+      if ("file".equals(baseURL.getProtocol()))
+	{
+	  // Use canonical file name.
+	  try
+	    {
+	      File f = new File(baseURL.getFile()).getCanonicalFile();
+	      baseURL = new URL("file", "", f.toString());
+	    }
+	  catch (IOException ignore)
+	    {
+	    }
+	}
+
+      setBaseURL(baseURL);
+
       // cache url prefix for all resources in this jar url
       String external = baseURL.toExternalForm();
       StringBuffer sb = new StringBuffer(external.length() + 6);
@@ -306,7 +332,7 @@
       this.baseJarURL = baseJarURL;
       this.jarfile = jarfile;
     }
-    
+
     /** get resource with the name "name" in the jar url */
     Resource getResource(String name)
     {
@@ -331,7 +357,6 @@
 	  return null;
 	}
     }
-      
   }
 
   final static class JarURLResource extends Resource
@@ -358,7 +383,7 @@
     {
       return entry.getCertificates();
     }
-                
+
     URL getURL()
     {
       try
@@ -368,7 +393,9 @@
 	}
       catch(MalformedURLException e)
 	{
-	  throw new InternalError(e.toString());
+	  InternalError ie = new InternalError();
+	  ie.initCause(e);
+	  throw ie;
 	}
     }
   }
@@ -468,19 +495,35 @@
 
     FileURLLoader(URLClassLoader classloader, URL url)
     {
-      super(classloader, url);
-      // Note that this must be a "file" protocol URL.
+      super(classloader);
       dir = new File(url.getFile());
+      try
+	{
+	  dir = dir.getCanonicalFile();
+	  // We don't need to change URL if the canonicalization
+	  // failed.
+	  url = new URL("file", "", dir.toString());
+	}
+      catch (IOException ignore)
+	{
+	}
+      setBaseURL(url);
     }
-    
+
     /** get resource with the name "name" in the file url */
     Resource getResource(String name)
     {
       File file = new File(dir, name);
+      try
+	{
+	  file = file.getCanonicalFile();
+	}
+      catch (IOException ignore)
+	{
+	}
       if (file.exists() && !file.isDirectory())
 	return new FileResource(this, name, file);
-      else
-	return null;
+      return null;
     }
   }
 
@@ -503,7 +546,7 @@
     {
       return (int)file.length();
     }
-                
+
     public URL getURL()
     {
       try
@@ -513,7 +556,9 @@
 	}
       catch(MalformedURLException e)
 	{
-	  throw new InternalError(e.toString());
+	  InternalError ie = new InternalError();
+	  ie.initCause(e);
+	  throw ie;
 	}
     }
   }
@@ -667,13 +712,12 @@
 	  {
 	    String file = newUrl.getFile();
 	    // Check that it is not a directory
-	    if (!(file.endsWith("/") || file.endsWith(File.separator)))
+	    if (! (file.endsWith("/") || file.endsWith(File.separator)))
 	      loader = new JarURLLoader(this, newUrl);
-	    else // it's a url that point to a jar file
-	      if ("file".equals(newUrl.getProtocol()))
-		loader = new FileURLLoader(this, newUrl);
-	      else
-		loader = new RemoteURLLoader(this, newUrl);
+	    else if ("file".equals(newUrl.getProtocol()))
+	      loader = new FileURLLoader(this, newUrl);
+	    else
+	      loader = new RemoteURLLoader(this, newUrl);
 
 	    // cache it
 	    urlloaders.put(newUrl, loader);


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]