-/* Copyright (C) 2005 Free Software Foundation
+/* Copyright (C) 2005, 2007 Free Software Foundation
This file is part of libgcj.
package gnu.gcj.runtime;
+import gnu.java.net.protocol.core.Handler;
import java.io.File;
-import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
-import java.util.HashSet;
import java.util.StringTokenizer;
+import java.util.Vector;
/**
* This is a helper for the bootstrap class loader. It is a
* However, it is never called the way that an ordinary ClassLoader is
* called. For instance, loadClass() is never used.
*/
-public final class BootClassLoader extends URLClassLoader
+public final class BootClassLoader extends HelperClassLoader
{
+ // This forces the core URL handler to be included in statically
+ // linked executables. The line that adds core:/ to the search
+ // path fails otherwise.
+ static Class coreHandler = gnu.java.net.protocol.core.Handler.class;
+
+ private boolean initialized;
+ private URLClassLoader bootURLLoader;
+
BootClassLoader(String libdir)
{
- super(new URL[0]);
+ // The BootClassLoader is the top of the delegation chain. It does not
+ // have a parent.
+ super((ClassLoader) null);
+ addDirectoriesFromProperty("java.endorsed.dirs");
+ addDirectoriesFromProperty("gnu.gcj.runtime.endorsed.dirs");
- // Add the contents of the endorsed directories.
- StringTokenizer st
- = new StringTokenizer (System.getProperty ("java.endorsed.dirs", ""),
- File.pathSeparator);
try
{
- while (st.hasMoreElements ())
- {
- String dirname = st.nextToken ();
- File dir = new File (dirname);
- if (dir.exists ())
- {
- if (! dirname.endsWith (File.separator))
- dirname = dirname + File.separator;
- String files[] = dir.list (new FilenameFilter ()
- {
- public boolean accept (File dir, String name)
- {
- return name.endsWith (".jar") || name.endsWith (".zip");
- }
- });
- for (int i = files.length - 1; i >= 0; i--)
- addURL(new URL("file", "", -1, dirname + files[i]));
- }
- }
-
- String w3clib = (libdir + File.separator
- + System.mapLibraryName ("w3c-gcj"));
- addURL(new URL("gcjlib", "", -1, w3clib));
- String saxlib = (libdir + File.separator
- + System.mapLibraryName ("sax-gcj"));
- addURL(new URL("gcjlib", "", -1, saxlib));
+ // Add core:/ to the end so any resources compiled into this
+ // executable may be found.
+ addURL(new URL("core", "", -1, "/"));
}
catch (java.net.MalformedURLException x)
{
return c;
}
+ // Parse the boot classpath and create a URLClassLoader that loads
+ // resources from it. This is provided for the benefit of code that
+ // does things like
+ // ClassLoader.getResourceAsStream("java/lang/Object.class")
+ private synchronized URLClassLoader getBootURLLoader()
+ {
+ if (initialized)
+ return bootURLLoader;
+ initialized = true;
+
+ Vector<URL> urls = new Vector<URL>();
+ String bootClasspath = System.getProperty ("sun.boot.class.path");
+ StringTokenizer st =
+ new StringTokenizer(bootClasspath, File.pathSeparator);
+ while (st.hasMoreTokens())
+ {
+ try
+ {
+ urls.add(new File(st.nextToken()).toURL());
+ }
+ catch (java.net.MalformedURLException e)
+ {
+ }
+ }
+
+ if (urls.size() > 0)
+ bootURLLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
+ return bootURLLoader;
+ }
+
public URL bootGetResource(String name)
{
- return findResource(name);
+ URL url = findResource(name);
+ if (url != null)
+ return url;
+
+ URLClassLoader loader = getBootURLLoader();
+ if (loader != null)
+ url = loader.findResource(name);
+
+ return url;
}
public Enumeration bootGetResources(String name) throws IOException
{
- return findResources(name);
+ URLClassLoader loader = getBootURLLoader();
+ Enumeration[] e =
+ {
+ findResources(name),
+ (loader != null) ? loader.findResources(name) : null
+ };
+
+ Vector v = new Vector();
+ for (Enumeration en : e)
+ if (en != null)
+ while (en.hasMoreElements())
+ v.add(en.nextElement());
+
+ return v.elements();
}
}