jdk/src/share/classes/java/lang/ClassLoader.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 51 6fe31bc95bbc
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1994-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.Stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.misc.ClassFileTransformer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.misc.CompoundEnumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.misc.Resource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.misc.URLClassPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.misc.VM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import sun.reflect.Reflection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * A class loader is an object that is responsible for loading classes. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * class <tt>ClassLoader</tt> is an abstract class.  Given the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * href="#name">binary name</a> of a class, a class loader should attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * locate or generate data that constitutes a definition for the class.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * typical strategy is to transform the name into a file name and then read a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * "class file" of that name from a file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p> Every {@link Class <tt>Class</tt>} object contains a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Class#getClassLoader() reference} to the <tt>ClassLoader</tt> that defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p> <tt>Class</tt> objects for array classes are not created by class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * loaders, but are created automatically as required by the Java runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * The class loader for an array class, as returned by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Class#getClassLoader()} is the same as the class loader for its element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * type; if the element type is a primitive type, then the array class has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p> Applications implement subclasses of <tt>ClassLoader</tt> in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * extend the manner in which the Java virtual machine dynamically loads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p> Class loaders may typically be used by security managers to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * security domains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <p> The <tt>ClassLoader</tt> class uses a delegation model to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * classes and resources.  Each instance of <tt>ClassLoader</tt> has an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * associated parent class loader.  When requested to find a class or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * resource, a <tt>ClassLoader</tt> instance will delegate the search for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * class or resource to its parent class loader before attempting to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * class or resource itself.  The virtual machine's built-in class loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * called the "bootstrap class loader", does not itself have a parent but may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * serve as the parent of a <tt>ClassLoader</tt> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p> Normally, the Java virtual machine loads classes from the local file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * system in a platform-dependent manner.  For example, on UNIX systems, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * virtual machine loads classes from the directory defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <tt>CLASSPATH</tt> environment variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <p> However, some classes may not originate from a file; they may originate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * from other sources, such as the network, or they could be constructed by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * application.  The method {@link #defineClass(String, byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <tt>defineClass</tt>} converts an array of bytes into an instance of class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <tt>Class</tt>. Instances of this newly defined class can be created using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * {@link Class#newInstance <tt>Class.newInstance</tt>}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <p> The methods and constructors of objects created by a class loader may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * reference other classes.  To determine the class(es) referred to, the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * virtual machine invokes the {@link #loadClass <tt>loadClass</tt>} method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * the class loader that originally created the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <p> For example, an application could create a network class loader to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * download class files from a server.  Sample code might look like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *   ClassLoader loader&nbsp;= new NetworkClassLoader(host,&nbsp;port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *   Object main&nbsp;= loader.loadClass("Main", true).newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *       &nbsp;.&nbsp;.&nbsp;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <p> The network class loader subclass must define the methods {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * #findClass <tt>findClass</tt>} and <tt>loadClassData</tt> to load a class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * from the network.  Once it has downloaded the bytes that make up the class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * it should use the method {@link #defineClass <tt>defineClass</tt>} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * create a class instance.  A sample implementation is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *     class NetworkClassLoader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *         String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *         int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *         public Class findClass(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *             byte[] b = loadClassData(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *             return defineClass(name, b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *         private byte[] loadClassData(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *             // load the class data from the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *             &nbsp;.&nbsp;.&nbsp;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <h4> <a name="name">Binary names</a> </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * <p> Any class name provided as a {@link String} parameter to methods in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <tt>ClassLoader</tt> must be a binary name as defined by the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * href="http://java.sun.com/docs/books/jls/">Java Language Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <p> Examples of valid class names include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *   "java.lang.String"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *   "javax.swing.JSpinner$DefaultEditor"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *   "java.security.KeyStore$Builder$FileBuilder$1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *   "java.net.URLClassLoader$3$1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * @see      #resolveClass(Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
public abstract class ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private static native void registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    // If initialization succeed this is set to true and security checks will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // succeed.  Otherwise the object is not initialized and the object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    // useless.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private boolean initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // The parent class loader for delegation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private ClassLoader parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    // Hashtable that maps packages to certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private Hashtable package2certs = new Hashtable(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    // Shared among all packages with unsigned classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    java.security.cert.Certificate[] nocerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // The classes loaded by this class loader.  The only purpose of this table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // is to keep the classes from being GC'ed until the loader is GC'ed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private Vector classes = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    // The initiating protection domains for all classes loaded by this loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private Set domains = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    // Invoked by the VM to record every loaded class with this loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    void addClass(Class c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        classes.addElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    // The packages defined in this class loader.  Each package name is mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    // to its corresponding Package object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private HashMap packages = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Creates a new class loader using the specified parent class loader for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * delegation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <p> If there is a security manager, its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * SecurityManager#checkCreateClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <tt>checkCreateClassLoader</tt>} method is invoked.  This may result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * a security exception.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param  parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *         The parent class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *          If a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *          <tt>checkCreateClassLoader</tt> method doesn't allow creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *          of a new class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    protected ClassLoader(ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Creates a new class loader using the <tt>ClassLoader</tt> returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * the method {@link #getSystemClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <tt>getSystemClassLoader()</tt>} as the parent class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <p> If there is a security manager, its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * SecurityManager#checkCreateClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <tt>checkCreateClassLoader</tt>} method is invoked.  This may result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * a security exception.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *          If a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *          <tt>checkCreateClassLoader</tt> method doesn't allow creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *          of a new class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    protected ClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        this.parent = getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    // -- Class --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Loads the class with the specified <a href="#name">binary name</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * This method searches for classes in the same manner as the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * #loadClass(String, boolean)} method.  It is invoked by the Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * machine to resolve class references.  Invoking this method is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * to invoking {@link #loadClass(String, boolean) <tt>loadClass(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * false)</tt>}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @return  The resulting <tt>Class</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *          If the class was not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public Class<?> loadClass(String name) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return loadClass(name, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Loads the class with the specified <a href="#name">binary name</a>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * default implementation of this method searches for classes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <p><ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *   <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *   has already been loaded.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *   <li><p> Invoke the {@link #loadClass(String) <tt>loadClass</tt>} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *   on the parent class loader.  If the parent is <tt>null</tt> the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *   loader built-in to the virtual machine is used, instead.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *   <li><p> Invoke the {@link #findClass(String)} method to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *   class.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <p> If the class was found using the above steps, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <tt>resolve</tt> flag is true, this method will then invoke the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * #resolveClass(Class)} method on the resulting <tt>Class</tt> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <p> Subclasses of <tt>ClassLoader</tt> are encouraged to override {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * #findClass(String)}, rather than this method.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @param  resolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *         If <tt>true</tt> then resolve the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @return  The resulting <tt>Class</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *          If the class could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    protected synchronized Class<?> loadClass(String name, boolean resolve)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // First, check if the class has already been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        Class c = findLoadedClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    c = parent.loadClass(name, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    c = findBootstrapClass0(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                // If still not found, then invoke findClass in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                // to find the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                c = findClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (resolve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            resolveClass(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    // This method is invoked by the virtual machine to load a class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    private synchronized Class loadClassInternal(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return loadClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private void checkPackageAccess(Class cls, ProtectionDomain pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        final SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            final String name = cls.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            final int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        sm.checkPackageAccess(name.substring(0, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                }, new AccessControlContext(new ProtectionDomain[] {pd}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        domains.add(pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Finds the class with the specified <a href="#name">binary name</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * This method should be overridden by class loader implementations that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * follow the delegation model for loading classes, and will be invoked by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * the {@link #loadClass <tt>loadClass</tt>} method after checking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * parent class loader for the requested class.  The default implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * throws a <tt>ClassNotFoundException</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @return  The resulting <tt>Class</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *          If the class could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    protected Class<?> findClass(String name) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Converts an array of bytes into an instance of class <tt>Class</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Before the <tt>Class</tt> can be used it must be resolved.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * is deprecated in favor of the version that takes a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * href="#name">binary name</a> as its first argument, and is more secure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *         The bytes that make up the class data.  The bytes in positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *         <tt>off</tt> through <tt>off+len-1</tt> should have the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *         of a valid class file as defined by the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *         href="http://java.sun.com/docs/books/vmspec/">Java Virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *         Machine Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param  off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *         The start offset in <tt>b</tt> of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *         The length of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @return  The <tt>Class</tt> object that was created from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *          class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *          If the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *          If either <tt>off</tt> or <tt>len</tt> is negative, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *          <tt>off+len</tt> is greater than <tt>b.length</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @see  #loadClass(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @see  #resolveClass(Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @deprecated  Replaced by {@link #defineClass(String, byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * defineClass(String, byte[], int, int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    protected final Class<?> defineClass(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return defineClass(null, b, off, len, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Converts an array of bytes into an instance of class <tt>Class</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Before the <tt>Class</tt> can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * <p> This method assigns a default {@link java.security.ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * <tt>ProtectionDomain</tt>} to the newly defined class.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <tt>ProtectionDomain</tt> is effectively granted the same set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * permissions returned when {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * java.security.Policy#getPermissions(java.security.CodeSource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * <tt>Policy.getPolicy().getPermissions(new CodeSource(null, null))</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * is invoked.  The default domain is created on the first invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * {@link #defineClass(String, byte[], int, int) <tt>defineClass</tt>},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * and re-used on subsequent invocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * <p> To assign a specific <tt>ProtectionDomain</tt> to the class, use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * the {@link #defineClass(String, byte[], int, int,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * java.security.ProtectionDomain) <tt>defineClass</tt>} method that takes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * <tt>ProtectionDomain</tt> as one of its arguments.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *         The expected <a href="#name">binary name</a> of the class, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *         <tt>null</tt> if not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *         The bytes that make up the class data.  The bytes in positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *         <tt>off</tt> through <tt>off+len-1</tt> should have the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *         of a valid class file as defined by the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *         href="http://java.sun.com/docs/books/vmspec/">Java Virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *         Machine Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @param  off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *         The start offset in <tt>b</tt> of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *         The length of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @return  The <tt>Class</tt> object that was created from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *          class data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *          If the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *          If either <tt>off</tt> or <tt>len</tt> is negative, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *          <tt>off+len</tt> is greater than <tt>b.length</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *          If an attempt is made to add this class to a package that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *          contains classes that were signed by a different set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *          certificates than this class (which is unsigned), or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *          <tt>name</tt> begins with "<tt>java.</tt>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @see  #loadClass(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @see  #resolveClass(Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @see  java.security.CodeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @see  java.security.SecureClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    protected final Class<?> defineClass(String name, byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        return defineClass(name, b, off, len, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /* Determine protection domain, and check that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        - not define java.* class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        - signer of this class matches signers for the rest of the classes in package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    private ProtectionDomain preDefineClass(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                                            ProtectionDomain protectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (!checkName(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            throw new NoClassDefFoundError("IllegalName: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if ((name != null) && name.startsWith("java.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            throw new SecurityException("Prohibited package name: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                                        name.substring(0, name.lastIndexOf('.')));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (protectionDomain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            protectionDomain = getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (name != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            checkCerts(name, protectionDomain.getCodeSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return protectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    private String defineClassSourceLocation(ProtectionDomain protectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        CodeSource cs = protectionDomain.getCodeSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        String source = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        if (cs != null && cs.getLocation() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            source = cs.getLocation().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        return source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    private Class defineTransformedClass(String name, byte[] b, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                                         ProtectionDomain protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                                         ClassFormatError cfe, String source)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
      throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        // Class format error - try to transform the bytecode and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        // define the class again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        Object[] transformers = ClassFileTransformer.getTransformers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        Class c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        for (int i = 0; transformers != null && i < transformers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
              // Transform byte code using transformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
              byte[] tb = ((ClassFileTransformer) transformers[i]).transform(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
              c = defineClass1(name, tb, 0, tb.length, protectionDomain, source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            } catch (ClassFormatError cfe2)     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
              // If ClassFormatError occurs, try next transformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        // Rethrow original ClassFormatError if unable to transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        // bytecode to well-formed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            throw cfe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    private void postDefineClass(Class c, ProtectionDomain protectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        if (protectionDomain.getCodeSource() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            java.security.cert.Certificate certs[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                protectionDomain.getCodeSource().getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            if (certs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                setSigners(c, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * Converts an array of bytes into an instance of class <tt>Class</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * with an optional <tt>ProtectionDomain</tt>.  If the domain is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * <tt>null</tt>, then a default domain will be assigned to the class as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * specified in the documentation for {@link #defineClass(String, byte[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * int, int)}.  Before the class can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * <p> The first class defined in a package determines the exact set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * certificates that all subsequent classes defined in that package must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * contain.  The set of certificates for a class is obtained from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * {@link java.security.CodeSource <tt>CodeSource</tt>} within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * <tt>ProtectionDomain</tt> of the class.  Any classes added to that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * package must contain the same set of certificates or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * <tt>SecurityException</tt> will be thrown.  Note that if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * <tt>name</tt> is <tt>null</tt>, this check is not performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * You should always pass in the <a href="#name">binary name</a> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * class you are defining as well as the bytes.  This ensures that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * class you are defining is indeed the class you think it is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <p> The specified <tt>name</tt> cannot begin with "<tt>java.</tt>", since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * all classes in the "<tt>java.*</tt> packages can only be defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * bootstrap class loader.  If <tt>name</tt> is not <tt>null</tt>, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * must be equal to the <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * specified by the byte array "<tt>b</tt>", otherwise a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <tt>NoClassDefFoundError</tt>} will be thrown.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *         The expected <a href="#name">binary name</a> of the class, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *         <tt>null</tt> if not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *         The bytes that make up the class data. The bytes in positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *         <tt>off</tt> through <tt>off+len-1</tt> should have the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *         of a valid class file as defined by the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *         href="http://java.sun.com/docs/books/vmspec/">Java Virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *         Machine Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @param  off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *         The start offset in <tt>b</tt> of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *         The length of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @param  protectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *         The ProtectionDomain of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @return  The <tt>Class</tt> object created from the data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *          and optional <tt>ProtectionDomain</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *          If the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @throws  NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *          If <tt>name</tt> is not equal to the <a href="#name">binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *          name</a> of the class specified by <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *          If either <tt>off</tt> or <tt>len</tt> is negative, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *          <tt>off+len</tt> is greater than <tt>b.length</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *          If an attempt is made to add this class to a package that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *          contains classes that were signed by a different set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *          certificates than this class, or if <tt>name</tt> begins with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *          "<tt>java.</tt>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    protected final Class<?> defineClass(String name, byte[] b, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                                         ProtectionDomain protectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        protectionDomain = preDefineClass(name, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        Class c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        String source = defineClassSourceLocation(protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            c = defineClass1(name, b, off, len, protectionDomain, source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        } catch (ClassFormatError cfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            c = defineTransformedClass(name, b, off, len, protectionDomain, cfe, source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        postDefineClass(c, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Converts a {@link java.nio.ByteBuffer <tt>ByteBuffer</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * into an instance of class <tt>Class</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * with an optional <tt>ProtectionDomain</tt>.  If the domain is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * <tt>null</tt>, then a default domain will be assigned to the class as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * specified in the documentation for {@link #defineClass(String, byte[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * int, int)}.  Before the class can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * <p>The rules about the first class defined in a package determining the set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * certificates for the package, and the restrictions on class names are identical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * to those specified in the documentation for {@link #defineClass(String, byte[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * int, int, ProtectionDomain)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * <i>cl</i><tt>.defineClass(</tt><i>name</i><tt>,</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * <i>bBuffer</i><tt>,</tt> <i>pd</i><tt>)</tt> yields exactly the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * result as the statements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * <blockquote><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * ...<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * byte[] temp = new byte[</tt><i>bBuffer</i><tt>.{@link java.nio.ByteBuffer#remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * remaining}()];<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *     </tt><i>bBuffer</i><tt>.{@link java.nio.ByteBuffer#get(byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * get}(temp);<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *     return {@link #defineClass(String, byte[], int, int, ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * </tt><i>cl</i><tt>.defineClass}(</tt><i>name</i><tt>, temp, 0, temp.length, </tt><i>pd</i><tt>);<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * </tt></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *         The expected <a href="#name">binary name</a. of the class, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *         <tt>null</tt> if not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *         The bytes that make up the class data. The bytes from positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *         <tt>b.position()</tt> through <tt>b.position() + b.limit() -1 </tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *         should have the format of a valid class file as defined by the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *         href="http://java.sun.com/docs/books/vmspec/">Java Virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *         Machine Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @param  protectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *         The ProtectionDomain of the class, or <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @return  The <tt>Class</tt> object created from the data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *          and optional <tt>ProtectionDomain</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *          If the data did not contain a valid class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @throws  NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *          If <tt>name</tt> is not equal to the <a href="#name">binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *          name</a> of the class specified by <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *          If an attempt is made to add this class to a package that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *          contains classes that were signed by a different set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *          certificates than this class, or if <tt>name</tt> begins with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *          "<tt>java.</tt>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @see      #defineClass(String, byte[], int, int, ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                                         ProtectionDomain protectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        int len = b.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        // Use byte[] if not a direct ByteBufer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if (!b.isDirect()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            if (b.hasArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                return defineClass(name, b.array(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                                   b.position() + b.arrayOffset(), len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                                   protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                // no array, or read-only array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                byte[] tb = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                b.get(tb);  // get bytes out of byte buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                return defineClass(name, tb, 0, len, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        protectionDomain = preDefineClass(name, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        Class c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        String source = defineClassSourceLocation(protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            c = defineClass2(name, b, b.position(), len, protectionDomain, source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        } catch (ClassFormatError cfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            byte[] tb = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            b.get(tb);  // get bytes out of byte buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            c = defineTransformedClass(name, tb, 0, len, protectionDomain, cfe, source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        postDefineClass(c, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    private native Class defineClass0(String name, byte[] b, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                                      ProtectionDomain pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    private native Class defineClass1(String name, byte[] b, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                                      ProtectionDomain pd, String source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    private native Class defineClass2(String name, java.nio.ByteBuffer b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                                      int off, int len, ProtectionDomain pd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                                      String source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    // true if the name is null or has the potential to be a valid binary name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    private boolean checkName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if ((name == null) || (name.length() == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        if ((name.indexOf('/') != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            || (!VM.allowArraySyntax() && (name.charAt(0) == '[')))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    private synchronized void checkCerts(String name, CodeSource cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        String pname = (i == -1) ? "" : name.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        java.security.cert.Certificate[] pcerts =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            (java.security.cert.Certificate[]) package2certs.get(pname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        if (pcerts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            // first class in this package gets to define which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            // certificates must be the same for all other classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            // in this package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            if (cs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                pcerts = cs.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            if (pcerts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                if (nocerts == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    nocerts = new java.security.cert.Certificate[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                pcerts = nocerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            package2certs.put(pname, pcerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            java.security.cert.Certificate[] certs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            if (cs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                certs = cs.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            if (!compareCerts(pcerts, certs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                throw new SecurityException("class \""+ name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                                            "\"'s signer information does not match signer information of other classes in the same package");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * check to make sure the certs for the new class (certs) are the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * the certs for the first class inserted in the package (pcerts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    private boolean compareCerts(java.security.cert.Certificate[] pcerts,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                                 java.security.cert.Certificate[] certs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        // certs can be null, indicating no certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        if ((certs == null) || (certs.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            return pcerts.length == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        // the length must be the same at this point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        if (certs.length != pcerts.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        // go through and make sure all the certs in one array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        // are in the other and vice-versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        boolean match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        for (int i = 0; i < certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            for (int j = 0; j < pcerts.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                if (certs[i].equals(pcerts[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        // now do the same for pcerts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        for (int i = 0; i < pcerts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            for (int j = 0; j < certs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                if (pcerts[i].equals(certs[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * Links the specified class.  This (misleadingly named) method may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * used by a class loader to link a class.  If the class <tt>c</tt> has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * already been linked, then this method simply returns. Otherwise, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * class is linked as described in the "Execution" chapter of the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * href="http://java.sun.com/docs/books/jls/">Java Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * @param  c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *         The class to link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *          If <tt>c</tt> is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @see  #defineClass(String, byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    protected final void resolveClass(Class<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        resolveClass0(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    private native void resolveClass0(Class c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * Finds a class with the specified <a href="#name">binary name</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * loading it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * <p> This method loads the class through the system class loader (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * {@link #getSystemClassLoader()}).  The <tt>Class</tt> object returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * might have more than one <tt>ClassLoader</tt> associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Subclasses of <tt>ClassLoader</tt> need not usually invoke this method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * because most class loaders need to override just {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * #findClass(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * @return  The <tt>Class</tt> object for the specified <tt>name</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *          If the class could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @see  #ClassLoader(ClassLoader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @see  #getParent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    protected final Class<?> findSystemClass(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        ClassLoader system = getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        if (system == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            if (!checkName(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            return findBootstrapClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        return system.loadClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    private Class findBootstrapClass0(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        if (!checkName(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        return findBootstrapClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    private native Class findBootstrapClass(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        throws ClassNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    // Check to make sure the class loader has been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    private void check() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            throw new SecurityException("ClassLoader object not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * Returns the class with the given <a href="#name">binary name</a> if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * loader has been recorded by the Java virtual machine as an initiating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * loader of a class with that <a href="#name">binary name</a>.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * <tt>null</tt> is returned.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * @return  The <tt>Class</tt> object, or <tt>null</tt> if the class has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     *          not been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    protected final Class<?> findLoadedClass(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        if (!checkName(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        return findLoadedClass0(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    private native final Class findLoadedClass0(String name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * Sets the signers of a class.  This should be invoked after defining a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * class.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @param  c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     *         The <tt>Class</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * @param  signers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *         The signers for the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    protected final void setSigners(Class<?> c, Object[] signers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        c.setSigners(signers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    // -- Resource --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * Finds the resource with the given name.  A resource is some data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * (images, audio, text, etc) that can be accessed by class code in a way
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * that is independent of the location of the code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * <p> The name of a resource is a '<tt>/</tt>'-separated path name that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * identifies the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * <p> This method will first search the parent class loader for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * resource; if the parent is <tt>null</tt> the path of the class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * built-in to the virtual machine is searched.  That failing, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * will invoke {@link #findResource(String)} to find the resource.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * @return  A <tt>URL</tt> object for reading the resource, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *          <tt>null</tt> if the resource could not be found or the invoker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *          doesn't have adequate  privileges to get the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    public URL getResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            url = parent.getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            url = getBootstrapResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        if (url == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            url = findResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * Finds all the resources with the given name. A resource is some data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * (images, audio, text, etc) that can be accessed by class code in a way
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * that is independent of the location of the code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * <p>The name of a resource is a <tt>/</tt>-separated path name that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * identifies the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * <p> The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * #getResource(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * @return  An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     *          the resource.  If no resources could  be found, the enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     *          will be empty.  Resources that the class loader doesn't have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     *          access to will not be in the enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     *          If I/O errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * @see  #findResources(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    public Enumeration<URL> getResources(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        Enumeration[] tmp = new Enumeration[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            tmp[0] = parent.getResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            tmp[0] = getBootstrapResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        tmp[1] = findResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        return new CompoundEnumeration(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * Finds the resource with the given name. Class loader implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * should override this method to specify where to find resources.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @return  A <tt>URL</tt> object for reading the resource, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     *          <tt>null</tt> if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    protected URL findResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * Returns an enumeration of {@link java.net.URL <tt>URL</tt>} objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * representing all the resources with the given name. Class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * implementations should override this method to specify where to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * resources from.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @return  An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *          the resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     *          If I/O errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    protected Enumeration<URL> findResources(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        return new CompoundEnumeration(new Enumeration[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * Find a resource of the specified name from the search path used to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * classes.  This method locates the resource through the system class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * loader (see {@link #getSystemClassLoader()}).  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * @return  A {@link java.net.URL <tt>URL</tt>} object for reading the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     *          resource, or <tt>null</tt> if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    public static URL getSystemResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        ClassLoader system = getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        if (system == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            return getBootstrapResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        return system.getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * Finds all resources of the specified name from the search path used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * load classes.  The resources thus found are returned as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * {@link java.util.Enumeration <tt>Enumeration</tt>} of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * java.net.URL <tt>URL</tt>} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * <p> The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * #getSystemResource(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * @return  An enumeration of resource {@link java.net.URL <tt>URL</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *          objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *          If I/O errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    public static Enumeration<URL> getSystemResources(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        ClassLoader system = getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        if (system == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            return getBootstrapResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        return system.getResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * Find resources from the VM's built-in classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    private static URL getBootstrapResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        URLClassPath ucp = getBootstrapClassPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        Resource res = ucp.getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        return res != null ? res.getURL() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * Find resources from the VM's built-in classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    private static Enumeration getBootstrapResources(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        final Enumeration e = getBootstrapClassPath().getResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        return new Enumeration () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            public Object nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                return ((Resource)e.nextElement()).getURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                return e.hasMoreElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    // Returns the URLClassPath that is used for finding system resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    static URLClassPath getBootstrapClassPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        if (bootstrapClassPath == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            bootstrapClassPath = sun.misc.Launcher.getBootstrapClassPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        return bootstrapClassPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    private static URLClassPath bootstrapClassPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * Returns an input stream for reading the specified resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * <p> The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * #getResource(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @return  An input stream for reading the resource, or <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     *          if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    public InputStream getResourceAsStream(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        URL url = getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            return url != null ? url.openStream() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * Open for reading, a resource of the specified name from the search path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * used to load classes.  This method locates the resource through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * system class loader (see {@link #getSystemClassLoader()}).  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * @return  An input stream for reading the resource, or <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     *          if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    public static InputStream getSystemResourceAsStream(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        URL url = getSystemResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            return url != null ? url.openStream() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    // -- Hierarchy --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * Returns the parent class loader for delegation. Some implementations may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * use <tt>null</tt> to represent the bootstrap class loader. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * will return <tt>null</tt> in such implementations if this class loader's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * parent is the bootstrap class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * <p> If a security manager is present, and the invoker's class loader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * not <tt>null</tt> and is not an ancestor of this class loader, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * method invokes the security manager's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * SecurityManager#checkPermission(java.security.Permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * <tt>checkPermission</tt>} method with a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * RuntimePermission#RuntimePermission(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * <tt>RuntimePermission("getClassLoader")</tt>} permission to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * access to the parent class loader is permitted.  If not, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * <tt>SecurityException</tt> will be thrown.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * @return  The parent <tt>ClassLoader</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     *          If a security manager exists and its <tt>checkPermission</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     *          method doesn't allow access to this class loader's parent class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     *          loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    public final ClassLoader getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        if (parent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            ClassLoader ccl = getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            if (ccl != null && !isAncestor(ccl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * Returns the system class loader for delegation.  This is the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * delegation parent for new <tt>ClassLoader</tt> instances, and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * typically the class loader used to start the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * <p> This method is first invoked early in the runtime's startup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * sequence, at which point it creates the system class loader and sets it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * as the context class loader of the invoking <tt>Thread</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * <p> The default system class loader is an implementation-dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * <p> If the system property "<tt>java.system.class.loader</tt>" is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * when this method is first invoked then the value of that property is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * taken to be the name of a class that will be returned as the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * class loader.  The class is loaded using the default system class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * and must define a public constructor that takes a single parameter of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * type <tt>ClassLoader</tt> which is used as the delegation parent.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * instance is then created using this constructor with the default system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * class loader as the parameter.  The resulting class loader is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * to be the system class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * <p> If a security manager is present, and the invoker's class loader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * not <tt>null</tt> and the invoker's class loader is not the same as or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * an ancestor of the system class loader, then this method invokes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * security manager's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * SecurityManager#checkPermission(java.security.Permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * <tt>checkPermission</tt>} method with a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * RuntimePermission#RuntimePermission(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * <tt>RuntimePermission("getClassLoader")</tt>} permission to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * access to the system class loader.  If not, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * <tt>SecurityException</tt> will be thrown.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * @return  The system <tt>ClassLoader</tt> for delegation, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     *          <tt>null</tt> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     *          If a security manager exists and its <tt>checkPermission</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     *          method doesn't allow access to the system class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     *          If invoked recursively during the construction of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     *          loader specified by the "<tt>java.system.class.loader</tt>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     *          property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * @throws  Error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     *          If the system property "<tt>java.system.class.loader</tt>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     *          is defined but the named class could not be loaded, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     *          provider class does not define the required constructor, or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     *          exception is thrown by that constructor when it is invoked. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     *          underlying cause of the error can be retrieved via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *          {@link Throwable#getCause()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * @revised  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    public static ClassLoader getSystemClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        initSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        if (scl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            ClassLoader ccl = getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            if (ccl != null && ccl != scl && !scl.isAncestor(ccl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        return scl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    private static synchronized void initSystemClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        if (!sclSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            if (scl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                throw new IllegalStateException("recursive invocation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            sun.misc.Launcher l = sun.misc.Launcher.getLauncher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                Throwable oops = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                scl = l.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                    PrivilegedExceptionAction a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                    a = new SystemClassLoaderAction(scl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                    scl = (ClassLoader) AccessController.doPrivileged(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                    oops = pae.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                    if (oops instanceof InvocationTargetException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                        oops = oops.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                if (oops != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                    if (oops instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                        throw (Error) oops;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                        // wrap the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                        throw new Error(oops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            sclSet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    // Returns true if the specified class loader can be found in this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    // loader's delegation chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    boolean isAncestor(ClassLoader cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        ClassLoader acl = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            acl = acl.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            if (cl == acl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        } while (acl != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    // Returns the invoker's class loader, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    // NOTE: This must always be invoked when there is exactly one intervening
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    // frame from the core libraries on the stack between this method's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    // invocation and the desired invoker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    static ClassLoader getCallerClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        // NOTE use of more generic Reflection.getCallerClass()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        Class caller = Reflection.getCallerClass(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        // This can be null if the VM is requesting it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        if (caller == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        // Circumvent security check since this is package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        return caller.getClassLoader0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    // The class loader for the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    private static ClassLoader scl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    // Set to true once the system class loader has been set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    private static boolean sclSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    // -- Package --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * Defines a package by name in this <tt>ClassLoader</tt>.  This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * class loaders to define the packages for their classes. Packages must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * be created before the class is defined, and package names must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * unique within a class loader and cannot be redefined or changed once
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * created.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     *         The package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * @param  specTitle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     *         The specification title
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * @param  specVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     *         The specification version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * @param  specVendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     *         The specification vendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * @param  implTitle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     *         The implementation title
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * @param  implVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     *         The implementation version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     * @param  implVendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     *         The implementation vendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * @param  sealBase
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     *         If not <tt>null</tt>, then this package is sealed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     *         respect to the given code source {@link java.net.URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     *         <tt>URL</tt>}  object.  Otherwise, the package is not sealed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * @return  The newly defined <tt>Package</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     *          If package name duplicates an existing package either in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     *          class loader or one of its ancestors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    protected Package definePackage(String name, String specTitle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                                    String specVersion, String specVendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                                    String implTitle, String implVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                                    String implVendor, URL sealBase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        synchronized (packages) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            Package pkg = getPackage(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            if (pkg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                throw new IllegalArgumentException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            pkg = new Package(name, specTitle, specVersion, specVendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                              implTitle, implVersion, implVendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                              sealBase, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            packages.put(name, pkg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            return pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * Returns a <tt>Package</tt> that has been defined by this class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * or any of its ancestors.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     *         The package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * @return  The <tt>Package</tt> corresponding to the given name, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     *          <tt>null</tt> if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    protected Package getPackage(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        synchronized (packages) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            Package pkg = (Package)packages.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            if (pkg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                    pkg = parent.getPackage(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                    pkg = Package.getSystemPackage(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                if (pkg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                    packages.put(name, pkg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            return pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * Returns all of the <tt>Packages</tt> defined by this class loader and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * its ancestors.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * @return  The array of <tt>Package</tt> objects defined by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     *          <tt>ClassLoader</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    protected Package[] getPackages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        Map map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        synchronized (packages) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            map = (Map)packages.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        Package[] pkgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            pkgs = parent.getPackages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            pkgs = Package.getSystemPackages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        if (pkgs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            for (int i = 0; i < pkgs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                String pkgName = pkgs[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                if (map.get(pkgName) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                    map.put(pkgName, pkgs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        return (Package[])map.values().toArray(new Package[map.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    // -- Native library access --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * Returns the absolute path name of a native library.  The VM invokes this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * method to locate the native libraries that belong to classes loaded with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     * this class loader. If this method returns <tt>null</tt>, the VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * searches the library along the path specified as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * "<tt>java.library.path</tt>" property.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * @param  libname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     *         The library name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * @return  The absolute path of the native library
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * @see  System#loadLibrary(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * @see  System#mapLibraryName(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    protected String findLibrary(String libname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * The inner class NativeLibrary denotes a loaded native library instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * Every classloader contains a vector of loaded native libraries in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * private field <tt>nativeLibraries</tt>.  The native libraries loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * into the system are entered into the <tt>systemNativeLibraries</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * <p> Every native library requires a particular version of JNI. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * denoted by the private <tt>jniVersion</tt> field.  This field is set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     * the VM when it loads the library, and used by the VM to pass the correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * version of JNI to the native methods.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * @see      ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * @since    1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    static class NativeLibrary {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        // opaque handle to native library, used in native code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        long handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        // the version of JNI environment the native library requires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        private int jniVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        // the class from which the library is loaded, also indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        // the loader this native library belongs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        private Class fromClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        // the canonicalized name of the native library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        native void load(String name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        native long find(String name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        native void unload();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        public NativeLibrary(Class fromClass, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
            this.fromClass = fromClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
            synchronized (loadedLibraryNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                if (fromClass.getClassLoader() != null && handle != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                    /* remove the native library name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                    int size = loadedLibraryNames.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                    for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                        if (name.equals(loadedLibraryNames.elementAt(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                            loadedLibraryNames.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                    /* unload the library. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                    ClassLoader.nativeLibraryContext.push(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                        unload();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                        ClassLoader.nativeLibraryContext.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        // Invoked in the VM to determine the context class in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        // JNI_Load/JNI_Unload
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        static Class getFromClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            return ((NativeLibrary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                    (ClassLoader.nativeLibraryContext.peek())).fromClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
    // The "default" domain. Set as the default ProtectionDomain on newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    // created classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
    private ProtectionDomain defaultDomain = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
    // Returns (and initializes) the default domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
    private synchronized ProtectionDomain getDefaultDomain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        if (defaultDomain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            CodeSource cs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                new CodeSource(null, (java.security.cert.Certificate[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            defaultDomain = new ProtectionDomain(cs, null, this, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        return defaultDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
    // All native library names we've loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
    private static Vector loadedLibraryNames = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
    // Native libraries belonging to system classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
    private static Vector systemNativeLibraries = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
    // Native libraries associated with the class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    private Vector nativeLibraries = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    // native libraries being loaded/unloaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
    private static Stack nativeLibraryContext = new Stack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
    // The paths searched for libraries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
    static private String usr_paths[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
    static private String sys_paths[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
    private static String[] initializePath(String propname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        String ldpath = System.getProperty(propname, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        String ps = File.pathSeparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        int ldlen = ldpath.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        int i, j, n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        // Count the separators in the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        i = ldpath.indexOf(ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        while (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            i = ldpath.indexOf(ps, i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        // allocate the array of paths - n :'s = n + 1 path elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        String[] paths = new String[n + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        // Fill the array with paths from the ldpath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        n = i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        j = ldpath.indexOf(ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
        while (j >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            if (j - i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                paths[n++] = ldpath.substring(i, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            } else if (j - i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                paths[n++] = ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            i = j + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            j = ldpath.indexOf(ps, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        paths[n] = ldpath.substring(i, ldlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
        return paths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    // Invoked in the java.lang.Runtime class to implement load and loadLibrary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
    static void loadLibrary(Class fromClass, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                            boolean isAbsolute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        ClassLoader loader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            (fromClass == null) ? null : fromClass.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        if (sys_paths == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            usr_paths = initializePath("java.library.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            sys_paths = initializePath("sun.boot.library.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        if (isAbsolute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            if (loadLibrary0(fromClass, new File(name))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            throw new UnsatisfiedLinkError("Can't load library: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            String libfilename = loader.findLibrary(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            if (libfilename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                File libfile = new File(libfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                if (!libfile.isAbsolute()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                    throw new UnsatisfiedLinkError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
    "ClassLoader.findLibrary failed to return an absolute path: " + libfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                if (loadLibrary0(fromClass, libfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                throw new UnsatisfiedLinkError("Can't load " + libfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        for (int i = 0 ; i < sys_paths.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            File libfile = new File(sys_paths[i], System.mapLibraryName(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            if (loadLibrary0(fromClass, libfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
            for (int i = 0 ; i < usr_paths.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                File libfile = new File(usr_paths[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                                        System.mapLibraryName(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                if (loadLibrary0(fromClass, libfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        // Oops, it failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        throw new UnsatisfiedLinkError("no " + name + " in java.library.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    private static boolean loadLibrary0(Class fromClass, final File file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        Boolean exists = (Boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
            AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                    return new Boolean(file.exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        if (!exists.booleanValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            name = file.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        ClassLoader loader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            (fromClass == null) ? null : fromClass.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        Vector libs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
            loader != null ? loader.nativeLibraries : systemNativeLibraries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        synchronized (libs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
            int size = libs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                NativeLibrary lib = (NativeLibrary)libs.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                if (name.equals(lib.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
            synchronized (loadedLibraryNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                if (loadedLibraryNames.contains(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                    throw new UnsatisfiedLinkError
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                        ("Native Library " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                         name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                         " already loaded in another classloader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                /* If the library is being loaded (must be by the same thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                 * because Runtime.load and Runtime.loadLibrary are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                 * synchronous). The reason is can occur is that the JNI_OnLoad
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                 * function can cause another loadLibrary invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                 * Thus we can use a static stack to hold the list of libraries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                 * we are loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                 * If there is a pending load operation for the library, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                 * immediately return success; otherwise, we raise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                 * UnsatisfiedLinkError.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                int n = nativeLibraryContext.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                    NativeLibrary lib = (NativeLibrary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                        nativeLibraryContext.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                    if (name.equals(lib.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                        if (loader == lib.fromClass.getClassLoader()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                            throw new UnsatisfiedLinkError
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                                ("Native Library " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                                 name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                                 " is being loaded in another classloader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                NativeLibrary lib = new NativeLibrary(fromClass, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                nativeLibraryContext.push(lib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                    lib.load(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                    nativeLibraryContext.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                if (lib.handle != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                    loadedLibraryNames.addElement(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
                    libs.addElement(lib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
    // Invoked in the VM class linking code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    static long findNative(ClassLoader loader, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        Vector libs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            loader != null ? loader.nativeLibraries : systemNativeLibraries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        synchronized (libs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            int size = libs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                NativeLibrary lib = (NativeLibrary)libs.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                long entry = lib.find(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                if (entry != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                    return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
    // -- Assertion management --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    // The default toggle for assertion checking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
    private boolean defaultAssertionStatus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
    // Maps String packageName to Boolean package default assertion status Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    // that the default package is placed under a null map key.  If this field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
    // is null then we are delegating assertion status queries to the VM, i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
    // none of this ClassLoader's assertion status modification methods have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    // been invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    private Map packageAssertionStatus = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    // Maps String fullyQualifiedClassName to Boolean assertionStatus If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    // field is null then we are delegating assertion status queries to the VM,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
    // i.e., none of this ClassLoader's assertion status modification methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    // have been invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
    Map classAssertionStatus = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * Sets the default assertion status for this class loader.  This setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * determines whether classes loaded by this class loader and initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * in the future will have assertions enabled or disabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     * This setting may be overridden on a per-package or per-class basis by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     * invoking {@link #setPackageAssertionStatus(String, boolean)} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     * #setClassAssertionStatus(String, boolean)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     * @param  enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     *         <tt>true</tt> if classes loaded by this class loader will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     *         henceforth have assertions enabled by default, <tt>false</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     *         if they will have assertions disabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
    public synchronized void setDefaultAssertionStatus(boolean enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        if (classAssertionStatus == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            initializeJavaAssertionMaps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        defaultAssertionStatus = enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     * Sets the package default assertion status for the named package.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * package default assertion status determines the assertion status for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     * classes initialized in the future that belong to the named package or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * any of its "subpackages".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * <p> A subpackage of a package named p is any package whose name begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * with "<tt>p.</tt>".  For example, <tt>javax.swing.text</tt> is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * subpackage of <tt>javax.swing</tt>, and both <tt>java.util</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     * <tt>java.lang.reflect</tt> are subpackages of <tt>java</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     * <p> In the event that multiple package defaults apply to a given class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * the package default pertaining to the most specific package takes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * precedence over the others.  For example, if <tt>javax.lang</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     * <tt>javax.lang.reflect</tt> both have package defaults associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * them, the latter package default applies to classes in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     * <tt>javax.lang.reflect</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * <p> Package defaults take precedence over the class loader's default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     * assertion status, and may be overridden on a per-class basis by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * {@link #setClassAssertionStatus(String, boolean)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     * @param  packageName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     *         The name of the package whose package default assertion status
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     *         is to be set. A <tt>null</tt> value indicates the unnamed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     *         package that is "current"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     *         (<a href="http://java.sun.com/docs/books/jls/">Java Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     *         Specification</a>, section 7.4.2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * @param  enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     *         <tt>true</tt> if classes loaded by this classloader and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     *         belonging to the named package or any of its subpackages will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     *         have assertions enabled by default, <tt>false</tt> if they will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     *         have assertions disabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
    public synchronized void setPackageAssertionStatus(String packageName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                                                       boolean enabled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        if (packageAssertionStatus == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
            initializeJavaAssertionMaps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        packageAssertionStatus.put(packageName, Boolean.valueOf(enabled));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * Sets the desired assertion status for the named top-level class in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * class loader and any nested classes contained therein.  This setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * takes precedence over the class loader's default assertion status, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * over any applicable per-package default.  This method has no effect if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * the named class has already been initialized.  (Once a class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * initialized, its assertion status cannot change.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * <p> If the named class is not a top-level class, this invocation will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     * have no effect on the actual assertion status of any class. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     * @param  className
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     *         The fully qualified class name of the top-level class whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     *         assertion status is to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     * @param  enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     *         <tt>true</tt> if the named class is to have assertions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     *         enabled when (and if) it is initialized, <tt>false</tt> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     *         class is to have assertions disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    public synchronized void setClassAssertionStatus(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                                                     boolean enabled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        if (classAssertionStatus == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            initializeJavaAssertionMaps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        classAssertionStatus.put(className, Boolean.valueOf(enabled));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     * Sets the default assertion status for this class loader to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     * <tt>false</tt> and discards any package defaults or class assertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     * status settings associated with the class loader.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     * provided so that class loaders can be made to ignore any command line or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     * persistent assertion status settings and "start with a clean slate."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
    public synchronized void clearAssertionStatus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
         * Whether or not "Java assertion maps" are initialized, set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
         * them to empty maps, effectively ignoring any present settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        classAssertionStatus = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        packageAssertionStatus = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        defaultAssertionStatus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * Returns the assertion status that would be assigned to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * class if it were to be initialized at the time this method is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * If the named class has had its assertion status set, the most recent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     * setting will be returned; otherwise, if any package default assertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     * status pertains to this class, the most recent setting for the most
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * specific pertinent package default assertion status is returned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     * otherwise, this class loader's default assertion status is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     * @param  className
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     *         The fully qualified class name of the class whose desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     *         assertion status is being queried.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     * @return  The desired assertion status of the specified class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
     * @see  #setClassAssertionStatus(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
     * @see  #setPackageAssertionStatus(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     * @see  #setDefaultAssertionStatus(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
    synchronized boolean desiredAssertionStatus(String className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        Boolean result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        // assert classAssertionStatus   != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        // assert packageAssertionStatus != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        // Check for a class entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        result = (Boolean)classAssertionStatus.get(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        if (result != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
            return result.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        // Check for most specific package entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        int dotIndex = className.lastIndexOf(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        if (dotIndex < 0) { // default package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
            result = (Boolean)packageAssertionStatus.get(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            if (result != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                return result.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        while(dotIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            className = className.substring(0, dotIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            result = (Boolean)packageAssertionStatus.get(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            if (result != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                return result.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            dotIndex = className.lastIndexOf(".", dotIndex-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        // Return the classloader default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        return defaultAssertionStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    // Set up the assertions with information provided by the VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
    private void initializeJavaAssertionMaps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        // assert Thread.holdsLock(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        classAssertionStatus = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        packageAssertionStatus = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        AssertionStatusDirectives directives = retrieveDirectives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        for(int i = 0; i < directives.classes.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            classAssertionStatus.put(directives.classes[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                              Boolean.valueOf(directives.classEnabled[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        for(int i = 0; i < directives.packages.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            packageAssertionStatus.put(directives.packages[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                              Boolean.valueOf(directives.packageEnabled[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        defaultAssertionStatus = directives.deflt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
    // Retrieves the assertion directives from the VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    private static native AssertionStatusDirectives retrieveDirectives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
class SystemClassLoaderAction implements PrivilegedExceptionAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
    private ClassLoader parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
    SystemClassLoaderAction(ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
    public Object run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
        ClassLoader sys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        Constructor ctor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
        Class c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        Class cp[] = { ClassLoader.class };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        Object params[] = { parent };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        String cls = System.getProperty("java.system.class.loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        if (cls == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
            return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        c = Class.forName(cls, true, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        ctor = c.getDeclaredConstructor(cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        sys = (ClassLoader) ctor.newInstance(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        Thread.currentThread().setContextClassLoader(sys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        return sys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
}