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