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]

PR java/14070: gij and -jar argument should set the manifest Class-path recursively


This is quite tricky, because the "Class-Path" attributes have to be
resolved relatively to the JARs in which they occur.

Classpath dudes: sorry, this is relative to my branch and not
Classpath mainline, but it should adapt easily enough.

Andrew.


2004-10-29  Andrew Haley  <aph@redhat.com>

	* java/net/URLClassLoader.java (JarURLLoader.classPath,
	JarURLLoader.extensionURLLoaders): new fields.
	(JarURLLoader.getExtensionURLLoaders): New method.
	(URLClassLoader.getURLLoader): New method broken out from addURLImpl.
	(JarURLLoader.JarURLLoader): Read mainfest to get "Class-Path"
	attribute.
	(JarURLLoader.getResource): Scan JARs in the "Class-Path".

Index: URLClassLoader.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/URLClassLoader.java,v
retrieving revision 1.16.18.4
diff -c -2 -p -r1.16.18.4 URLClassLoader.java
*** URLClassLoader.java	12 Oct 2004 14:53:07 -0000	1.16.18.4
--- URLClassLoader.java	29 Oct 2004 14:53:28 -0000
*************** import java.security.cert.Certificate;
*** 54,57 ****
--- 54,59 ----
  import java.util.Enumeration;
  import java.util.HashMap;
+ import java.util.Iterator;
+ import java.util.StringTokenizer;
  import java.util.Vector;
  import java.util.jar.Attributes;
*************** public class URLClassLoader extends Secu
*** 295,298 ****
--- 297,305 ----
      final URL baseJarURL; // Base jar: url for all resources loaded from jar
  
+     String classPath;	// The "Class-Path" attribute of this Jar's manifest
+     private Vector extensionURLLoaders; // The corresponding
+ 					// URLLoaders for each URL in
+ 					// the "Class-Path" attribute.
+ 
      SoURLLoader soURLLoader;
  
*************** public class URLClassLoader extends Secu
*** 310,313 ****
--- 317,321 ----
  
        this.soURLLoader = null;
+       this.classPath = null;
        URL baseJarURL = null;
        JarFile jarfile = null;
*************** public class URLClassLoader extends Secu
*** 338,342 ****
  		    }
  		}
! 	    }
  	}
        catch (IOException ioe)
--- 346,357 ----
  		    }
  		}
! 	      Manifest manifest = jarfile.getManifest();
! 	      if (manifest != null)
! 		{
! 		  Attributes attributes = manifest.getMainAttributes();
! 		  if (attributes != null)
! 		    this.classPath = attributes.getValue(Attributes.Name.CLASS_PATH);
! 		}
!  	    }
  	}
        catch (IOException ioe)
*************** public class URLClassLoader extends Secu
*** 344,348 ****
  	  /* ignored */
          }
! 
        this.baseJarURL = baseJarURL;
        this.jarfile = jarfile;
--- 359,363 ----
  	  /* ignored */
          }
!       
        this.baseJarURL = baseJarURL;
        this.jarfile = jarfile;
*************** public class URLClassLoader extends Secu
*** 368,373 ****
        if (je != null)
  	return new JarURLResource(this, name, je);
!       else
! 	return null;
      }
  
--- 383,406 ----
        if (je != null)
  	return new JarURLResource(this, name, je);
! 
!       // We've failed to find the resource in this Jarfile, so look in
!       // the "Class-Path" attribute.
!       if (classPath != null)
! 	{
! 	  Vector loaders = getExtensionURLLoaders();
! 	  if (loaders != null)
! 	    {
! 	      Iterator elements = loaders.iterator();
! 	      while (elements.hasNext())
! 		{
! 		  URLLoader loader = (URLLoader)elements.next();
! 		  Resource resource = loader.getResource(name);
! 		  if (resource != null)
! 		    return resource;
! 		}
! 	    }
! 	}
! 
!       return null;
      }
  
*************** public class URLClassLoader extends Secu
*** 388,391 ****
--- 421,457 ----
          }
      }
+ 
+     /**
+      * Scan our the "Class_Path" attribute in our manifest and create
+      * a Vector of URLLoaders for everything mentioned therein.  These
+      * URLLoaders are all resolved relative to this JARfile.
+      */
+     private synchronized Vector getExtensionURLLoaders()
+     {
+       if (extensionURLLoaders == null)
+ 	{
+ 	  extensionURLLoaders = new Vector();
+ 
+ 	  StringTokenizer st
+ 	    = new StringTokenizer (classPath,
+ 				   System.getProperty ("path.separator", ":"));
+       
+ 	  while (st.hasMoreElements ()) 
+ 	    {  
+ 	      String e = st.nextToken ();
+ 	      try
+ 		{
+ 		  URL url = new URL(baseURL, e);
+ 		  extensionURLLoaders.add(getURLLoader(classloader, url));
+ 		} 
+ 	      catch (java.net.MalformedURLException xx)
+ 		{
+ 		  // Give up
+ 		}
+ 	    }
+ 	}
+ 
+       return extensionURLLoaders;
+     }
    }
  
*************** public class URLClassLoader extends Secu
*** 782,785 ****
--- 848,870 ----
    }
  
+   private static URLLoader getURLLoader(URLClassLoader self, URL newUrl)
+   {
+     String file = newUrl.getFile();
+     String protocol = newUrl.getProtocol();
+     URLLoader loader;
+ 
+     // Check that it is not a directory
+     if ("gcjlib".equals(protocol))
+       loader = new SoURLLoader(self, newUrl);
+     else if (! (file.endsWith("/") || file.endsWith(File.separator)))
+       loader = new JarURLLoader(self, newUrl);
+     else if ("file".equals(protocol))
+       loader = new FileURLLoader(self, newUrl);
+     else
+       loader = new RemoteURLLoader(self, newUrl);
+     
+     return loader;
+   }
+     
    private void addURLImpl(URL newUrl)
    {
*************** public class URLClassLoader extends Secu
*** 794,809 ****
  	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);
--- 879,883 ----
  	if (loader == null)
  	  {
! 	    loader = getURLLoader(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]