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