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