Beans.java revision 4373
3909N/A * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved. 893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 893N/A * This code is free software; you can redistribute it and/or modify it 893N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 893N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 893N/A * This code is distributed in the hope that it will be useful, but WITHOUT 893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 893N/A * version 2 for more details (a copy is included in the LICENSE file that 893N/A * accompanied this code). 893N/A * You should have received a copy of the GNU General Public License version 893N/A * 2 along with this work; if not, write to the Free Software Foundation, 893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 893N/A * This class provides some general purpose beans control methods. 893N/A * Instantiate a JavaBean. 893N/A * @param cls the class-loader from which we should create 893N/A * the bean. If this is null, then the system 893N/A * class-loader is used. 893N/A * @param beanName the name of the bean within the class-loader. 893N/A * For example "sun.beanbox.foobah" 893N/A * @exception ClassNotFoundException if the class of a serialized 893N/A * object could not be found. 893N/A * @exception IOException if an I/O error occurs. 893N/A * Instantiate a JavaBean. 893N/A * @param cls the class-loader from which we should create 893N/A * the bean. If this is null, then the system 893N/A * class-loader is used. 893N/A * @param beanName the name of the bean within the class-loader. 4216N/A * For example "sun.beanbox.foobah" 893N/A * @param beanContext The BeanContext in which to nest the new bean 893N/A * @exception ClassNotFoundException if the class of a serialized 893N/A * object could not be found. 893N/A * @exception IOException if an I/O error occurs. 893N/A * The bean is created based on a name relative to a class-loader. 3471N/A * This name should be a dot-separated name such as "a.b.c". 893N/A * In Beans 1.0 the given name can indicate either a serialized object 893N/A * or a class. Other mechanisms may be added in the future. In 893N/A * beans 1.0 we first try to treat the beanName as a serialized object 893N/A * name then as a class name. 893N/A * When using the beanName as a serialized object name we convert the 893N/A * given beanName to a resource pathname and add a trailing ".ser" suffix. 893N/A * We then try to load a serialized object from that resource. 893N/A * For example, given a beanName of "x.y", Beans.instantiate would first 893N/A * try to read a serialized object from the resource "x/y.ser" and if 893N/A * that failed it would try to load the class "x.y" and create an 893N/A * instance of that class. 893N/A * If the bean is a subtype of java.applet.Applet, then it is given 893N/A * some special initialization. First, it is supplied with a default 893N/A * AppletStub and AppletContext. Second, if it was instantiated from 908N/A * a classname the applet's "init" method is called. (If the bean was 893N/A * deserialized this step is skipped.) 3471N/A * Note that for beans which are applets, it is the caller's responsiblity 893N/A * to call "start" on the applet. For correct behaviour, this should be done 893N/A * after the applet has been added into a visible AWT container. 893N/A * Note that applets created via beans.instantiate run in a slightly 893N/A * different environment than applets running inside browsers. In 908N/A * particular, bean applets have no access to "parameters", so they may 893N/A * wish to provide property get/set methods to set parameter values. We 893N/A * advise bean-applet developers to test their bean-applets against both 908N/A * the JDK appletviewer (for a reference browser environment) and the 893N/A * BDK BeanBox (for a reference bean container). 893N/A * @param cls the class-loader from which we should create 893N/A * the bean. If this is null, then the system 893N/A * class-loader is used. 893N/A * @param beanName the name of the bean within the class-loader. 893N/A * For example "sun.beanbox.foobah" 893N/A * @param beanContext The BeanContext in which to nest the new bean 893N/A * @param initializer The AppletInitializer for the new bean 893N/A * @exception ClassNotFoundException if the class of a serialized 893N/A * object could not be found. 893N/A * @exception IOException if an I/O error occurs. 893N/A // If the given classloader is null, we check if an 893N/A // system classloader is available and (if so) 908N/A // Note that calls on the system class loader will 893N/A // look in the bootstrap class loader first. 908N/A // We're not allowed to access the system class loader. // Try to find a serialized object with this name // Drop through and try opening the class. But remember // the exception in case we can't find the class either. // No serialized object, try just instantiating the class // There is no appropriate class. If we earlier tried to // deserialize an object and got an IO exception, throw that, // otherwise rethrow the ClassNotFoundException. * Try to instantiate the class. // We have to remap the exception to one in our signature. // But we pass extra information in the detail message. // Ok, if the result is an applet initialize it. // Figure our the codebase and docbase URLs. We do this // by locating the URL for a known resource, and then // First find the "resource name" corresponding to the bean // itself. So a serialzied bean "a.b.c" would imply a // resource name of "a/b/c.ser" and a classname of "x.y" // would imply a resource name of "x/y.class". // Now get the URL correponding to the resource name. // If we found a URL, we try to locate the docbase by taking // of the final path name component, and the code base by taking // of the complete resourceName. // So if we had a resourceName of "a/b/c.class" and we got an // want to set the codebase to "file://bert/classes/" and the // Setup a default context and stub. // now, if there is a BeanContext, add the bean, if applicable. // If it was deserialized then it was already init-ed. // Otherwise we need to initialize it. // We need to set a reasonable initial size, as many // applets are unhappy if they are started without // having been explicitly sized. * From a given bean, obtain an object representing a specified * type view of that source object. * The result may be the same object or a different object. If * the requested target view isn't available then the given * This method is provided in Beans 1.0 as a hook to allow the * addition of more flexible bean behaviour in the future. * @param bean Object from which we want to obtain a view. * @param targetType The type of view we'd like to get. * Check if a bean can be viewed as a given target type. * The result will be true if the Beans.getInstanceof method * can be used on the given bean to obtain an object that * represents the specified targetType type view. * @param bean Bean from which we want to obtain a view. * @param targetType The type of view we'd like to get. * @return "true" if the given bean supports the given targetType. * Test if we are in design-mode. * @return True if we are running in an application construction * Determines whether beans can assume a GUI is available. * @return True if we are running in an environment where beans * can assume that an interactive GUI is available, so they * can pop up dialog boxes, etc. This will normally return * true in a windowing environment, and will normally return * false in a server environment or if an application is * running as part of a batch job. * Used to indicate whether of not we are running in an application * <p>Note that this method is security checked * and is not available to (for example) untrusted applets. * More specifically, if there is a security manager, * its <code>checkPropertiesAccess</code> * method is called. This could result in a SecurityException. * @param isDesignTime True if we're in an application builder tool. * @exception SecurityException if a security manager exists and its * <code>checkPropertiesAccess</code> method doesn't allow setting * @see SecurityManager#checkPropertiesAccess * Used to indicate whether of not we are running in an environment * where GUI interaction is available. * <p>Note that this method is security checked * and is not available to (for example) untrusted applets. * More specifically, if there is a security manager, * its <code>checkPropertiesAccess</code> * method is called. This could result in a SecurityException. * @param isGuiAvailable True if GUI interaction is available. * @exception SecurityException if a security manager exists and its * <code>checkPropertiesAccess</code> method doesn't allow setting * @see SecurityManager#checkPropertiesAccess * This subclass of ObjectInputStream delegates loading of classes to * an existing ClassLoader. * Loader must be non-null; * Use the given ClassLoader rather than using the system class * Package private support class. This provides a default AppletContext * for beans which are applets. // We don't currently support audio clips in the Beans.instantiate // applet context, unless by some luck there exists a URL content // class that can generate an AudioClip from the audio URL. if (o
instanceof Image) {
// Otherwise it must be an ImageProducer. * Package private support class. This provides an AppletStub * for beans which are applets. // use the root directory of the applet's class-loader // use the directory where we found the class or serialized object.