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