OSDN Git Service

1fc7081bb784dea875d45d0def53063ffcbb5ce3
[pf3gnuchains/gcc-fork.git] / libjava / gnu / gcj / runtime / VMClassLoader.java
1 /* Copyright (C) 1999, 2001, 2002, 2003, 2004  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 /* Author: Kresten Krab Thorup <krab@gnu.org>  */
10
11 package gnu.gcj.runtime;
12
13 import java.io.*;
14 import java.util.StringTokenizer;
15 import java.util.HashSet;
16 import java.net.URL;
17
18 public final class VMClassLoader extends java.net.URLClassLoader
19 {
20   private VMClassLoader ()
21   {     
22     super (new URL[0]);
23     String p
24       = System.getProperty ("gnu.gcj.runtime.VMClassLoader.library_control",
25                             "");
26     if ("never".equals(p))
27       lib_control = LIB_NEVER;
28     else if ("cache".equals(p))
29       lib_control = LIB_CACHE;
30     else if ("full".equals(p))
31       {
32         // In case we ever want to change the default.
33         lib_control = LIB_FULL;
34       }
35     else
36       lib_control = LIB_FULL;
37   }
38
39   private void init() 
40   {
41     StringTokenizer st
42         = new StringTokenizer (System.getProperty ("java.class.path", "."),
43                                System.getProperty ("path.separator", ":"));
44
45     while (st.hasMoreElements ()) 
46       {  
47         String e = st.nextToken ();
48         try
49           {
50             File path = new File(e);
51             // Ignore invalid paths.
52             if (!path.exists())
53               continue;
54             if (!e.endsWith (File.separator) && path.isDirectory ())
55               addURL(new URL("file", "", -1, e + File.separator));
56             else
57               addURL(new URL("file", "", -1, e));
58           } 
59         catch (java.net.MalformedURLException x)
60           {
61             // This should never happen.
62             throw new RuntimeException(x);
63           }
64       }
65
66     // Add the contents of the extensions directories.  
67     st = new StringTokenizer (System.getProperty ("java.ext.dirs"),
68                               System.getProperty ("path.separator", ":"));
69
70     try
71       {
72         while (st.hasMoreElements ())
73           {
74             String dirname = st.nextToken ();
75             File dir = new File (dirname);
76             if (dir.exists ())
77             {
78               if (! dirname.endsWith (File.separator))
79                   dirname = dirname + File.separator;
80               String files[] 
81                 = dir.list (new FilenameFilter ()
82                             { 
83                               public boolean accept (File dir, String name)
84                               {
85                                 return (name.endsWith (".jar") 
86                                         || name.endsWith (".zip"));
87                               }
88                             });
89               for (int i = files.length - 1; i >= 0; i--)
90                 addURL(new URL("file", "", -1, dirname + files[i]));
91             }
92           }
93
94         // Add core:/ to the end of the java.class.path so any resources
95         // compiled into this executable may be found.
96         addURL(new URL("core", "", -1, "/"));
97       }
98     catch (java.net.MalformedURLException x)
99       {
100         // This should never happen.
101         throw new RuntimeException(x);
102       }
103   }
104
105   /** This is overridden to search the internal hash table, which 
106    * will only search existing linked-in classes.   This will make
107    * the default implementation of loadClass (in ClassLoader) work right.
108    * The implementation of this method is in java/lang/natClassLoader.cc.
109    */
110   protected native Class findClass(String name) 
111     throws java.lang.ClassNotFoundException;
112
113   // This can be package-private because we only call it from native
114   // code during startup.
115   static void initialize ()
116   {
117     instance.init();
118   }
119
120   // This keeps track of shared libraries we've already tried to load.
121   private HashSet tried_libraries = new HashSet();
122
123   // Holds one of the LIB_* constants; used to determine how shared
124   // library loads are done.
125   private int lib_control;
126
127   // The only VMClassLoader that can exist.
128   static VMClassLoader instance = new VMClassLoader();
129
130   private static final int LIB_FULL = 0;
131   private static final int LIB_CACHE = 1;
132   private static final int LIB_NEVER = 2;
133 }