1 /* |
1 /* |
2 * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved. |
2 * Copyright 1996-2009 Sun Microsystems, Inc. All Rights Reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Sun designates this |
7 * published by the Free Software Foundation. Sun designates this |
25 |
25 |
26 package java.beans; |
26 package java.beans; |
27 |
27 |
28 import com.sun.beans.finder.ClassFinder; |
28 import com.sun.beans.finder.ClassFinder; |
29 |
29 |
30 import java.applet.*; |
30 import java.applet.Applet; |
31 |
31 import java.applet.AppletContext; |
32 import java.awt.*; |
32 import java.applet.AppletStub; |
33 |
33 import java.applet.AudioClip; |
34 import java.beans.AppletInitializer; |
34 |
|
35 import java.awt.GraphicsEnvironment; |
|
36 import java.awt.Image; |
35 |
37 |
36 import java.beans.beancontext.BeanContext; |
38 import java.beans.beancontext.BeanContext; |
37 |
39 |
38 import java.io.*; |
40 import java.io.IOException; |
39 |
41 import java.io.InputStream; |
40 import java.lang.reflect.Constructor; |
42 import java.io.ObjectInputStream; |
|
43 import java.io.ObjectStreamClass; |
|
44 import java.io.StreamCorruptedException; |
41 |
45 |
42 import java.net.URL; |
46 import java.net.URL; |
43 import java.lang.reflect.Array; |
47 |
|
48 import java.security.AccessController; |
|
49 import java.security.PrivilegedAction; |
|
50 |
|
51 import java.util.Enumeration; |
|
52 import java.util.Hashtable; |
|
53 import java.util.Iterator; |
|
54 import java.util.Vector; |
|
55 |
|
56 import sun.awt.AppContext; |
44 |
57 |
45 /** |
58 /** |
46 * This class provides some general purpose beans control methods. |
59 * This class provides some general purpose beans control methods. |
47 */ |
60 */ |
48 |
61 |
49 public class Beans { |
62 public class Beans { |
|
63 private static final Object DESIGN_TIME = new Object(); |
|
64 private static final Object GUI_AVAILABLE = new Object(); |
50 |
65 |
51 /** |
66 /** |
52 * <p> |
67 * <p> |
53 * Instantiate a JavaBean. |
68 * Instantiate a JavaBean. |
54 * </p> |
69 * </p> |
57 * the bean. If this is null, then the system |
72 * the bean. If this is null, then the system |
58 * class-loader is used. |
73 * class-loader is used. |
59 * @param beanName the name of the bean within the class-loader. |
74 * @param beanName the name of the bean within the class-loader. |
60 * For example "sun.beanbox.foobah" |
75 * For example "sun.beanbox.foobah" |
61 * |
76 * |
62 * @exception java.lang.ClassNotFoundException if the class of a serialized |
77 * @exception ClassNotFoundException if the class of a serialized |
63 * object could not be found. |
78 * object could not be found. |
64 * @exception java.io.IOException if an I/O error occurs. |
79 * @exception IOException if an I/O error occurs. |
65 */ |
80 */ |
66 |
81 |
67 public static Object instantiate(ClassLoader cls, String beanName) throws java.io.IOException, ClassNotFoundException { |
82 public static Object instantiate(ClassLoader cls, String beanName) throws IOException, ClassNotFoundException { |
68 return Beans.instantiate(cls, beanName, null, null); |
83 return Beans.instantiate(cls, beanName, null, null); |
69 } |
84 } |
70 |
85 |
71 /** |
86 /** |
72 * <p> |
87 * <p> |
78 * class-loader is used. |
93 * class-loader is used. |
79 * @param beanName the name of the bean within the class-loader. |
94 * @param beanName the name of the bean within the class-loader. |
80 * For example "sun.beanbox.foobah" |
95 * For example "sun.beanbox.foobah" |
81 * @param beanContext The BeanContext in which to nest the new bean |
96 * @param beanContext The BeanContext in which to nest the new bean |
82 * |
97 * |
83 * @exception java.lang.ClassNotFoundException if the class of a serialized |
98 * @exception ClassNotFoundException if the class of a serialized |
84 * object could not be found. |
99 * object could not be found. |
85 * @exception java.io.IOException if an I/O error occurs. |
100 * @exception IOException if an I/O error occurs. |
86 */ |
101 */ |
87 |
102 |
88 public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext) throws java.io.IOException, ClassNotFoundException { |
103 public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext) throws IOException, ClassNotFoundException { |
89 return Beans.instantiate(cls, beanName, beanContext, null); |
104 return Beans.instantiate(cls, beanName, beanContext, null); |
90 } |
105 } |
91 |
106 |
92 /** |
107 /** |
93 * Instantiate a bean. |
108 * Instantiate a bean. |
133 * @param beanName the name of the bean within the class-loader. |
148 * @param beanName the name of the bean within the class-loader. |
134 * For example "sun.beanbox.foobah" |
149 * For example "sun.beanbox.foobah" |
135 * @param beanContext The BeanContext in which to nest the new bean |
150 * @param beanContext The BeanContext in which to nest the new bean |
136 * @param initializer The AppletInitializer for the new bean |
151 * @param initializer The AppletInitializer for the new bean |
137 * |
152 * |
138 * @exception java.lang.ClassNotFoundException if the class of a serialized |
153 * @exception ClassNotFoundException if the class of a serialized |
139 * object could not be found. |
154 * object could not be found. |
140 * @exception java.io.IOException if an I/O error occurs. |
155 * @exception IOException if an I/O error occurs. |
141 */ |
156 */ |
142 |
157 |
143 public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext, AppletInitializer initializer) |
158 public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext, AppletInitializer initializer) |
144 throws java.io.IOException, ClassNotFoundException { |
159 throws IOException, ClassNotFoundException { |
145 |
160 |
146 java.io.InputStream ins; |
161 InputStream ins; |
147 java.io.ObjectInputStream oins = null; |
162 ObjectInputStream oins = null; |
148 Object result = null; |
163 Object result = null; |
149 boolean serialized = false; |
164 boolean serialized = false; |
150 java.io.IOException serex = null; |
165 IOException serex = null; |
151 |
166 |
152 // If the given classloader is null, we check if an |
167 // If the given classloader is null, we check if an |
153 // system classloader is available and (if so) |
168 // system classloader is available and (if so) |
154 // use that instead. |
169 // use that instead. |
155 // Note that calls on the system class loader will |
170 // Note that calls on the system class loader will |
164 } |
179 } |
165 |
180 |
166 // Try to find a serialized object with this name |
181 // Try to find a serialized object with this name |
167 final String serName = beanName.replace('.','/').concat(".ser"); |
182 final String serName = beanName.replace('.','/').concat(".ser"); |
168 final ClassLoader loader = cls; |
183 final ClassLoader loader = cls; |
169 ins = (InputStream)java.security.AccessController.doPrivileged |
184 ins = (InputStream)AccessController.doPrivileged |
170 (new java.security.PrivilegedAction() { |
185 (new PrivilegedAction() { |
171 public Object run() { |
186 public Object run() { |
172 if (loader == null) |
187 if (loader == null) |
173 return ClassLoader.getSystemResourceAsStream(serName); |
188 return ClassLoader.getSystemResourceAsStream(serName); |
174 else |
189 else |
175 return loader.getResourceAsStream(serName); |
190 return loader.getResourceAsStream(serName); |
183 oins = new ObjectInputStreamWithLoader(ins, cls); |
198 oins = new ObjectInputStreamWithLoader(ins, cls); |
184 } |
199 } |
185 result = oins.readObject(); |
200 result = oins.readObject(); |
186 serialized = true; |
201 serialized = true; |
187 oins.close(); |
202 oins.close(); |
188 } catch (java.io.IOException ex) { |
203 } catch (IOException ex) { |
189 ins.close(); |
204 ins.close(); |
190 // Drop through and try opening the class. But remember |
205 // Drop through and try opening the class. But remember |
191 // the exception in case we can't find the class either. |
206 // the exception in case we can't find the class either. |
192 serex = ex; |
207 serex = ex; |
193 } catch (ClassNotFoundException ex) { |
208 } catch (ClassNotFoundException ex) { |
262 |
277 |
263 // Now get the URL correponding to the resource name. |
278 // Now get the URL correponding to the resource name. |
264 |
279 |
265 final ClassLoader cloader = cls; |
280 final ClassLoader cloader = cls; |
266 objectUrl = (URL) |
281 objectUrl = (URL) |
267 java.security.AccessController.doPrivileged |
282 AccessController.doPrivileged |
268 (new java.security.PrivilegedAction() { |
283 (new PrivilegedAction() { |
269 public Object run() { |
284 public Object run() { |
270 if (cloader == null) |
285 if (cloader == null) |
271 return ClassLoader.getSystemResource |
286 return ClassLoader.getSystemResource |
272 (resourceName); |
287 (resourceName); |
273 else |
288 else |
375 * Test if we are in design-mode. |
390 * Test if we are in design-mode. |
376 * |
391 * |
377 * @return True if we are running in an application construction |
392 * @return True if we are running in an application construction |
378 * environment. |
393 * environment. |
379 * |
394 * |
380 * @see java.beans.DesignMode |
395 * @see DesignMode |
381 */ |
396 */ |
382 public static boolean isDesignTime() { |
397 public static boolean isDesignTime() { |
383 return designTime; |
398 Object value = AppContext.getAppContext().get(DESIGN_TIME); |
|
399 return (value instanceof Boolean) && (Boolean) value; |
384 } |
400 } |
385 |
401 |
386 /** |
402 /** |
387 * Determines whether beans can assume a GUI is available. |
403 * Determines whether beans can assume a GUI is available. |
388 * |
404 * |
391 * can pop up dialog boxes, etc. This will normally return |
407 * can pop up dialog boxes, etc. This will normally return |
392 * true in a windowing environment, and will normally return |
408 * true in a windowing environment, and will normally return |
393 * false in a server environment or if an application is |
409 * false in a server environment or if an application is |
394 * running as part of a batch job. |
410 * running as part of a batch job. |
395 * |
411 * |
396 * @see java.beans.Visibility |
412 * @see Visibility |
397 * |
413 * |
398 */ |
414 */ |
399 public static boolean isGuiAvailable() { |
415 public static boolean isGuiAvailable() { |
400 return guiAvailable; |
416 Object value = AppContext.getAppContext().get(GUI_AVAILABLE); |
|
417 return (value instanceof Boolean) ? (Boolean) value : !GraphicsEnvironment.isHeadless(); |
401 } |
418 } |
402 |
419 |
403 /** |
420 /** |
404 * Used to indicate whether of not we are running in an application |
421 * Used to indicate whether of not we are running in an application |
405 * builder environment. |
422 * builder environment. |
421 throws SecurityException { |
438 throws SecurityException { |
422 SecurityManager sm = System.getSecurityManager(); |
439 SecurityManager sm = System.getSecurityManager(); |
423 if (sm != null) { |
440 if (sm != null) { |
424 sm.checkPropertiesAccess(); |
441 sm.checkPropertiesAccess(); |
425 } |
442 } |
426 designTime = isDesignTime; |
443 AppContext.getAppContext().put(DESIGN_TIME, Boolean.valueOf(isDesignTime)); |
427 } |
444 } |
428 |
445 |
429 /** |
446 /** |
430 * Used to indicate whether of not we are running in an environment |
447 * Used to indicate whether of not we are running in an environment |
431 * where GUI interaction is available. |
448 * where GUI interaction is available. |
447 throws SecurityException { |
464 throws SecurityException { |
448 SecurityManager sm = System.getSecurityManager(); |
465 SecurityManager sm = System.getSecurityManager(); |
449 if (sm != null) { |
466 if (sm != null) { |
450 sm.checkPropertiesAccess(); |
467 sm.checkPropertiesAccess(); |
451 } |
468 } |
452 guiAvailable = isGuiAvailable; |
469 AppContext.getAppContext().put(GUI_AVAILABLE, Boolean.valueOf(isGuiAvailable)); |
453 } |
|
454 |
|
455 |
|
456 private static boolean designTime; |
|
457 private static boolean guiAvailable; |
|
458 static { |
|
459 guiAvailable = !GraphicsEnvironment.isHeadless(); |
|
460 } |
470 } |
461 } |
471 } |
462 |
472 |
463 /** |
473 /** |
464 * This subclass of ObjectInputStream delegates loading of classes to |
474 * This subclass of ObjectInputStream delegates loading of classes to |
499 * for beans which are applets. |
509 * for beans which are applets. |
500 */ |
510 */ |
501 |
511 |
502 class BeansAppletContext implements AppletContext { |
512 class BeansAppletContext implements AppletContext { |
503 Applet target; |
513 Applet target; |
504 java.util.Hashtable imageCache = new java.util.Hashtable(); |
514 Hashtable imageCache = new Hashtable(); |
505 |
515 |
506 BeansAppletContext(Applet target) { |
516 BeansAppletContext(Applet target) { |
507 this.target = target; |
517 this.target = target; |
508 } |
518 } |
509 |
519 |
544 |
554 |
545 public Applet getApplet(String name) { |
555 public Applet getApplet(String name) { |
546 return null; |
556 return null; |
547 } |
557 } |
548 |
558 |
549 public java.util.Enumeration getApplets() { |
559 public Enumeration getApplets() { |
550 java.util.Vector applets = new java.util.Vector(); |
560 Vector applets = new Vector(); |
551 applets.addElement(target); |
561 applets.addElement(target); |
552 return applets.elements(); |
562 return applets.elements(); |
553 } |
563 } |
554 |
564 |
555 public void showDocument(URL url) { |
565 public void showDocument(URL url) { |