Source for gnu.gcj.runtime.BootClassLoader

   1: /* Copyright (C) 2005, 2007  Free Software Foundation
   2: 
   3:    This file is part of libgcj.
   4: 
   5: This software is copyrighted work licensed under the terms of the
   6: Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
   7: details.  */
   8: 
   9: package gnu.gcj.runtime;
  10: 
  11: import gnu.java.net.protocol.core.Handler;
  12: import java.io.File;
  13: import java.io.IOException;
  14: import java.net.URL;
  15: import java.net.URLClassLoader;
  16: import java.util.Enumeration;
  17: import java.util.StringTokenizer;
  18: import java.util.Vector;
  19: 
  20: /**
  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.
  26:  */
  27: public final class BootClassLoader extends HelperClassLoader
  28: {
  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;
  33: 
  34:   private boolean initialized;
  35:   private URLClassLoader bootURLLoader;
  36: 
  37:   BootClassLoader(String libdir)
  38:   {
  39:     // The BootClassLoader is the top of the delegation chain. It does not
  40:     // have a parent.
  41:     super((ClassLoader) null);
  42:     addDirectoriesFromProperty("java.endorsed.dirs");
  43:     addDirectoriesFromProperty("gnu.gcj.runtime.endorsed.dirs");
  44: 
  45:     try
  46:       {
  47:     // Add core:/ to the end so any resources compiled into this
  48:     // executable may be found.
  49:     addURL(new URL("core", "", -1, "/"));
  50:       }
  51:     catch (java.net.MalformedURLException x)
  52:       {
  53:     // This should never happen.
  54:     throw new RuntimeException(x);
  55:       }
  56:   }
  57: 
  58:   public Class bootLoadClass(String name)
  59:     throws ClassNotFoundException
  60:   {
  61:     Class c = findLoadedClass(name);
  62:     if (c == null)
  63:       {
  64:     try
  65:       {
  66:         // We could hack URLClassLoader to make this more
  67:         // efficient, if it mattered.
  68:         c = findClass(name);
  69:       }
  70:     catch (ClassNotFoundException _)
  71:       {
  72:         c = null;
  73:       }
  74:       }
  75:     return c;
  76:   }
  77: 
  78:   // Parse the boot classpath and create a URLClassLoader that loads
  79:   // resources from it.  This is provided for the benefit of code that
  80:   // does things like
  81:   //   ClassLoader.getResourceAsStream("java/lang/Object.class")
  82:   private synchronized URLClassLoader getBootURLLoader()
  83:   {
  84:     if (initialized)
  85:       return bootURLLoader;
  86:     initialized = true;
  87: 
  88:     Vector<URL> urls = new Vector<URL>();
  89:     String bootClasspath = System.getProperty ("sun.boot.class.path");
  90:     StringTokenizer st =
  91:       new StringTokenizer(bootClasspath, File.pathSeparator);
  92:     while (st.hasMoreTokens())
  93:       {
  94:     try
  95:       {
  96:         urls.add(new File(st.nextToken()).toURL());
  97:       }
  98:     catch (java.net.MalformedURLException e)
  99:       {
 100:       }
 101:       }
 102: 
 103:     if (urls.size() > 0)
 104:       bootURLLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
 105:     return bootURLLoader;
 106:   }
 107: 
 108:   public URL bootGetResource(String name)
 109:   {
 110:     URL url = findResource(name);
 111:     if (url != null)
 112:       return url;
 113: 
 114:     URLClassLoader loader = getBootURLLoader();
 115:     if (loader != null)
 116:       url = loader.findResource(name);
 117: 
 118:     return url;
 119:   }
 120: 
 121:   public Enumeration bootGetResources(String name) throws IOException
 122:   {
 123:     URLClassLoader loader = getBootURLLoader();
 124:     Enumeration[] e =
 125:       {
 126:     findResources(name),
 127:     (loader != null) ? loader.findResources(name) : null
 128:       };
 129: 
 130:     Vector v = new Vector();
 131:     for (Enumeration en : e)
 132:       if (en != null)
 133:     while (en.hasMoreElements())
 134:       v.add(en.nextElement());
 135: 
 136:     return v.elements();
 137:   }
 138: }