| 1 | // ClassLoader.java - Define policies for loading Java classes.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
|
|---|
| 4 |
|
|---|
| 5 | This file is part of libgcj.
|
|---|
| 6 |
|
|---|
| 7 | This software is copyrighted work licensed under the terms of the
|
|---|
| 8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 9 | details. */
|
|---|
| 10 |
|
|---|
| 11 | package java.lang;
|
|---|
| 12 |
|
|---|
| 13 | import java.io.InputStream;
|
|---|
| 14 | import java.io.IOException;
|
|---|
| 15 | import java.net.URL;
|
|---|
| 16 | import java.net.URLConnection;
|
|---|
| 17 | import java.security.AllPermission;
|
|---|
| 18 | import java.security.CodeSource;
|
|---|
| 19 | import java.security.Permission;
|
|---|
| 20 | import java.security.Permissions;
|
|---|
| 21 | import java.security.Policy;
|
|---|
| 22 | import java.security.ProtectionDomain;
|
|---|
| 23 | import java.util.Enumeration;
|
|---|
| 24 | import java.util.HashMap;
|
|---|
| 25 | import java.util.Stack;
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * The class <code>ClassLoader</code> is intended to be subclassed by
|
|---|
| 29 | * applications in order to describe new ways of loading classes,
|
|---|
| 30 | * such as over the network.
|
|---|
| 31 | *
|
|---|
| 32 | * @author Kresten Krab Thorup
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | public abstract class ClassLoader
|
|---|
| 36 | {
|
|---|
| 37 | private ClassLoader parent;
|
|---|
| 38 | private HashMap definedPackages = new HashMap();
|
|---|
| 39 |
|
|---|
| 40 | public final ClassLoader getParent ()
|
|---|
| 41 | {
|
|---|
| 42 | /* FIXME: security */
|
|---|
| 43 | return parent;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | public static ClassLoader getSystemClassLoader ()
|
|---|
| 47 | {
|
|---|
| 48 | return gnu.gcj.runtime.VMClassLoader.instance;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * Creates a <code>ClassLoader</code> with no parent.
|
|---|
| 53 | * @exception java.lang.SecurityException if not allowed
|
|---|
| 54 | */
|
|---|
| 55 | protected ClassLoader()
|
|---|
| 56 | {
|
|---|
| 57 | this (null);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Creates a <code>ClassLoader</code> with the given parent.
|
|---|
| 62 | * The parent may be <code>null</code>.
|
|---|
| 63 | * The only thing this
|
|---|
| 64 | * constructor does, is to call
|
|---|
| 65 | * <code>checkCreateClassLoader</code> on the current
|
|---|
| 66 | * security manager.
|
|---|
| 67 | * @exception java.lang.SecurityException if not allowed
|
|---|
| 68 | * @since 1.2
|
|---|
| 69 | */
|
|---|
| 70 | protected ClassLoader(ClassLoader parent)
|
|---|
| 71 | {
|
|---|
| 72 | SecurityManager security = System.getSecurityManager ();
|
|---|
| 73 | if (security != null)
|
|---|
| 74 | security.checkCreateClassLoader ();
|
|---|
| 75 | this.parent = parent;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * Loads and link the class by the given name.
|
|---|
| 80 | * @param name the name of the class.
|
|---|
| 81 | * @return the class loaded.
|
|---|
| 82 | * @see ClassLoader#loadClass(String,boolean)
|
|---|
| 83 | * @exception java.lang.ClassNotFoundException
|
|---|
| 84 | */
|
|---|
| 85 | public Class loadClass(String name)
|
|---|
| 86 | throws java.lang.ClassNotFoundException
|
|---|
| 87 | {
|
|---|
| 88 | return loadClass (name, false);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * Loads the class by the given name. The default implementation
|
|---|
| 93 | * will search for the class in the following order (similar to jdk 1.2)
|
|---|
| 94 | * <ul>
|
|---|
| 95 | * <li> First <code>findLoadedClass</code>.
|
|---|
| 96 | * <li> If parent is non-null, <code>parent.loadClass</code>;
|
|---|
| 97 | * otherwise <code>findSystemClass</code>.
|
|---|
| 98 | * <li> <code>findClass</code>.
|
|---|
| 99 | * </ul>
|
|---|
| 100 | * If <code>link</code> is true, <code>resolveClass</code> is then
|
|---|
| 101 | * called. <p> Normally, this need not be overridden; override
|
|---|
| 102 | * <code>findClass</code> instead.
|
|---|
| 103 | * @param name the name of the class.
|
|---|
| 104 | * @param link if the class should be linked.
|
|---|
| 105 | * @return the class loaded.
|
|---|
| 106 | * @exception java.lang.ClassNotFoundException
|
|---|
| 107 | * @deprecated
|
|---|
| 108 | */
|
|---|
| 109 | protected Class loadClass(String name, boolean link)
|
|---|
| 110 | throws java.lang.ClassNotFoundException
|
|---|
| 111 | {
|
|---|
| 112 | Class c = findLoadedClass (name);
|
|---|
| 113 |
|
|---|
| 114 | if (c == null)
|
|---|
| 115 | {
|
|---|
| 116 | try {
|
|---|
| 117 | if (parent != null)
|
|---|
| 118 | return parent.loadClass (name, link);
|
|---|
| 119 | else
|
|---|
| 120 | c = gnu.gcj.runtime.VMClassLoader.instance.findClass (name);
|
|---|
| 121 | } catch (ClassNotFoundException ex) {
|
|---|
| 122 | /* ignore, we'll try findClass */;
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | if (c == null)
|
|---|
| 127 | c = findClass (name);
|
|---|
| 128 |
|
|---|
| 129 | if (c == null)
|
|---|
| 130 | throw new ClassNotFoundException (name);
|
|---|
| 131 |
|
|---|
| 132 | if (link)
|
|---|
| 133 | resolveClass (c);
|
|---|
| 134 |
|
|---|
| 135 | return c;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /** Find a class. This should be overridden by subclasses; the
|
|---|
| 139 | * default implementation throws ClassNotFoundException.
|
|---|
| 140 | *
|
|---|
| 141 | * @param name Name of the class to find.
|
|---|
| 142 | * @return The class found.
|
|---|
| 143 | * @exception java.lang.ClassNotFoundException
|
|---|
| 144 | * @since 1.2
|
|---|
| 145 | */
|
|---|
| 146 | protected Class findClass (String name)
|
|---|
| 147 | throws ClassNotFoundException
|
|---|
| 148 | {
|
|---|
| 149 | throw new ClassNotFoundException (name);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | // Protection Domain definitions
|
|---|
| 153 | // FIXME: should there be a special protection domain used for native code?
|
|---|
| 154 |
|
|---|
| 155 | // The permission required to check what a classes protection domain is.
|
|---|
| 156 | static final Permission protectionDomainPermission
|
|---|
| 157 | = new RuntimePermission("getProtectionDomain");
|
|---|
| 158 | // The protection domain returned if we cannot determine it.
|
|---|
| 159 | static ProtectionDomain unknownProtectionDomain;
|
|---|
| 160 | // Protection domain to use when a class is defined without one specified.
|
|---|
| 161 | static ProtectionDomain defaultProtectionDomain;
|
|---|
| 162 |
|
|---|
| 163 | static
|
|---|
| 164 | {
|
|---|
| 165 | Permissions permissions = new Permissions();
|
|---|
| 166 | permissions.add(new AllPermission());
|
|---|
| 167 | unknownProtectionDomain = new ProtectionDomain(null, permissions);
|
|---|
| 168 |
|
|---|
| 169 | CodeSource cs = new CodeSource(null, null);
|
|---|
| 170 | defaultProtectionDomain =
|
|---|
| 171 | new ProtectionDomain(cs, Policy.getPolicy().getPermissions(cs));
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /**
|
|---|
| 175 | * Defines a class, given the class-data. According to the JVM, this
|
|---|
| 176 | * method should not be used; instead use the variant of this method
|
|---|
| 177 | * in which the name of the class being defined is specified
|
|---|
| 178 | * explicitly.
|
|---|
| 179 | * <P>
|
|---|
| 180 | * If the name of the class, as specified (implicitly) in the class
|
|---|
| 181 | * data, denotes a class which has already been loaded by this class
|
|---|
| 182 | * loader, an instance of
|
|---|
| 183 | * <code>java.lang.ClassNotFoundException</code> will be thrown.
|
|---|
| 184 | *
|
|---|
| 185 | * @param data bytes in class file format.
|
|---|
| 186 | * @param off offset to start interpreting data.
|
|---|
| 187 | * @param len length of data in class file.
|
|---|
| 188 | * @return the class defined.
|
|---|
| 189 | * @exception java.lang.ClassNotFoundException
|
|---|
| 190 | * @exception java.lang.LinkageError
|
|---|
| 191 | * @see ClassLoader#defineClass(String,byte[],int,int) */
|
|---|
| 192 | protected final Class defineClass(byte[] data, int off, int len)
|
|---|
| 193 | throws ClassFormatError
|
|---|
| 194 | {
|
|---|
| 195 | return defineClass (null, data, off, len, defaultProtectionDomain);
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | protected final Class defineClass(String name, byte[] data, int off, int len)
|
|---|
| 199 | throws ClassFormatError
|
|---|
| 200 | {
|
|---|
| 201 | return defineClass (name, data, off, len, defaultProtectionDomain);
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | /**
|
|---|
| 205 | * Defines a class, given the class-data. This is preferable
|
|---|
| 206 | * over <code>defineClass(byte[],off,len)</code> since it is more
|
|---|
| 207 | * secure. If the expected name does not match that of the class
|
|---|
| 208 | * file, <code>ClassNotFoundException</code> is thrown. If
|
|---|
| 209 | * <code>name</code> denotes the name of an already loaded class, a
|
|---|
| 210 | * <code>LinkageError</code> is thrown.
|
|---|
| 211 | * <p>
|
|---|
| 212 | *
|
|---|
| 213 | * FIXME: How do we assure that the class-file data is not being
|
|---|
| 214 | * modified, simultaneously with the class loader running!? If this
|
|---|
| 215 | * was done in some very clever way, it might break security.
|
|---|
| 216 | * Right now I am thinking that defineclass should make sure never to
|
|---|
| 217 | * read an element of this array more than once, and that that would
|
|---|
| 218 | * assure the ``immutable'' appearance. It is still to be determined
|
|---|
| 219 | * if this is in fact how defineClass operates.
|
|---|
| 220 | *
|
|---|
| 221 | * @param name the expected name.
|
|---|
| 222 | * @param data bytes in class file format.
|
|---|
| 223 | * @param off offset to start interpreting data.
|
|---|
| 224 | * @param len length of data in class file.
|
|---|
| 225 | * @param protectionDomain security protection domain for the class.
|
|---|
| 226 | * @return the class defined.
|
|---|
| 227 | * @exception java.lang.ClassNotFoundException
|
|---|
| 228 | * @exception java.lang.LinkageError
|
|---|
| 229 | */
|
|---|
| 230 | protected final synchronized Class defineClass(String name,
|
|---|
| 231 | byte[] data,
|
|---|
| 232 | int off,
|
|---|
| 233 | int len,
|
|---|
| 234 | ProtectionDomain protectionDomain)
|
|---|
| 235 | throws ClassFormatError
|
|---|
| 236 | {
|
|---|
| 237 | if (data==null || data.length < off+len || off<0 || len<0)
|
|---|
| 238 | throw new ClassFormatError ("arguments to defineClass "
|
|---|
| 239 | + "are meaningless");
|
|---|
| 240 |
|
|---|
| 241 | // as per 5.3.5.1
|
|---|
| 242 | if (name != null && findLoadedClass (name) != null)
|
|---|
| 243 | throw new java.lang.LinkageError ("class "
|
|---|
| 244 | + name
|
|---|
| 245 | + " already loaded");
|
|---|
| 246 |
|
|---|
| 247 | if (protectionDomain == null)
|
|---|
| 248 | protectionDomain = defaultProtectionDomain;
|
|---|
| 249 |
|
|---|
| 250 | try {
|
|---|
| 251 | // Since we're calling into native code here,
|
|---|
| 252 | // we better make sure that any generated
|
|---|
| 253 | // exception is to spec!
|
|---|
| 254 |
|
|---|
| 255 | return defineClass0 (name, data, off, len, protectionDomain);
|
|---|
| 256 |
|
|---|
| 257 | } catch (LinkageError x) {
|
|---|
| 258 | throw x; // rethrow
|
|---|
| 259 |
|
|---|
| 260 | } catch (java.lang.VirtualMachineError x) {
|
|---|
| 261 | throw x; // rethrow
|
|---|
| 262 |
|
|---|
| 263 | } catch (java.lang.Throwable x) {
|
|---|
| 264 | // This should never happen, or we are beyond spec.
|
|---|
| 265 |
|
|---|
| 266 | throw new InternalError ("Unexpected exception "
|
|---|
| 267 | + "while defining class "
|
|---|
| 268 | + name + ": "
|
|---|
| 269 | + x.toString ());
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | /** This is the entry point of defineClass into the native code */
|
|---|
| 274 | private native Class defineClass0 (String name,
|
|---|
| 275 | byte[] data,
|
|---|
| 276 | int off,
|
|---|
| 277 | int len,
|
|---|
| 278 | ProtectionDomain protectionDomain)
|
|---|
| 279 | throws ClassFormatError;
|
|---|
| 280 |
|
|---|
| 281 | /**
|
|---|
| 282 | * Link the given class. This will bring the class to a state where
|
|---|
| 283 | * the class initializer can be run. Linking involves the following
|
|---|
| 284 | * steps:
|
|---|
| 285 | * <UL>
|
|---|
| 286 | * <LI> Prepare (allocate and internalize) the constant strings that
|
|---|
| 287 | * are used in this class.
|
|---|
| 288 | * <LI> Allocate storage for static fields, and define the layout
|
|---|
| 289 | * of instance fields.
|
|---|
| 290 | * <LI> Perform static initialization of ``static final'' int,
|
|---|
| 291 | * long, float, double and String fields for which there is a
|
|---|
| 292 | * compile-time constant initializer.
|
|---|
| 293 | * <LI> Create the internal representation of the ``vtable''.
|
|---|
| 294 | * </UL>
|
|---|
| 295 | * For <code>gcj</code>-compiled classes, only the first step is
|
|---|
| 296 | * performed. The compiler will have done the rest already.
|
|---|
| 297 | * <P>
|
|---|
| 298 | * This is called by the system automatically,
|
|---|
| 299 | * as part of class initialization; there is no reason to ever call
|
|---|
| 300 | * this method directly.
|
|---|
| 301 | * <P>
|
|---|
| 302 | * For historical reasons, this method has a name which is easily
|
|---|
| 303 | * misunderstood. Java classes are never ``resolved''. Classes are
|
|---|
| 304 | * linked; whereas method and field references are resolved.
|
|---|
| 305 | *
|
|---|
| 306 | * @param clazz the class to link.
|
|---|
| 307 | * @exception java.lang.LinkageError
|
|---|
| 308 | */
|
|---|
| 309 | protected final void resolveClass(Class clazz)
|
|---|
| 310 | {
|
|---|
| 311 | resolveClass0(clazz);
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | static void resolveClass0(Class clazz)
|
|---|
| 315 | {
|
|---|
| 316 | synchronized (clazz)
|
|---|
| 317 | {
|
|---|
| 318 | try {
|
|---|
| 319 | linkClass0 (clazz);
|
|---|
| 320 | } catch (Throwable x) {
|
|---|
| 321 | markClassErrorState0 (clazz);
|
|---|
| 322 |
|
|---|
| 323 | if (x instanceof Error)
|
|---|
| 324 | throw (Error)x;
|
|---|
| 325 | else
|
|---|
| 326 | throw new java.lang.InternalError
|
|---|
| 327 | ("unexpected exception during linking: " + x);
|
|---|
| 328 | }
|
|---|
| 329 | }
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | /** Internal method. Calls _Jv_PrepareClass and
|
|---|
| 333 | * _Jv_PrepareCompiledClass. This is only called from resolveClass. */
|
|---|
| 334 | private static native void linkClass0(Class clazz);
|
|---|
| 335 |
|
|---|
| 336 | /** Internal method. Marks the given clazz to be in an erroneous
|
|---|
| 337 | * state, and calls notifyAll() on the class object. This should only
|
|---|
| 338 | * be called when the caller has the lock on the class object. */
|
|---|
| 339 | private static native void markClassErrorState0(Class clazz);
|
|---|
| 340 |
|
|---|
| 341 | /**
|
|---|
| 342 | * Defines a new package and creates a Package object.
|
|---|
| 343 | * The package should be defined before any class in the package is
|
|---|
| 344 | * defined with <code>defineClass()</code>. The package should not yet
|
|---|
| 345 | * be defined before in this classloader or in one of its parents (which
|
|---|
| 346 | * means that <code>getPackage()</code> should return <code>null</code>).
|
|---|
| 347 | * All parameters except the <code>name</code> of the package may be
|
|---|
| 348 | * <code>null</code>.
|
|---|
| 349 | * <p>
|
|---|
| 350 | * Subclasses should call this method from their <code>findClass()</code>
|
|---|
| 351 | * implementation before calling <code>defineClass()</code> on a Class
|
|---|
| 352 | * in a not yet defined Package (which can be checked by calling
|
|---|
| 353 | * <code>getPackage()</code>).
|
|---|
| 354 | *
|
|---|
| 355 | * @param name The name of the Package
|
|---|
| 356 | * @param specTitle The name of the specification
|
|---|
| 357 | * @param specVendor The name of the specification designer
|
|---|
| 358 | * @param specVersion The version of this specification
|
|---|
| 359 | * @param implTitle The name of the implementation
|
|---|
| 360 | * @param implVendor The vendor that wrote this implementation
|
|---|
| 361 | * @param implVersion The version of this implementation
|
|---|
| 362 | * @param sealed If sealed the origin of the package classes
|
|---|
| 363 | * @return the Package object for the specified package
|
|---|
| 364 | *
|
|---|
| 365 | * @exception IllegalArgumentException if the package name is null or if
|
|---|
| 366 | * it was already defined by this classloader or one of its parents.
|
|---|
| 367 | *
|
|---|
| 368 | * @see Package
|
|---|
| 369 | * @since 1.2
|
|---|
| 370 | */
|
|---|
| 371 | protected Package definePackage(String name,
|
|---|
| 372 | String specTitle, String specVendor,
|
|---|
| 373 | String specVersion, String implTitle,
|
|---|
| 374 | String implVendor, String implVersion,
|
|---|
| 375 | URL sealed)
|
|---|
| 376 | {
|
|---|
| 377 | if (getPackage(name) != null)
|
|---|
| 378 | throw new IllegalArgumentException("Package " + name
|
|---|
| 379 | + " already defined");
|
|---|
| 380 | Package p = new Package(name,
|
|---|
| 381 | specTitle, specVendor, specVersion,
|
|---|
| 382 | implTitle, implVendor, implVersion,
|
|---|
| 383 | sealed);
|
|---|
| 384 | synchronized (definedPackages)
|
|---|
| 385 | {
|
|---|
| 386 | definedPackages.put(name, p);
|
|---|
| 387 | }
|
|---|
| 388 | return p;
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | /**
|
|---|
| 392 | * Returns the Package object for the requested package name. It returns
|
|---|
| 393 | * null when the package is not defined by this classloader or one of its
|
|---|
| 394 | * parents.
|
|---|
| 395 | *
|
|---|
| 396 | * @since 1.2
|
|---|
| 397 | */
|
|---|
| 398 | protected Package getPackage(String name)
|
|---|
| 399 | {
|
|---|
| 400 | Package p;
|
|---|
| 401 | if (parent == null)
|
|---|
| 402 | // XXX - Should we use the bootstrap classloader?
|
|---|
| 403 | p = null;
|
|---|
| 404 | else
|
|---|
| 405 | p = parent.getPackage(name);
|
|---|
| 406 |
|
|---|
| 407 | if (p == null)
|
|---|
| 408 | {
|
|---|
| 409 | synchronized (definedPackages)
|
|---|
| 410 | {
|
|---|
| 411 | p = (Package) definedPackages.get(name);
|
|---|
| 412 | }
|
|---|
| 413 | }
|
|---|
| 414 |
|
|---|
| 415 | return p;
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | /**
|
|---|
| 419 | * Returns all Package objects defined by this classloader and its parents.
|
|---|
| 420 | *
|
|---|
| 421 | * @since 1.2
|
|---|
| 422 | */
|
|---|
| 423 | protected Package[] getPackages()
|
|---|
| 424 | {
|
|---|
| 425 | Package[] allPackages;
|
|---|
| 426 |
|
|---|
| 427 | // Get all our packages.
|
|---|
| 428 | Package[] packages;
|
|---|
| 429 | synchronized(definedPackages)
|
|---|
| 430 | {
|
|---|
| 431 | packages = new Package[definedPackages.size()];
|
|---|
| 432 | definedPackages.values().toArray(packages);
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | // If we have a parent get all packages defined by our parents.
|
|---|
| 436 | if (parent != null)
|
|---|
| 437 | {
|
|---|
| 438 | Package[] parentPackages = parent.getPackages();
|
|---|
| 439 | allPackages = new Package[parentPackages.length + packages.length];
|
|---|
| 440 | System.arraycopy(parentPackages, 0, allPackages, 0,
|
|---|
| 441 | parentPackages.length);
|
|---|
| 442 | System.arraycopy(packages, 0, allPackages, parentPackages.length,
|
|---|
| 443 | packages.length);
|
|---|
| 444 | }
|
|---|
| 445 | else
|
|---|
| 446 | // XXX - Should we use the bootstrap classloader?
|
|---|
| 447 | allPackages = packages;
|
|---|
| 448 |
|
|---|
| 449 | return allPackages;
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | /**
|
|---|
| 453 | * Returns a class found in a system-specific way, typically
|
|---|
| 454 | * via the <code>java.class.path</code> system property. Loads the
|
|---|
| 455 | * class if necessary.
|
|---|
| 456 | *
|
|---|
| 457 | * @param name the class to resolve.
|
|---|
| 458 | * @return the class loaded.
|
|---|
| 459 | * @exception java.lang.LinkageError
|
|---|
| 460 | * @exception java.lang.ClassNotFoundException
|
|---|
| 461 | */
|
|---|
| 462 | protected final Class findSystemClass(String name)
|
|---|
| 463 | throws java.lang.ClassNotFoundException
|
|---|
| 464 | {
|
|---|
| 465 | return gnu.gcj.runtime.VMClassLoader.instance.loadClass (name);
|
|---|
| 466 | }
|
|---|
| 467 |
|
|---|
| 468 | /*
|
|---|
| 469 | * Does currently nothing. FIXME.
|
|---|
| 470 | */
|
|---|
| 471 | protected final void setSigners(Class claz, Object[] signers) {
|
|---|
| 472 | /* claz.setSigners (signers); */
|
|---|
| 473 | }
|
|---|
| 474 |
|
|---|
| 475 | /**
|
|---|
| 476 | * If a class named <code>name</code> was previously loaded using
|
|---|
| 477 | * this <code>ClassLoader</code>, then it is returned. Otherwise
|
|---|
| 478 | * it returns <code>null</code>. (Unlike the JDK this is native,
|
|---|
| 479 | * since we implement the class table internally.)
|
|---|
| 480 | * @param name class to find.
|
|---|
| 481 | * @return the class loaded, or null.
|
|---|
| 482 | */
|
|---|
| 483 | protected final native Class findLoadedClass(String name);
|
|---|
| 484 |
|
|---|
| 485 | public static InputStream getSystemResourceAsStream(String name) {
|
|---|
| 486 | return getSystemClassLoader().getResourceAsStream (name);
|
|---|
| 487 | }
|
|---|
| 488 |
|
|---|
| 489 | public static URL getSystemResource(String name) {
|
|---|
| 490 | return getSystemClassLoader().getResource (name);
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | /**
|
|---|
| 494 | * Return an InputStream representing the resource name.
|
|---|
| 495 | * This is essentially like
|
|---|
| 496 | * <code>getResource(name).openStream()</code>, except
|
|---|
| 497 | * it masks out any IOException and returns null on failure.
|
|---|
| 498 | * @param name resource to load
|
|---|
| 499 | * @return an InputStream, or null
|
|---|
| 500 | * @see java.lang.ClassLoader#getResource(String)
|
|---|
| 501 | * @see java.io.InputStream
|
|---|
| 502 | */
|
|---|
| 503 | public InputStream getResourceAsStream(String name)
|
|---|
| 504 | {
|
|---|
| 505 | try {
|
|---|
| 506 | URL res = getResource (name);
|
|---|
| 507 | if (res == null) return null;
|
|---|
| 508 | return res.openStream ();
|
|---|
| 509 | } catch (java.io.IOException x) {
|
|---|
| 510 | return null;
|
|---|
| 511 | }
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 | /**
|
|---|
| 515 | * Return an java.io.URL representing the resouce <code>name</code>.
|
|---|
| 516 | * The default implementation just returns <code>null</code>.
|
|---|
| 517 | * @param name resource to load
|
|---|
| 518 | * @return a URL, or null if there is no such resource.
|
|---|
| 519 | * @see java.lang.ClassLoader#getResourceAsBytes(String)
|
|---|
| 520 | * @see java.lang.ClassLoader#getResourceAsStream(String)
|
|---|
| 521 | * @see java.io.URL
|
|---|
| 522 | */
|
|---|
| 523 | public URL getResource (String name)
|
|---|
| 524 | {
|
|---|
| 525 | // The rules say search the parent class if non-null,
|
|---|
| 526 | // otherwise search the built-in class loader (assumed to be
|
|---|
| 527 | // the system ClassLoader). If not found, call
|
|---|
| 528 | // findResource().
|
|---|
| 529 | URL result = null;
|
|---|
| 530 |
|
|---|
| 531 | ClassLoader delegate = parent;
|
|---|
| 532 |
|
|---|
| 533 | if (delegate == null)
|
|---|
| 534 | delegate = getSystemClassLoader ();
|
|---|
| 535 |
|
|---|
| 536 | // Protect ourselves from looping.
|
|---|
| 537 | if (this != delegate)
|
|---|
| 538 | result = delegate.getResource (name);
|
|---|
| 539 |
|
|---|
| 540 | if (result != null)
|
|---|
| 541 | return result;
|
|---|
| 542 | else
|
|---|
| 543 | return findResource (name);
|
|---|
| 544 | }
|
|---|
| 545 |
|
|---|
| 546 | protected URL findResource (String name)
|
|---|
| 547 | {
|
|---|
| 548 | // Default to returning null. Derived classes implement this.
|
|---|
| 549 | return null;
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | public final Enumeration getResources (String name) throws IOException
|
|---|
| 553 | {
|
|---|
| 554 | // The rules say search the parent class if non-null,
|
|---|
| 555 | // otherwise search the built-in class loader (assumed to be
|
|---|
| 556 | // the system ClassLoader). If not found, call
|
|---|
| 557 | // findResource().
|
|---|
| 558 | Enumeration result = null;
|
|---|
| 559 |
|
|---|
| 560 | ClassLoader delegate = parent;
|
|---|
| 561 |
|
|---|
| 562 | if (delegate == null)
|
|---|
| 563 | delegate = getSystemClassLoader ();
|
|---|
| 564 |
|
|---|
| 565 | // Protect ourselves from looping.
|
|---|
| 566 | if (this != delegate)
|
|---|
| 567 | result = delegate.getResources (name);
|
|---|
| 568 |
|
|---|
| 569 | if (result != null)
|
|---|
| 570 | return result;
|
|---|
| 571 | else
|
|---|
| 572 | return findResources (name);
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| 575 | protected Enumeration findResources (String name) throws IOException
|
|---|
| 576 | {
|
|---|
| 577 | // Default to returning null. Derived classes implement this.
|
|---|
| 578 | return null;
|
|---|
| 579 | }
|
|---|
| 580 | }
|
|---|