2 * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
28 import com.sun.beans.finder.ClassFinder;
34 import java.beans.AppletInitializer;
36 import java.beans.beancontext.BeanContext;
40 import java.lang.reflect.Constructor;
43 import java.lang.reflect.Array;
46 * This class provides some general purpose beans control methods.
53 * Instantiate a JavaBean.
56 * @param cls the class-loader from which we should create
57 * the bean. If this is null, then the system
58 * class-loader is used.
59 * @param beanName the name of the bean within the class-loader.
60 * For example "sun.beanbox.foobah"
62 * @exception java.lang.ClassNotFoundException if the class of a serialized
63 * object could not be found.
64 * @exception java.io.IOException if an I/O error occurs.
67 public static Object instantiate(ClassLoader cls, String beanName) throws java.io.IOException, ClassNotFoundException {
68 return Beans.instantiate(cls, beanName, null, null);
73 * Instantiate a JavaBean.
76 * @param cls the class-loader from which we should create
77 * the bean. If this is null, then the system
78 * class-loader is used.
79 * @param beanName the name of the bean within the class-loader.
80 * For example "sun.beanbox.foobah"
81 * @param beanContext The BeanContext in which to nest the new bean
83 * @exception java.lang.ClassNotFoundException if the class of a serialized
84 * object could not be found.
85 * @exception java.io.IOException if an I/O error occurs.
88 public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext) throws java.io.IOException, ClassNotFoundException {
89 return Beans.instantiate(cls, beanName, beanContext, null);
95 * The bean is created based on a name relative to a class-loader.
96 * This name should be a dot-separated name such as "a.b.c".
98 * In Beans 1.0 the given name can indicate either a serialized object
99 * or a class. Other mechanisms may be added in the future. In
100 * beans 1.0 we first try to treat the beanName as a serialized object
101 * name then as a class name.
103 * When using the beanName as a serialized object name we convert the
104 * given beanName to a resource pathname and add a trailing ".ser" suffix.
105 * We then try to load a serialized object from that resource.
107 * For example, given a beanName of "x.y", Beans.instantiate would first
108 * try to read a serialized object from the resource "x/y.ser" and if
109 * that failed it would try to load the class "x.y" and create an
110 * instance of that class.
112 * If the bean is a subtype of java.applet.Applet, then it is given
113 * some special initialization. First, it is supplied with a default
114 * AppletStub and AppletContext. Second, if it was instantiated from
115 * a classname the applet's "init" method is called. (If the bean was
116 * deserialized this step is skipped.)
118 * Note that for beans which are applets, it is the caller's responsiblity
119 * to call "start" on the applet. For correct behaviour, this should be done
120 * after the applet has been added into a visible AWT container.
122 * Note that applets created via beans.instantiate run in a slightly
123 * different environment than applets running inside browsers. In
124 * particular, bean applets have no access to "parameters", so they may
125 * wish to provide property get/set methods to set parameter values. We
126 * advise bean-applet developers to test their bean-applets against both
127 * the JDK appletviewer (for a reference browser environment) and the
128 * BDK BeanBox (for a reference bean container).
130 * @param cls the class-loader from which we should create
131 * the bean. If this is null, then the system
132 * class-loader is used.
133 * @param beanName the name of the bean within the class-loader.
134 * For example "sun.beanbox.foobah"
135 * @param beanContext The BeanContext in which to nest the new bean
136 * @param initializer The AppletInitializer for the new bean
138 * @exception java.lang.ClassNotFoundException if the class of a serialized
139 * object could not be found.
140 * @exception java.io.IOException if an I/O error occurs.
143 public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext, AppletInitializer initializer)
144 throws java.io.IOException, ClassNotFoundException {
146 java.io.InputStream ins;
147 java.io.ObjectInputStream oins = null;
148 Object result = null;
149 boolean serialized = false;
150 java.io.IOException serex = null;
152 // If the given classloader is null, we check if an
153 // system classloader is available and (if so)
155 // Note that calls on the system class loader will
156 // look in the bootstrap class loader first.
159 cls = ClassLoader.getSystemClassLoader();
160 } catch (SecurityException ex) {
161 // We're not allowed to access the system class loader.
166 // Try to find a serialized object with this name
167 final String serName = beanName.replace('.','/').concat(".ser");
168 final ClassLoader loader = cls;
169 ins = (InputStream)java.security.AccessController.doPrivileged
170 (new java.security.PrivilegedAction() {
171 public Object run() {
173 return ClassLoader.getSystemResourceAsStream(serName);
175 return loader.getResourceAsStream(serName);
181 oins = new ObjectInputStream(ins);
183 oins = new ObjectInputStreamWithLoader(ins, cls);
185 result = oins.readObject();
188 } catch (java.io.IOException ex) {
190 // Drop through and try opening the class. But remember
191 // the exception in case we can't find the class either.
193 } catch (ClassNotFoundException ex) {
199 if (result == null) {
200 // No serialized object, try just instantiating the class
204 cl = ClassFinder.findClass(beanName, cls);
205 } catch (ClassNotFoundException ex) {
206 // There is no appropriate class. If we earlier tried to
207 // deserialize an object and got an IO exception, throw that,
208 // otherwise rethrow the ClassNotFoundException.
216 * Try to instantiate the class.
220 result = cl.newInstance();
221 } catch (Exception ex) {
222 // We have to remap the exception to one in our signature.
223 // But we pass extra information in the detail message.
224 throw new ClassNotFoundException("" + cl + " : " + ex, ex);
228 if (result != null) {
230 // Ok, if the result is an applet initialize it.
232 AppletStub stub = null;
234 if (result instanceof Applet) {
235 Applet applet = (Applet) result;
236 boolean needDummies = initializer == null;
240 // Figure our the codebase and docbase URLs. We do this
241 // by locating the URL for a known resource, and then
242 // massaging the URL.
244 // First find the "resource name" corresponding to the bean
245 // itself. So a serialzied bean "a.b.c" would imply a
246 // resource name of "a/b/c.ser" and a classname of "x.y"
247 // would imply a resource name of "x/y.class".
249 final String resourceName;
253 resourceName = beanName.replace('.','/').concat(".ser");
256 resourceName = beanName.replace('.','/').concat(".class");
259 URL objectUrl = null;
263 // Now get the URL correponding to the resource name.
265 final ClassLoader cloader = cls;
267 java.security.AccessController.doPrivileged
268 (new java.security.PrivilegedAction() {
269 public Object run() {
271 return ClassLoader.getSystemResource
274 return cloader.getResource(resourceName);
278 // If we found a URL, we try to locate the docbase by taking
279 // of the final path name component, and the code base by taking
280 // of the complete resourceName.
281 // So if we had a resourceName of "a/b/c.class" and we got an
282 // objectURL of "file://bert/classes/a/b/c.class" then we would
283 // want to set the codebase to "file://bert/classes/" and the
284 // docbase to "file://bert/classes/a/b/"
286 if (objectUrl != null) {
287 String s = objectUrl.toExternalForm();
289 if (s.endsWith(resourceName)) {
290 int ix = s.length() - resourceName.length();
291 codeBase = new URL(s.substring(0,ix));
294 ix = s.lastIndexOf('/');
297 docBase = new URL(s.substring(0,ix+1));
302 // Setup a default context and stub.
303 BeansAppletContext context = new BeansAppletContext(applet);
305 stub = (AppletStub)new BeansAppletStub(applet, context, codeBase, docBase);
306 applet.setStub(stub);
308 initializer.initialize(applet, beanContext);
311 // now, if there is a BeanContext, add the bean, if applicable.
313 if (beanContext != null) {
314 beanContext.add(result);
317 // If it was deserialized then it was already init-ed.
318 // Otherwise we need to initialize it.
321 // We need to set a reasonable initial size, as many
322 // applets are unhappy if they are started without
323 // having been explicitly sized.
324 applet.setSize(100,100);
329 ((BeansAppletStub)stub).active = true;
330 } else initializer.activate(applet);
332 } else if (beanContext != null) beanContext.add(result);
340 * From a given bean, obtain an object representing a specified
341 * type view of that source object.
343 * The result may be the same object or a different object. If
344 * the requested target view isn't available then the given
347 * This method is provided in Beans 1.0 as a hook to allow the
348 * addition of more flexible bean behaviour in the future.
350 * @param bean Object from which we want to obtain a view.
351 * @param targetType The type of view we'd like to get.
354 public static Object getInstanceOf(Object bean, Class<?> targetType) {
359 * Check if a bean can be viewed as a given target type.
360 * The result will be true if the Beans.getInstanceof method
361 * can be used on the given bean to obtain an object that
362 * represents the specified targetType type view.
364 * @param bean Bean from which we want to obtain a view.
365 * @param targetType The type of view we'd like to get.
366 * @return "true" if the given bean supports the given targetType.
369 public static boolean isInstanceOf(Object bean, Class<?> targetType) {
370 return Introspector.isSubclass(bean.getClass(), targetType);
375 * Test if we are in design-mode.
377 * @return True if we are running in an application construction
380 * @see java.beans.DesignMode
382 public static boolean isDesignTime() {
387 * Determines whether beans can assume a GUI is available.
389 * @return True if we are running in an environment where beans
390 * can assume that an interactive GUI is available, so they
391 * can pop up dialog boxes, etc. This will normally return
392 * true in a windowing environment, and will normally return
393 * false in a server environment or if an application is
394 * running as part of a batch job.
396 * @see java.beans.Visibility
399 public static boolean isGuiAvailable() {
404 * Used to indicate whether of not we are running in an application
405 * builder environment.
407 * <p>Note that this method is security checked
408 * and is not available to (for example) untrusted applets.
409 * More specifically, if there is a security manager,
410 * its <code>checkPropertiesAccess</code>
411 * method is called. This could result in a SecurityException.
413 * @param isDesignTime True if we're in an application builder tool.
414 * @exception SecurityException if a security manager exists and its
415 * <code>checkPropertiesAccess</code> method doesn't allow setting
416 * of system properties.
417 * @see SecurityManager#checkPropertiesAccess
420 public static void setDesignTime(boolean isDesignTime)
421 throws SecurityException {
422 SecurityManager sm = System.getSecurityManager();
424 sm.checkPropertiesAccess();
426 designTime = isDesignTime;
430 * Used to indicate whether of not we are running in an environment
431 * where GUI interaction is available.
433 * <p>Note that this method is security checked
434 * and is not available to (for example) untrusted applets.
435 * More specifically, if there is a security manager,
436 * its <code>checkPropertiesAccess</code>
437 * method is called. This could result in a SecurityException.
439 * @param isGuiAvailable True if GUI interaction is available.
440 * @exception SecurityException if a security manager exists and its
441 * <code>checkPropertiesAccess</code> method doesn't allow setting
442 * of system properties.
443 * @see SecurityManager#checkPropertiesAccess
446 public static void setGuiAvailable(boolean isGuiAvailable)
447 throws SecurityException {
448 SecurityManager sm = System.getSecurityManager();
450 sm.checkPropertiesAccess();
452 guiAvailable = isGuiAvailable;
456 private static boolean designTime;
457 private static boolean guiAvailable;
459 guiAvailable = !GraphicsEnvironment.isHeadless();
464 * This subclass of ObjectInputStream delegates loading of classes to
465 * an existing ClassLoader.
468 class ObjectInputStreamWithLoader extends ObjectInputStream
470 private ClassLoader loader;
473 * Loader must be non-null;
476 public ObjectInputStreamWithLoader(InputStream in, ClassLoader loader)
477 throws IOException, StreamCorruptedException {
480 if (loader == null) {
481 throw new IllegalArgumentException("Illegal null argument to ObjectInputStreamWithLoader");
483 this.loader = loader;
487 * Use the given ClassLoader rather than using the system class
489 protected Class resolveClass(ObjectStreamClass classDesc)
490 throws IOException, ClassNotFoundException {
492 String cname = classDesc.getName();
493 return ClassFinder.resolveClass(cname, this.loader);
498 * Package private support class. This provides a default AppletContext
499 * for beans which are applets.
502 class BeansAppletContext implements AppletContext {
504 java.util.Hashtable imageCache = new java.util.Hashtable();
506 BeansAppletContext(Applet target) {
507 this.target = target;
510 public AudioClip getAudioClip(URL url) {
511 // We don't currently support audio clips in the Beans.instantiate
512 // applet context, unless by some luck there exists a URL content
513 // class that can generate an AudioClip from the audio URL.
515 return (AudioClip) url.getContent();
516 } catch (Exception ex) {
521 public synchronized Image getImage(URL url) {
522 Object o = imageCache.get(url);
527 o = url.getContent();
531 if (o instanceof Image) {
532 imageCache.put(url, o);
535 // Otherwise it must be an ImageProducer.
536 Image img = target.createImage((java.awt.image.ImageProducer)o);
537 imageCache.put(url, img);
540 } catch (Exception ex) {
545 public Applet getApplet(String name) {
549 public java.util.Enumeration getApplets() {
550 java.util.Vector applets = new java.util.Vector();
551 applets.addElement(target);
552 return applets.elements();
555 public void showDocument(URL url) {
559 public void showDocument(URL url, String target) {
563 public void showStatus(String status) {
567 public void setStream(String key, InputStream stream)throws IOException{
571 public InputStream getStream(String key){
576 public java.util.Iterator getStreamKeys(){
583 * Package private support class. This provides an AppletStub
584 * for beans which are applets.
586 class BeansAppletStub implements AppletStub {
587 transient boolean active;
588 transient Applet target;
589 transient AppletContext context;
590 transient URL codeBase;
591 transient URL docBase;
593 BeansAppletStub(Applet target,
594 AppletContext context, URL codeBase,
596 this.target = target;
597 this.context = context;
598 this.codeBase = codeBase;
599 this.docBase = docBase;
602 public boolean isActive() {
606 public URL getDocumentBase() {
607 // use the root directory of the applet's class-loader
611 public URL getCodeBase() {
612 // use the directory where we found the class or serialized object.
616 public String getParameter(String name) {
620 public AppletContext getAppletContext() {
624 public void appletResize(int width, int height) {