jdk/src/share/classes/java/beans/Beans.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2483 71aa8bfff99d
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 1996-2006 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.beans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.beans.finder.ClassFinder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.applet.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.beans.AppletInitializer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.beans.beancontext.BeanContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * This class provides some general purpose beans control methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class Beans {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Instantiate a JavaBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @param     cls         the class-loader from which we should create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *                        the bean.  If this is null, then the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *                        class-loader is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param     beanName    the name of the bean within the class-loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *                        For example "sun.beanbox.foobah"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @exception java.lang.ClassNotFoundException if the class of a serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *              object could not be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @exception java.io.IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static Object instantiate(ClassLoader cls, String beanName) throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return Beans.instantiate(cls, beanName, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Instantiate a JavaBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param     cls         the class-loader from which we should create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *                        the bean.  If this is null, then the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *                        class-loader is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param     beanName    the name of the bean within the class-loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *                        For example "sun.beanbox.foobah"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param     beanContext The BeanContext in which to nest the new bean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @exception java.lang.ClassNotFoundException if the class of a serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *              object could not be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @exception java.io.IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext) throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return Beans.instantiate(cls, beanName, beanContext, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Instantiate a bean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * The bean is created based on a name relative to a class-loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * This name should be a dot-separated name such as "a.b.c".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * In Beans 1.0 the given name can indicate either a serialized object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * or a class.  Other mechanisms may be added in the future.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * beans 1.0 we first try to treat the beanName as a serialized object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * name then as a class name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * When using the beanName as a serialized object name we convert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * given beanName to a resource pathname and add a trailing ".ser" suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * We then try to load a serialized object from that resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * For example, given a beanName of "x.y", Beans.instantiate would first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * try to read a serialized object from the resource "x/y.ser" and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * that failed it would try to load the class "x.y" and create an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * instance of that class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * If the bean is a subtype of java.applet.Applet, then it is given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * some special initialization.  First, it is supplied with a default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * AppletStub and AppletContext.  Second, if it was instantiated from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * a classname the applet's "init" method is called.  (If the bean was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * deserialized this step is skipped.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Note that for beans which are applets, it is the caller's responsiblity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * to call "start" on the applet.  For correct behaviour, this should be done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * after the applet has been added into a visible AWT container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Note that applets created via beans.instantiate run in a slightly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * different environment than applets running inside browsers.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * particular, bean applets have no access to "parameters", so they may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * wish to provide property get/set methods to set parameter values.  We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * advise bean-applet developers to test their bean-applets against both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * the JDK appletviewer (for a reference browser environment) and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * BDK BeanBox (for a reference bean container).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param     cls         the class-loader from which we should create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *                        the bean.  If this is null, then the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *                        class-loader is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param     beanName    the name of the bean within the class-loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *                        For example "sun.beanbox.foobah"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param     beanContext The BeanContext in which to nest the new bean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param     initializer The AppletInitializer for the new bean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @exception java.lang.ClassNotFoundException if the class of a serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *              object could not be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @exception java.io.IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext, AppletInitializer initializer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        java.io.InputStream ins;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        java.io.ObjectInputStream oins = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        Object result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        boolean serialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        java.io.IOException serex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // If the given classloader is null, we check if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // system classloader is available and (if so)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // use that instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // Note that calls on the system class loader will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // look in the bootstrap class loader first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (cls == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                cls = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            } catch (SecurityException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                // We're not allowed to access the system class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // Try to find a serialized object with this name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        final String serName = beanName.replace('.','/').concat(".ser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        final ClassLoader loader = cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        ins = (InputStream)java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            (new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    if (loader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        return ClassLoader.getSystemResourceAsStream(serName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        return loader.getResourceAsStream(serName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (ins != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                if (cls == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    oins = new ObjectInputStream(ins);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    oins = new ObjectInputStreamWithLoader(ins, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                result = oins.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                serialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                oins.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            } catch (java.io.IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                ins.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                // Drop through and try opening the class.  But remember
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                // the exception in case we can't find the class either.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                serex = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            } catch (ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                ins.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // No serialized object, try just instantiating the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            Class cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                cl = ClassFinder.findClass(beanName, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            } catch (ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                // There is no appropriate class.  If we earlier tried to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                // deserialize an object and got an IO exception, throw that,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                // otherwise rethrow the ClassNotFoundException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if (serex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    throw serex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
             * Try to instantiate the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                result = cl.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                // We have to remap the exception to one in our signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                // But we pass extra information in the detail message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                throw new ClassNotFoundException("" + cl + " : " + ex, ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            // Ok, if the result is an applet initialize it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            AppletStub stub = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (result instanceof Applet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                Applet  applet      = (Applet) result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                boolean needDummies = initializer == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                if (needDummies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    // Figure our the codebase and docbase URLs.  We do this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    // by locating the URL for a known resource, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    // massaging the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    // First find the "resource name" corresponding to the bean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    // itself.  So a serialzied bean "a.b.c" would imply a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    // resource name of "a/b/c.ser" and a classname of "x.y"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    // would imply a resource name of "x/y.class".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    final String resourceName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    if (serialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        // Serialized bean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        resourceName = beanName.replace('.','/').concat(".ser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        // Regular class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        resourceName = beanName.replace('.','/').concat(".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    URL objectUrl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    URL codeBase  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    URL docBase   = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    // Now get the URL correponding to the resource name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    final ClassLoader cloader = cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    objectUrl = (URL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        (new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                if (cloader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                    return ClassLoader.getSystemResource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                                                (resourceName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                                    return cloader.getResource(resourceName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    // If we found a URL, we try to locate the docbase by taking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    // of the final path name component, and the code base by taking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    // of the complete resourceName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    // So if we had a resourceName of "a/b/c.class" and we got an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    // objectURL of "file://bert/classes/a/b/c.class" then we would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    // want to set the codebase to "file://bert/classes/" and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    // docbase to "file://bert/classes/a/b/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    if (objectUrl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        String s = objectUrl.toExternalForm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        if (s.endsWith(resourceName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                            int ix   = s.length() - resourceName.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                            codeBase = new URL(s.substring(0,ix));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                            docBase  = codeBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            ix = s.lastIndexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                            if (ix >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                docBase = new URL(s.substring(0,ix+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    // Setup a default context and stub.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    BeansAppletContext context = new BeansAppletContext(applet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    stub = (AppletStub)new BeansAppletStub(applet, context, codeBase, docBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    applet.setStub(stub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    initializer.initialize(applet, beanContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                // now, if there is a BeanContext, add the bean, if applicable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                if (beanContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    beanContext.add(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                // If it was deserialized then it was already init-ed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                // Otherwise we need to initialize it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                if (!serialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    // We need to set a reasonable initial size, as many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    // applets are unhappy if they are started without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    // having been explicitly sized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    applet.setSize(100,100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    applet.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                if (needDummies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                  ((BeansAppletStub)stub).active = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                } else initializer.activate(applet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            } else if (beanContext != null) beanContext.add(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * From a given bean, obtain an object representing a specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * type view of that source object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * The result may be the same object or a different object.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * the requested target view isn't available then the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * bean is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * This method is provided in Beans 1.0 as a hook to allow the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * addition of more flexible bean behaviour in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param bean        Object from which we want to obtain a view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @param targetType  The type of view we'd like to get.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public static Object getInstanceOf(Object bean, Class<?> targetType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return bean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Check if a bean can be viewed as a given target type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * The result will be true if the Beans.getInstanceof method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * can be used on the given bean to obtain an object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * represents the specified targetType type view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param bean  Bean from which we want to obtain a view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @param targetType  The type of view we'd like to get.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @return "true" if the given bean supports the given targetType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public static boolean isInstanceOf(Object bean, Class<?> targetType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return Introspector.isSubclass(bean.getClass(), targetType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Test if we are in design-mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @return  True if we are running in an application construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *          environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @see java.beans.DesignMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public static boolean isDesignTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return designTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Determines whether beans can assume a GUI is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @return  True if we are running in an environment where beans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *     can assume that an interactive GUI is available, so they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *     can pop up dialog boxes, etc.  This will normally return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *     true in a windowing environment, and will normally return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *     false in a server environment or if an application is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *     running as part of a batch job.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @see java.beans.Visibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public static boolean isGuiAvailable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return guiAvailable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Used to indicate whether of not we are running in an application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * builder environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * <p>Note that this method is security checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * and is not available to (for example) untrusted applets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * More specifically, if there is a security manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * its <code>checkPropertiesAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * method is called. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @param isDesignTime  True if we're in an application builder tool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *             <code>checkPropertiesAccess</code> method doesn't allow setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *              of system properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @see SecurityManager#checkPropertiesAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public static void setDesignTime(boolean isDesignTime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            sm.checkPropertiesAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        designTime = isDesignTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Used to indicate whether of not we are running in an environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * where GUI interaction is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * <p>Note that this method is security checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * and is not available to (for example) untrusted applets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * More specifically, if there is a security manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * its <code>checkPropertiesAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * method is called. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param isGuiAvailable  True if GUI interaction is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *             <code>checkPropertiesAccess</code> method doesn't allow setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *              of system properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @see SecurityManager#checkPropertiesAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public static void setGuiAvailable(boolean isGuiAvailable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            sm.checkPropertiesAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        guiAvailable = isGuiAvailable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    private static boolean designTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    private static boolean guiAvailable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        guiAvailable = !GraphicsEnvironment.isHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
 * This subclass of ObjectInputStream delegates loading of classes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
 * an existing ClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
class ObjectInputStreamWithLoader extends ObjectInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    private ClassLoader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Loader must be non-null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    public ObjectInputStreamWithLoader(InputStream in, ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            throws IOException, StreamCorruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (loader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            throw new IllegalArgumentException("Illegal null argument to ObjectInputStreamWithLoader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        this.loader = loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * Use the given ClassLoader rather than using the system class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    protected Class resolveClass(ObjectStreamClass classDesc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        String cname = classDesc.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        return ClassFinder.resolveClass(cname, this.loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
 * Package private support class.  This provides a default AppletContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
 * for beans which are applets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
class BeansAppletContext implements AppletContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    Applet target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    java.util.Hashtable imageCache = new java.util.Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    BeansAppletContext(Applet target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    public AudioClip getAudioClip(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        // We don't currently support audio clips in the Beans.instantiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        // applet context, unless by some luck there exists a URL content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        // class that can generate an AudioClip from the audio URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            return (AudioClip) url.getContent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public synchronized Image getImage(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        Object o = imageCache.get(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (o != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            return (Image)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            o = url.getContent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            if (o instanceof Image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                imageCache.put(url, o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                return (Image) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            // Otherwise it must be an ImageProducer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            Image img = target.createImage((java.awt.image.ImageProducer)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            imageCache.put(url, img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            return img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    public Applet getApplet(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    public java.util.Enumeration getApplets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        java.util.Vector applets = new java.util.Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        applets.addElement(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return applets.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    public void showDocument(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        // We do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public void showDocument(URL url, String target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        // We do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    public void showStatus(String status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        // We do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public void setStream(String key, InputStream stream)throws IOException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        // We do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    public InputStream getStream(String key){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        // We do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    public java.util.Iterator getStreamKeys(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        // We do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
 * Package private support class.  This provides an AppletStub
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
 * for beans which are applets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
class BeansAppletStub implements AppletStub {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    transient boolean active;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    transient Applet target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    transient AppletContext context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    transient URL codeBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    transient URL docBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    BeansAppletStub(Applet target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                AppletContext context, URL codeBase,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                                URL docBase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        this.codeBase = codeBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        this.docBase = docBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public boolean isActive() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        return active;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    public URL getDocumentBase() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        // use the root directory of the applet's class-loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return docBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    public URL getCodeBase() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        // use the directory where we found the class or serialized object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        return codeBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    public String getParameter(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    public AppletContext getAppletContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    public void appletResize(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        // we do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
}