src/share/classes/java/beans/Beans.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 17 Jun 2009 14:45:59 +0200
changeset 1239 0b5692417910
parent 1238 57914fd9382f
child 1240 4b353465cd30
permissions -rw-r--r--
Returning back the right type of the static method - now the Beans class cannot be compiled without applet module around
     1 /*
     2  * Copyright 1996-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    25 
    26 package java.beans;
    27 
    28 import com.sun.beans.finder.ClassFinder;
    29 
    30 import java.awt.GraphicsEnvironment;
    31 
    32 import java.beans.beancontext.BeanContext;
    33 
    34 import java.io.IOException;
    35 import java.io.InputStream;
    36 import java.io.ObjectInputStream;
    37 import java.io.ObjectStreamClass;
    38 import java.io.StreamCorruptedException;
    39 
    40 
    41 import java.security.AccessController;
    42 import java.security.PrivilegedAction;
    43 
    44 import java.util.Iterator;
    45 import java.util.ServiceLoader;
    46 
    47 import sun.awt.AppContext;
    48 import sun.beans.AppletProxy;
    49 
    50 /**
    51  * This class provides some general purpose beans control methods.
    52  */
    53 
    54 public class Beans {
    55     private static final Object DESIGN_TIME = new Object();
    56     private static final Object GUI_AVAILABLE = new Object();
    57 
    58     /**
    59      * <p>
    60      * Instantiate a JavaBean.
    61      * </p>
    62      *
    63      * @param     cls         the class-loader from which we should create
    64      *                        the bean.  If this is null, then the system
    65      *                        class-loader is used.
    66      * @param     beanName    the name of the bean within the class-loader.
    67      *                        For example "sun.beanbox.foobah"
    68      *
    69      * @exception ClassNotFoundException if the class of a serialized
    70      *              object could not be found.
    71      * @exception IOException if an I/O error occurs.
    72      */
    73 
    74     public static Object instantiate(ClassLoader cls, String beanName) throws IOException, ClassNotFoundException {
    75         return Beans.instantiate(cls, beanName, null, null);
    76     }
    77 
    78     /**
    79      * <p>
    80      * Instantiate a JavaBean.
    81      * </p>
    82      *
    83      * @param     cls         the class-loader from which we should create
    84      *                        the bean.  If this is null, then the system
    85      *                        class-loader is used.
    86      * @param     beanName    the name of the bean within the class-loader.
    87      *                        For example "sun.beanbox.foobah"
    88      * @param     beanContext The BeanContext in which to nest the new bean
    89      *
    90      * @exception ClassNotFoundException if the class of a serialized
    91      *              object could not be found.
    92      * @exception IOException if an I/O error occurs.
    93      */
    94 
    95     public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext) throws IOException, ClassNotFoundException {
    96         return Beans.instantiate(cls, beanName, beanContext, null);
    97     }
    98 
    99     /**
   100      * Instantiate a bean.
   101      * <p>
   102      * The bean is created based on a name relative to a class-loader.
   103      * This name should be a dot-separated name such as "a.b.c".
   104      * <p>
   105      * In Beans 1.0 the given name can indicate either a serialized object
   106      * or a class.  Other mechanisms may be added in the future.  In
   107      * beans 1.0 we first try to treat the beanName as a serialized object
   108      * name then as a class name.
   109      * <p>
   110      * When using the beanName as a serialized object name we convert the
   111      * given beanName to a resource pathname and add a trailing ".ser" suffix.
   112      * We then try to load a serialized object from that resource.
   113      * <p>
   114      * For example, given a beanName of "x.y", Beans.instantiate would first
   115      * try to read a serialized object from the resource "x/y.ser" and if
   116      * that failed it would try to load the class "x.y" and create an
   117      * instance of that class.
   118      * <p>
   119      * If the bean is a subtype of java.applet.Applet, then it is given
   120      * some special initialization.  First, it is supplied with a default
   121      * AppletStub and AppletContext.  Second, if it was instantiated from
   122      * a classname the applet's "init" method is called.  (If the bean was
   123      * deserialized this step is skipped.)
   124      * <p>
   125      * Note that for beans which are applets, it is the caller's responsiblity
   126      * to call "start" on the applet.  For correct behaviour, this should be done
   127      * after the applet has been added into a visible AWT container.
   128      * <p>
   129      * Note that applets created via beans.instantiate run in a slightly
   130      * different environment than applets running inside browsers.  In
   131      * particular, bean applets have no access to "parameters", so they may
   132      * wish to provide property get/set methods to set parameter values.  We
   133      * advise bean-applet developers to test their bean-applets against both
   134      * the JDK appletviewer (for a reference browser environment) and the
   135      * BDK BeanBox (for a reference bean container).
   136      *
   137      * @param     cls         the class-loader from which we should create
   138      *                        the bean.  If this is null, then the system
   139      *                        class-loader is used.
   140      * @param     beanName    the name of the bean within the class-loader.
   141      *                        For example "sun.beanbox.foobah"
   142      * @param     beanContext The BeanContext in which to nest the new bean
   143      * @param     initializer The AppletInitializer for the new bean
   144      *
   145      * @exception ClassNotFoundException if the class of a serialized
   146      *              object could not be found.
   147      * @exception IOException if an I/O error occurs.
   148      */
   149 
   150     public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext, AppletInitializer initializer)
   151                         throws IOException, ClassNotFoundException {
   152 
   153         InputStream ins;
   154         ObjectInputStream oins = null;
   155         Object result = null;
   156         boolean serialized = false;
   157         IOException serex = null;
   158 
   159         // If the given classloader is null, we check if an
   160         // system classloader is available and (if so)
   161         // use that instead.
   162         // Note that calls on the system class loader will
   163         // look in the bootstrap class loader first.
   164         if (cls == null) {
   165             try {
   166                 cls = ClassLoader.getSystemClassLoader();
   167             } catch (SecurityException ex) {
   168                 // We're not allowed to access the system class loader.
   169                 // Drop through.
   170             }
   171         }
   172 
   173         // Try to find a serialized object with this name
   174         final String serName = beanName.replace('.','/').concat(".ser");
   175         final ClassLoader loader = cls;
   176         ins = (InputStream)AccessController.doPrivileged
   177             (new PrivilegedAction() {
   178                 public Object run() {
   179                     if (loader == null)
   180                         return ClassLoader.getSystemResourceAsStream(serName);
   181                     else
   182                         return loader.getResourceAsStream(serName);
   183                 }
   184         });
   185         if (ins != null) {
   186             try {
   187                 if (cls == null) {
   188                     oins = new ObjectInputStream(ins);
   189                 } else {
   190                     oins = new ObjectInputStreamWithLoader(ins, cls);
   191                 }
   192                 result = oins.readObject();
   193                 serialized = true;
   194                 oins.close();
   195             } catch (IOException ex) {
   196                 ins.close();
   197                 // Drop through and try opening the class.  But remember
   198                 // the exception in case we can't find the class either.
   199                 serex = ex;
   200             } catch (ClassNotFoundException ex) {
   201                 ins.close();
   202                 throw ex;
   203             }
   204         }
   205 
   206         if (result == null) {
   207             // No serialized object, try just instantiating the class
   208             Class cl;
   209 
   210             try {
   211                 cl = ClassFinder.findClass(beanName, cls);
   212             } catch (ClassNotFoundException ex) {
   213                 // There is no appropriate class.  If we earlier tried to
   214                 // deserialize an object and got an IO exception, throw that,
   215                 // otherwise rethrow the ClassNotFoundException.
   216                 if (serex != null) {
   217                     throw serex;
   218                 }
   219                 throw ex;
   220             }
   221 
   222             /*
   223              * Try to instantiate the class.
   224              */
   225 
   226                 try {
   227                 result = cl.newInstance();
   228             } catch (Exception ex) {
   229                 // We have to remap the exception to one in our signature.
   230                 // But we pass extra information in the detail message.
   231                 throw new ClassNotFoundException("" + cl + " : " + ex, ex);
   232             }
   233         }
   234 
   235         if (result != null) {
   236 
   237             // Ok, if the result is an applet initialize it.
   238             Iterator<AppletProxy> it = ServiceLoader.load(AppletProxy.class).iterator();
   239             AppletProxy ap = it.hasNext() ? it.next() : null;
   240             if (ap != null || !ap.initialize(
   241                 result, initializer, serialized, beanName, beanContext, cls
   242             )) {
   243                 if (beanContext != null) beanContext.add(result);
   244             }
   245         }
   246 
   247         return result;
   248     }
   249 
   250 
   251     /**
   252      * From a given bean, obtain an object representing a specified
   253      * type view of that source object.
   254      * <p>
   255      * The result may be the same object or a different object.  If
   256      * the requested target view isn't available then the given
   257      * bean is returned.
   258      * <p>
   259      * This method is provided in Beans 1.0 as a hook to allow the
   260      * addition of more flexible bean behaviour in the future.
   261      *
   262      * @param bean        Object from which we want to obtain a view.
   263      * @param targetType  The type of view we'd like to get.
   264      *
   265      */
   266     public static Object getInstanceOf(Object bean, Class<?> targetType) {
   267         return bean;
   268     }
   269 
   270     /**
   271      * Check if a bean can be viewed as a given target type.
   272      * The result will be true if the Beans.getInstanceof method
   273      * can be used on the given bean to obtain an object that
   274      * represents the specified targetType type view.
   275      *
   276      * @param bean  Bean from which we want to obtain a view.
   277      * @param targetType  The type of view we'd like to get.
   278      * @return "true" if the given bean supports the given targetType.
   279      *
   280      */
   281     public static boolean isInstanceOf(Object bean, Class<?> targetType) {
   282         return Introspector.isSubclass(bean.getClass(), targetType);
   283     }
   284 
   285 
   286     /**
   287      * Test if we are in design-mode.
   288      *
   289      * @return  True if we are running in an application construction
   290      *          environment.
   291      *
   292      * @see DesignMode
   293      */
   294     public static boolean isDesignTime() {
   295         Object value = AppContext.getAppContext().get(DESIGN_TIME);
   296         return (value instanceof Boolean) && (Boolean) value;
   297     }
   298 
   299     /**
   300      * Determines whether beans can assume a GUI is available.
   301      *
   302      * @return  True if we are running in an environment where beans
   303      *     can assume that an interactive GUI is available, so they
   304      *     can pop up dialog boxes, etc.  This will normally return
   305      *     true in a windowing environment, and will normally return
   306      *     false in a server environment or if an application is
   307      *     running as part of a batch job.
   308      *
   309      * @see Visibility
   310      *
   311      */
   312     public static boolean isGuiAvailable() {
   313         Object value = AppContext.getAppContext().get(GUI_AVAILABLE);
   314         return (value instanceof Boolean) ? (Boolean) value : !GraphicsEnvironment.isHeadless();
   315     }
   316 
   317     /**
   318      * Used to indicate whether of not we are running in an application
   319      * builder environment.
   320      *
   321      * <p>Note that this method is security checked
   322      * and is not available to (for example) untrusted applets.
   323      * More specifically, if there is a security manager,
   324      * its <code>checkPropertiesAccess</code>
   325      * method is called. This could result in a SecurityException.
   326      *
   327      * @param isDesignTime  True if we're in an application builder tool.
   328      * @exception  SecurityException  if a security manager exists and its
   329      *             <code>checkPropertiesAccess</code> method doesn't allow setting
   330      *              of system properties.
   331      * @see SecurityManager#checkPropertiesAccess
   332      */
   333 
   334     public static void setDesignTime(boolean isDesignTime)
   335                         throws SecurityException {
   336         SecurityManager sm = System.getSecurityManager();
   337         if (sm != null) {
   338             sm.checkPropertiesAccess();
   339         }
   340         AppContext.getAppContext().put(DESIGN_TIME, Boolean.valueOf(isDesignTime));
   341     }
   342 
   343     /**
   344      * Used to indicate whether of not we are running in an environment
   345      * where GUI interaction is available.
   346      *
   347      * <p>Note that this method is security checked
   348      * and is not available to (for example) untrusted applets.
   349      * More specifically, if there is a security manager,
   350      * its <code>checkPropertiesAccess</code>
   351      * method is called. This could result in a SecurityException.
   352      *
   353      * @param isGuiAvailable  True if GUI interaction is available.
   354      * @exception  SecurityException  if a security manager exists and its
   355      *             <code>checkPropertiesAccess</code> method doesn't allow setting
   356      *              of system properties.
   357      * @see SecurityManager#checkPropertiesAccess
   358      */
   359 
   360     public static void setGuiAvailable(boolean isGuiAvailable)
   361                         throws SecurityException {
   362         SecurityManager sm = System.getSecurityManager();
   363         if (sm != null) {
   364             sm.checkPropertiesAccess();
   365         }
   366         AppContext.getAppContext().put(GUI_AVAILABLE, Boolean.valueOf(isGuiAvailable));
   367     }
   368 }
   369 
   370 /**
   371  * This subclass of ObjectInputStream delegates loading of classes to
   372  * an existing ClassLoader.
   373  */
   374 
   375 class ObjectInputStreamWithLoader extends ObjectInputStream
   376 {
   377     private ClassLoader loader;
   378 
   379     /**
   380      * Loader must be non-null;
   381      */
   382 
   383     public ObjectInputStreamWithLoader(InputStream in, ClassLoader loader)
   384             throws IOException, StreamCorruptedException {
   385 
   386         super(in);
   387         if (loader == null) {
   388             throw new IllegalArgumentException("Illegal null argument to ObjectInputStreamWithLoader");
   389         }
   390         this.loader = loader;
   391     }
   392 
   393     /**
   394      * Use the given ClassLoader rather than using the system class
   395      */
   396     protected Class resolveClass(ObjectStreamClass classDesc)
   397         throws IOException, ClassNotFoundException {
   398 
   399         String cname = classDesc.getName();
   400         return ClassFinder.resolveClass(cname, this.loader);
   401     }
   402 }
   403