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