natVMClassLoader.cc (getSystemClassLoaderInternal): Updated for name change.
[gcc.git] / libjava / java / lang / VMClassLoader.java
1 /* VMClassLoader.java -- Reference implementation of native interface
2 required by ClassLoader
3 Copyright (C) 1998, 2001, 2002, 2003, 2004, 2005 Free Software Foundation
4
5 This file is part of GNU Classpath.
6
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA.
21
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
26
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
38
39 package java.lang;
40
41 import gnu.java.util.EmptyEnumeration;
42 import java.lang.reflect.Constructor;
43 import java.io.File;
44 import java.io.IOException;
45 import java.net.URL;
46 import java.net.URLClassLoader;
47 import java.security.AllPermission;
48 import java.security.Permission;
49 import java.security.Permissions;
50 import java.security.ProtectionDomain;
51 import java.util.ArrayList;
52 import java.util.Enumeration;
53 import java.util.HashMap;
54 import java.util.HashSet;
55 import java.util.Map;
56 import java.util.StringTokenizer;
57 import gnu.gcj.runtime.BootClassLoader;
58
59 /**
60 * java.lang.VMClassLoader is a package-private helper for VMs to implement
61 * on behalf of java.lang.ClassLoader.
62 *
63 * @author John Keiser
64 * @author Mark Wielaard <mark@klomp.org>
65 * @author Eric Blake <ebb9@email.byu.edu>
66 */
67 final class VMClassLoader
68 {
69 // Protection Domain definitions
70 // FIXME: should there be a special protection domain used for native code?
71
72 // The permission required to check what a classes protection domain is.
73 static final Permission protectionDomainPermission
74 = new RuntimePermission("getProtectionDomain");
75 // The protection domain returned if we cannot determine it.
76 static ProtectionDomain unknownProtectionDomain;
77
78 static
79 {
80 Permissions permissions = new Permissions();
81 permissions.add(new AllPermission());
82 unknownProtectionDomain = new ProtectionDomain(null, permissions);
83 }
84
85 static final HashMap definedPackages = new HashMap();
86
87 // This is a helper for handling java.endorsed.dirs. It is null
88 // until we've initialized the system, at which point it is created.
89 static BootClassLoader bootLoader;
90
91 // This keeps track of shared libraries we've already tried to load.
92 private static HashSet tried_libraries;
93
94 // Holds one of the LIB_* constants; used to determine how shared
95 // library loads are done.
96 private static int lib_control;
97
98 private static final int LIB_FULL = 0;
99 private static final int LIB_CACHE = 1;
100 private static final int LIB_NEVER = 2;
101
102 /**
103 * Helper to define a class using a string of bytes. This assumes that
104 * the security checks have already been performed, if necessary.
105 *
106 * <strong>For backward compatibility, this just ignores the protection
107 * domain; that is the wrong behavior, and you should directly implement
108 * this method natively if you can.</strong>
109 *
110 * @param name the name to give the class, or null if unknown
111 * @param data the data representing the classfile, in classfile format
112 * @param offset the offset into the data where the classfile starts
113 * @param len the length of the classfile data in the array
114 * @param pd the protection domain
115 * @return the class that was defined
116 * @throws ClassFormatError if data is not in proper classfile format
117 */
118 static final native Class defineClass(ClassLoader cl, String name,
119 byte[] data, int offset, int len,
120 ProtectionDomain pd)
121 throws ClassFormatError;
122
123 /**
124 * Helper to resolve all references to other classes from this class.
125 *
126 * @param c the class to resolve
127 */
128 static final native void resolveClass(Class clazz);
129
130 static final void transformException(Class clazz, Throwable x)
131 {
132 LinkageError e;
133 if (x instanceof LinkageError)
134 e = (LinkageError) x;
135 else if (x instanceof ClassNotFoundException)
136 {
137 e = new NoClassDefFoundError("while resolving class: "
138 + clazz.getName());
139 e.initCause (x);
140 }
141 else
142 {
143 e = new LinkageError ("unexpected exception during linking: "
144 + clazz.getName());
145 e.initCause (x);
146 }
147 throw e;
148 }
149
150 /**
151 * Helper to load a class from the bootstrap class loader.
152 *
153 * @param name the class name to load
154 * @param resolve whether to resolve it
155 * @return the class, loaded by the bootstrap classloader or null
156 * if the class wasn't found. Returning null is equivalent to throwing
157 * a ClassNotFoundException (but a possible performance optimization).
158 */
159 static final native Class loadClass(String name, boolean resolve)
160 throws ClassNotFoundException;
161
162 /**
163 * Helper to load a resource from the bootstrap class loader.
164 *
165 * In libgcj, this does nothing, as the default system loader knows
166 * how to find resources that have been linked in.
167 *
168 * @param name the resource to find
169 * @return the URL to the resource
170 */
171 static URL getResource(String name)
172 {
173 if (bootLoader != null)
174 return bootLoader.bootGetResource(name);
175 return null;
176 }
177
178 /**
179 * Helper to get a list of resources from the bootstrap class loader.
180 *
181 * In libgcj, this does nothing, as the default system loader knows
182 * how to find resources that have been linked in.
183 *
184 * @param name the resource to find
185 * @return an enumeration of resources
186 * @throws IOException if one occurs
187 */
188 static Enumeration getResources(String name) throws IOException
189 {
190 if (bootLoader != null)
191 return bootLoader.bootGetResources(name);
192 return EmptyEnumeration.getInstance();
193 }
194
195 /**
196 * Helper to get a package from the bootstrap class loader. The default
197 * implementation of returning null may be adequate, or you may decide
198 * that this needs some native help.
199 *
200 * @param name the name to find
201 * @return the named package, if it exists
202 */
203 static synchronized Package getPackage(String name)
204 {
205 return (Package) definedPackages.get(name);
206 }
207
208 /**
209 * Helper to get all packages from the bootstrap class loader. The default
210 * implementation of returning an empty array may be adequate, or you may
211 * decide that this needs some native help.
212 *
213 * @return all named packages, if any exist
214 */
215 static synchronized Package[] getPackages()
216 {
217 Package[] packages = new Package[definedPackages.size()];
218 return (Package[]) definedPackages.values().toArray(packages);
219 }
220
221 // Define a package for something loaded natively.
222 static synchronized void definePackageForNative(String className)
223 {
224 int lastDot = className.lastIndexOf('.');
225 if (lastDot != -1)
226 {
227 String packageName = className.substring(0, lastDot);
228 if (getPackage(packageName) == null)
229 {
230 // FIXME: this assumes we're defining the core, which
231 // isn't necessarily so. We could detect this and set up
232 // appropriately. We could also look at a manifest file
233 // compiled into the .so.
234 Package p = new Package(packageName,
235 "Java Platform API Specification",
236 "GNU", "1.4", "gcj", "GNU",
237 null, // FIXME: gcj version.
238 null);
239 definedPackages.put(packageName, p);
240 }
241 }
242 }
243
244 /**
245 * Helper for java.lang.Integer, Byte, etc to get the TYPE class
246 * at initialization time. The type code is one of the chars that
247 * represents the primitive type as in JNI.
248 *
249 * <ul>
250 * <li>'Z' - boolean</li>
251 * <li>'B' - byte</li>
252 * <li>'C' - char</li>
253 * <li>'D' - double</li>
254 * <li>'F' - float</li>
255 * <li>'I' - int</li>
256 * <li>'J' - long</li>
257 * <li>'S' - short</li>
258 * <li>'V' - void</li>
259 * </ul>
260 *
261 * @param type the primitive type
262 * @return a "bogus" class representing the primitive type
263 */
264 static final native Class getPrimitiveClass(char type);
265
266 /**
267 * The system default for assertion status. This is used for all system
268 * classes (those with a null ClassLoader), as well as the initial value for
269 * every ClassLoader's default assertion status.
270 *
271 * XXX - Not implemented yet; this requires native help.
272 *
273 * @return the system-wide default assertion status
274 */
275 static final boolean defaultAssertionStatus()
276 {
277 return true;
278 }
279
280 /**
281 * The system default for package assertion status. This is used for all
282 * ClassLoader's packageAssertionStatus defaults. It must be a map of
283 * package names to Boolean.TRUE or Boolean.FALSE, with the unnamed package
284 * represented as a null key.
285 *
286 * XXX - Not implemented yet; this requires native help.
287 *
288 * @return a (read-only) map for the default packageAssertionStatus
289 */
290 static final Map packageAssertionStatus()
291 {
292 return new HashMap();
293 }
294
295 /**
296 * The system default for class assertion status. This is used for all
297 * ClassLoader's classAssertionStatus defaults. It must be a map of
298 * class names to Boolean.TRUE or Boolean.FALSE
299 *
300 * XXX - Not implemented yet; this requires native help.
301 *
302 * @return a (read-only) map for the default classAssertionStatus
303 */
304 static final Map classAssertionStatus()
305 {
306 return new HashMap();
307 }
308
309 static native ClassLoader getSystemClassLoaderInternal();
310
311 static native void initBootLoader(String libdir);
312
313 static void initialize(String libdir)
314 {
315 initBootLoader(libdir);
316
317 String p
318 = System.getProperty ("gnu.gcj.runtime.VMClassLoader.library_control",
319 "");
320 if ("never".equals(p))
321 lib_control = LIB_NEVER;
322 else if ("cache".equals(p))
323 lib_control = LIB_CACHE;
324 else if ("full".equals(p))
325 lib_control = LIB_FULL;
326 else
327 lib_control = LIB_CACHE;
328
329 tried_libraries = new HashSet();
330 }
331
332 /**
333 * Possibly load a .so and search it for classes.
334 */
335 static native Class nativeFindClass(String name);
336
337 static ClassLoader getSystemClassLoader()
338 {
339 // This method is called as the initialization of systemClassLoader,
340 // so if there is a null value, this is the first call and we must check
341 // for java.system.class.loader.
342 String loader = System.getProperty("java.system.class.loader");
343 ClassLoader default_sys = getSystemClassLoaderInternal();
344 if (loader != null)
345 {
346 try
347 {
348 Class load_class = Class.forName(loader, true, default_sys);
349 Constructor c
350 = load_class.getConstructor(new Class[] { ClassLoader.class });
351 default_sys
352 = (ClassLoader) c.newInstance(new Object[] { default_sys });
353 }
354 catch (Exception ex)
355 {
356 throw new Error("Failed to load requested system classloader "
357 + loader, ex);
358 }
359 }
360
361 return default_sys;
362 }
363 }