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