1 /* Copyright (C) 2005, 2007 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 package gnu.gcj.runtime;
11 import gnu.java.net.protocol.core.Handler;
13 import java.io.IOException;
15 import java.net.URLClassLoader;
16 import java.util.Enumeration;
17 import java.util.StringTokenizer;
18 import java.util.Vector;
21 * This is a helper for the bootstrap class loader. It is a
22 * URLClassLoader so that we can read a class path and re-use all the
23 * existing code for finding classes, extracting them from jars, etc.
24 * However, it is never called the way that an ordinary ClassLoader is
25 * called. For instance, loadClass() is never used.
27 public final class BootClassLoader extends HelperClassLoader
29 // This forces the core URL handler to be included in statically
30 // linked executables. The line that adds core:/ to the search
31 // path fails otherwise.
32 static Class coreHandler = gnu.java.net.protocol.core.Handler.class;
34 private boolean initialized;
35 private URLClassLoader bootURLLoader;
37 BootClassLoader(String libdir)
39 // The BootClassLoader is the top of the delegation chain. It does not
41 super((ClassLoader) null);
42 addDirectoriesFromProperty("java.endorsed.dirs");
43 addDirectoriesFromProperty("gnu.gcj.runtime.endorsed.dirs");
47 // Add core:/ to the end so any resources compiled into this
48 // executable may be found.
49 addURL(new URL("core", "", -1, "/"));
51 catch (java.net.MalformedURLException x)
53 // This should never happen.
54 throw new RuntimeException(x);
58 public Class bootLoadClass(String name)
59 throws ClassNotFoundException
61 Class c = findLoadedClass(name);
66 // We could hack URLClassLoader to make this more
67 // efficient, if it mattered.
70 catch (ClassNotFoundException _)
78 // Parse the boot classpath and create a URLClassLoader that loads
79 // resources from it. This is provided for the benefit of code that
81 // ClassLoader.getResourceAsStream("java/lang/Object.class")
82 private synchronized URLClassLoader getBootURLLoader()
88 Vector<URL> urls = new Vector<URL>();
89 String bootClasspath = System.getProperty ("sun.boot.class.path");
91 new StringTokenizer(bootClasspath, File.pathSeparator);
92 while (st.hasMoreTokens())
96 urls.add(new File(st.nextToken()).toURL());
98 catch (java.net.MalformedURLException e)
104 bootURLLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
105 return bootURLLoader;
108 public URL bootGetResource(String name)
110 URL url = findResource(name);
114 URLClassLoader loader = getBootURLLoader();
116 url = loader.findResource(name);
121 public Enumeration bootGetResources(String name) throws IOException
123 URLClassLoader loader = getBootURLLoader();
127 (loader != null) ? loader.findResources(name) : null
130 Vector v = new Vector();
131 for (Enumeration en : e)
133 while (en.hasMoreElements())
134 v.add(en.nextElement());