Source for gnu.gcj.runtime.SystemClassLoader

   1: /* Copyright (C) 2005, 2006  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 java.io.*;
  12: import java.lang.reflect.Field;
  13: import java.util.StringTokenizer;
  14: import java.util.HashMap;
  15: import java.net.URL;
  16: import java.net.URLClassLoader;
  17: 
  18: public final class SystemClassLoader extends URLClassLoader
  19: {
  20:   SystemClassLoader(ClassLoader parent)
  21:   {
  22:     super(new URL[0], parent);
  23:   }
  24: 
  25:   // This holds all the "native" classes linked into the executable
  26:   // and registered with this loader.
  27:   private HashMap nativeClasses = new HashMap();
  28: 
  29:   // This is called to register a native class which was linked into
  30:   // the application but which is registered with the system class
  31:   // loader after the VM is initialized.
  32:   void addClass(Class klass)
  33:   {
  34:     String packageName = null;
  35:     String className = klass.getName();
  36:     int lastDot = className.lastIndexOf('.');
  37:     if (lastDot != -1)
  38:       packageName = className.substring(0, lastDot);
  39:     if (packageName != null && getPackage(packageName) == null)
  40:       {
  41:     // Should have some way to store this information in a
  42:     // precompiled manifest.
  43:     definePackage(packageName, null, null, null, null, null, null, null);
  44:       }
  45:       
  46:     // Use reflection to access the package-private "loadedClasses" field.
  47:     nativeClasses.put(className, klass);
  48:   }
  49: 
  50:   protected native Class findClass(String name);
  51: 
  52:   // We add the URLs to the system class loader late.  The reason for
  53:   // this is that during bootstrap we don't want to parse URLs or
  54:   // create URL connections, since that will result in circularities
  55:   // causing a crash.
  56:   void init()
  57:   {
  58:     String sep = File.pathSeparator;
  59:     StringTokenizer st
  60:       = new StringTokenizer (System.getProperty ("java.class.path", "."),
  61:                  sep, true);
  62:     // Pretend we start with a ':', so if we see a ':' first we add
  63:     // '.'.
  64:     boolean last_was_sep = true;
  65:     while (st.hasMoreElements ()) 
  66:       {  
  67:     String e = st.nextToken ();
  68:     try
  69:       {
  70:         if (sep.equals(e))
  71:           {
  72:         if (last_was_sep)
  73:           {
  74:             // We saw two separators in a row, so add ".".
  75:             addURL(new URL("file", "", -1, "./"));
  76:             last_was_sep = false;
  77:           }
  78:         else
  79:           last_was_sep = true;
  80:         continue;
  81:           }
  82: 
  83:         last_was_sep = false;
  84:         File path = new File(e);
  85:         // Ignore invalid paths.
  86:         if (!path.exists())
  87:           continue;
  88:         if (!e.endsWith (File.separator) && path.isDirectory ())
  89:           addURL(new URL("file", "", -1, e + File.separator));
  90:         else
  91:           addURL(new URL("file", "", -1, e));
  92:       } 
  93:     catch (java.net.MalformedURLException x)
  94:       {
  95:         // This should never happen.
  96:         throw new RuntimeException(x);
  97:       }
  98:       }
  99:     // If we saw a trailing ":", add "." to the path.
 100:     if (last_was_sep)
 101:       {
 102:     try
 103:       {
 104:         addURL(new URL("file", "", -1, "./"));
 105:       }
 106:     catch (java.net.MalformedURLException x)
 107:       {
 108:         // This should never happen.
 109:         throw new RuntimeException(x);
 110:       }
 111:       }
 112:   }
 113: }