author | son |
Thu, 13 Mar 2008 16:27:20 +0300 | |
changeset 108 | deaaed5cedb7 |
parent 2 | 90ce3da70b43 |
child 112 | c7ed16a1bef2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
108
deaaed5cedb7
6592751: EmbeddedFrame disposal is fragile and breaks clean AppContext termination
son
parents:
2
diff
changeset
|
2 |
* Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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 sun.awt; |
|
27 |
||
28 |
import java.awt.EventQueue; |
|
29 |
import java.awt.Window; |
|
30 |
import java.awt.SystemTray; |
|
31 |
import java.awt.TrayIcon; |
|
32 |
import java.awt.Toolkit; |
|
33 |
import java.awt.GraphicsEnvironment; |
|
34 |
import java.awt.event.InvocationEvent; |
|
35 |
import java.security.AccessController; |
|
36 |
import java.security.PrivilegedAction; |
|
37 |
import java.util.Collections; |
|
38 |
import java.util.HashMap; |
|
39 |
import java.util.IdentityHashMap; |
|
40 |
import java.util.Map; |
|
41 |
import java.util.Set; |
|
42 |
import java.util.HashSet; |
|
108
deaaed5cedb7
6592751: EmbeddedFrame disposal is fragile and breaks clean AppContext termination
son
parents:
2
diff
changeset
|
43 |
import java.util.logging.Level; |
deaaed5cedb7
6592751: EmbeddedFrame disposal is fragile and breaks clean AppContext termination
son
parents:
2
diff
changeset
|
44 |
import java.util.logging.Logger; |
2 | 45 |
import java.beans.PropertyChangeSupport; |
46 |
import java.beans.PropertyChangeListener; |
|
47 |
||
48 |
/** |
|
49 |
* The AppContext is a table referenced by ThreadGroup which stores |
|
50 |
* application service instances. (If you are not writing an application |
|
51 |
* service, or don't know what one is, please do not use this class.) |
|
52 |
* The AppContext allows applet access to what would otherwise be |
|
53 |
* potentially dangerous services, such as the ability to peek at |
|
54 |
* EventQueues or change the look-and-feel of a Swing application.<p> |
|
55 |
* |
|
56 |
* Most application services use a singleton object to provide their |
|
57 |
* services, either as a default (such as getSystemEventQueue or |
|
58 |
* getDefaultToolkit) or as static methods with class data (System). |
|
59 |
* The AppContext works with the former method by extending the concept |
|
60 |
* of "default" to be ThreadGroup-specific. Application services |
|
61 |
* lookup their singleton in the AppContext.<p> |
|
62 |
* |
|
63 |
* For example, here we have a Foo service, with its pre-AppContext |
|
64 |
* code:<p> |
|
65 |
* <code><pre> |
|
66 |
* public class Foo { |
|
67 |
* private static Foo defaultFoo = new Foo(); |
|
68 |
* |
|
69 |
* public static Foo getDefaultFoo() { |
|
70 |
* return defaultFoo; |
|
71 |
* } |
|
72 |
* |
|
73 |
* ... Foo service methods |
|
74 |
* }</pre></code><p> |
|
75 |
* |
|
76 |
* The problem with the above is that the Foo service is global in scope, |
|
77 |
* so that applets and other untrusted code can execute methods on the |
|
78 |
* single, shared Foo instance. The Foo service therefore either needs |
|
79 |
* to block its use by untrusted code using a SecurityManager test, or |
|
80 |
* restrict its capabilities so that it doesn't matter if untrusted code |
|
81 |
* executes it.<p> |
|
82 |
* |
|
83 |
* Here's the Foo class written to use the AppContext:<p> |
|
84 |
* <code><pre> |
|
85 |
* public class Foo { |
|
86 |
* public static Foo getDefaultFoo() { |
|
87 |
* Foo foo = (Foo)AppContext.getAppContext().get(Foo.class); |
|
88 |
* if (foo == null) { |
|
89 |
* foo = new Foo(); |
|
90 |
* getAppContext().put(Foo.class, foo); |
|
91 |
* } |
|
92 |
* return foo; |
|
93 |
* } |
|
94 |
* |
|
95 |
* ... Foo service methods |
|
96 |
* }</pre></code><p> |
|
97 |
* |
|
98 |
* Since a separate AppContext can exist for each ThreadGroup, trusted |
|
99 |
* and untrusted code have access to different Foo instances. This allows |
|
100 |
* untrusted code access to "system-wide" services -- the service remains |
|
101 |
* within the AppContext "sandbox". For example, say a malicious applet |
|
102 |
* wants to peek all of the key events on the EventQueue to listen for |
|
103 |
* passwords; if separate EventQueues are used for each ThreadGroup |
|
104 |
* using AppContexts, the only key events that applet will be able to |
|
105 |
* listen to are its own. A more reasonable applet request would be to |
|
106 |
* change the Swing default look-and-feel; with that default stored in |
|
107 |
* an AppContext, the applet's look-and-feel will change without |
|
108 |
* disrupting other applets or potentially the browser itself.<p> |
|
109 |
* |
|
110 |
* Because the AppContext is a facility for safely extending application |
|
111 |
* service support to applets, none of its methods may be blocked by a |
|
112 |
* a SecurityManager check in a valid Java implementation. Applets may |
|
113 |
* therefore safely invoke any of its methods without worry of being |
|
114 |
* blocked. |
|
115 |
* |
|
116 |
* Note: If a SecurityManager is installed which derives from |
|
117 |
* sun.awt.AWTSecurityManager, it may override the |
|
118 |
* AWTSecurityManager.getAppContext() method to return the proper |
|
119 |
* AppContext based on the execution context, in the case where |
|
120 |
* the default ThreadGroup-based AppContext indexing would return |
|
121 |
* the main "system" AppContext. For example, in an applet situation, |
|
122 |
* if a system thread calls into an applet, rather than returning the |
|
123 |
* main "system" AppContext (the one corresponding to the system thread), |
|
124 |
* an installed AWTSecurityManager may return the applet's AppContext |
|
125 |
* based on the execution context. |
|
126 |
* |
|
127 |
* @author Thomas Ball |
|
128 |
* @author Fred Ecks |
|
129 |
*/ |
|
130 |
public final class AppContext { |
|
108
deaaed5cedb7
6592751: EmbeddedFrame disposal is fragile and breaks clean AppContext termination
son
parents:
2
diff
changeset
|
131 |
private static final Logger log = Logger.getLogger("sun.awt.AppContext"); |
2 | 132 |
|
133 |
/* Since the contents of an AppContext are unique to each Java |
|
134 |
* session, this class should never be serialized. */ |
|
135 |
||
136 |
/* The key to put()/get() the Java EventQueue into/from the AppContext. |
|
137 |
*/ |
|
138 |
public static final Object EVENT_QUEUE_KEY = new StringBuffer("EventQueue"); |
|
139 |
||
140 |
/* A map of AppContexts, referenced by ThreadGroup. |
|
141 |
*/ |
|
142 |
private static final Map<ThreadGroup, AppContext> threadGroup2appContext = |
|
143 |
Collections.synchronizedMap(new IdentityHashMap<ThreadGroup, AppContext>()); |
|
144 |
||
145 |
/** |
|
146 |
* Returns a set containing all <code>AppContext</code>s. |
|
147 |
*/ |
|
148 |
public static Set<AppContext> getAppContexts() { |
|
149 |
return new HashSet<AppContext>(threadGroup2appContext.values()); |
|
150 |
} |
|
151 |
||
152 |
/* The main "system" AppContext, used by everything not otherwise |
|
153 |
contained in another AppContext. |
|
154 |
*/ |
|
155 |
private static AppContext mainAppContext = null; |
|
156 |
||
157 |
/* |
|
158 |
* The hash map associated with this AppContext. A private delegate |
|
159 |
* is used instead of subclassing HashMap so as to avoid all of |
|
160 |
* HashMap's potentially risky methods, such as clear(), elements(), |
|
161 |
* putAll(), etc. |
|
162 |
*/ |
|
163 |
private final HashMap table = new HashMap(); |
|
164 |
||
165 |
private final ThreadGroup threadGroup; |
|
166 |
||
167 |
/** |
|
168 |
* If any <code>PropertyChangeListeners</code> have been registered, |
|
169 |
* the <code>changeSupport</code> field describes them. |
|
170 |
* |
|
171 |
* @see #addPropertyChangeListener |
|
172 |
* @see #removePropertyChangeListener |
|
173 |
* @see #firePropertyChange |
|
174 |
*/ |
|
175 |
private PropertyChangeSupport changeSupport = null; |
|
176 |
||
177 |
public static final String DISPOSED_PROPERTY_NAME = "disposed"; |
|
178 |
public static final String GUI_DISPOSED = "guidisposed"; |
|
179 |
||
180 |
private boolean isDisposed = false; // true if AppContext is disposed |
|
181 |
||
182 |
public boolean isDisposed() { |
|
183 |
return isDisposed; |
|
184 |
} |
|
185 |
||
186 |
||
187 |
static { |
|
188 |
// On the main Thread, we get the ThreadGroup, make a corresponding |
|
189 |
// AppContext, and instantiate the Java EventQueue. This way, legacy |
|
190 |
// code is unaffected by the move to multiple AppContext ability. |
|
191 |
AccessController.doPrivileged(new PrivilegedAction() { |
|
192 |
public Object run() { |
|
193 |
ThreadGroup currentThreadGroup = |
|
194 |
Thread.currentThread().getThreadGroup(); |
|
195 |
ThreadGroup parentThreadGroup = currentThreadGroup.getParent(); |
|
196 |
while (parentThreadGroup != null) { |
|
197 |
// Find the root ThreadGroup to construct our main AppContext |
|
198 |
currentThreadGroup = parentThreadGroup; |
|
199 |
parentThreadGroup = currentThreadGroup.getParent(); |
|
200 |
} |
|
201 |
mainAppContext = new AppContext(currentThreadGroup); |
|
202 |
numAppContexts = 1; |
|
203 |
return mainAppContext; |
|
204 |
} |
|
205 |
}); |
|
206 |
} |
|
207 |
||
208 |
/* |
|
209 |
* The total number of AppContexts, system-wide. This number is |
|
210 |
* incremented at the beginning of the constructor, and decremented |
|
211 |
* at the end of dispose(). getAppContext() checks to see if this |
|
212 |
* number is 1. If so, it returns the sole AppContext without |
|
213 |
* checking Thread.currentThread(). |
|
214 |
*/ |
|
215 |
private static int numAppContexts; |
|
216 |
||
217 |
/* |
|
218 |
* The context ClassLoader that was used to create this AppContext. |
|
219 |
*/ |
|
220 |
private final ClassLoader contextClassLoader; |
|
221 |
||
222 |
/** |
|
223 |
* Constructor for AppContext. This method is <i>not</i> public, |
|
224 |
* nor should it ever be used as such. The proper way to construct |
|
225 |
* an AppContext is through the use of SunToolkit.createNewAppContext. |
|
226 |
* A ThreadGroup is created for the new AppContext, a Thread is |
|
227 |
* created within that ThreadGroup, and that Thread calls |
|
228 |
* SunToolkit.createNewAppContext before calling anything else. |
|
229 |
* That creates both the new AppContext and its EventQueue. |
|
230 |
* |
|
231 |
* @param threadGroup The ThreadGroup for the new AppContext |
|
232 |
* @see sun.awt.SunToolkit |
|
233 |
* @since 1.2 |
|
234 |
*/ |
|
235 |
AppContext(ThreadGroup threadGroup) { |
|
236 |
numAppContexts++; |
|
237 |
||
238 |
this.threadGroup = threadGroup; |
|
239 |
threadGroup2appContext.put(threadGroup, this); |
|
240 |
||
241 |
this.contextClassLoader = |
|
242 |
(ClassLoader) AccessController.doPrivileged(new PrivilegedAction() { |
|
243 |
public Object run() { |
|
244 |
return Thread.currentThread().getContextClassLoader(); |
|
245 |
} |
|
246 |
}); |
|
247 |
} |
|
248 |
||
249 |
private static MostRecentThreadAppContext mostRecentThreadAppContext = null; |
|
250 |
||
251 |
/** |
|
252 |
* Returns the appropriate AppContext for the caller, |
|
253 |
* as determined by its ThreadGroup. If the main "system" AppContext |
|
254 |
* would be returned and there's an AWTSecurityManager installed, it |
|
255 |
* is called to get the proper AppContext based on the execution |
|
256 |
* context. |
|
257 |
* |
|
258 |
* @return the AppContext for the caller. |
|
259 |
* @see java.lang.ThreadGroup |
|
260 |
* @since 1.2 |
|
261 |
*/ |
|
262 |
public final static AppContext getAppContext() { |
|
263 |
if (numAppContexts == 1) // If there's only one system-wide, |
|
264 |
return mainAppContext; // return the main system AppContext. |
|
265 |
||
266 |
final Thread currentThread = Thread.currentThread(); |
|
267 |
||
268 |
AppContext appContext = null; |
|
269 |
||
270 |
// Note: this most recent Thread/AppContext caching is thread-hot. |
|
271 |
// A simple test using SwingSet found that 96.8% of lookups |
|
272 |
// were matched using the most recent Thread/AppContext. By |
|
273 |
// instantiating a simple MostRecentThreadAppContext object on |
|
274 |
// cache misses, the cache hits can be processed without |
|
275 |
// synchronization. |
|
276 |
||
277 |
MostRecentThreadAppContext recent = mostRecentThreadAppContext; |
|
278 |
if ((recent != null) && (recent.thread == currentThread)) { |
|
279 |
appContext = recent.appContext; // Cache hit |
|
280 |
} else { |
|
281 |
appContext = (AppContext)AccessController.doPrivileged( |
|
282 |
new PrivilegedAction() { |
|
283 |
public Object run() { |
|
284 |
// Get the current ThreadGroup, and look for it and its |
|
285 |
// parents in the hash from ThreadGroup to AppContext -- |
|
286 |
// it should be found, because we use createNewContext() |
|
287 |
// when new AppContext objects are created. |
|
288 |
ThreadGroup currentThreadGroup = currentThread.getThreadGroup(); |
|
289 |
ThreadGroup threadGroup = currentThreadGroup; |
|
290 |
AppContext context = threadGroup2appContext.get(threadGroup); |
|
291 |
while (context == null) { |
|
292 |
threadGroup = threadGroup.getParent(); |
|
293 |
if (threadGroup == null) { |
|
294 |
// If we get here, we're running under a ThreadGroup that |
|
295 |
// has no AppContext associated with it. This should never |
|
296 |
// happen, because createNewContext() should be used by the |
|
297 |
// toolkit to create the ThreadGroup that everything runs |
|
298 |
// under. |
|
299 |
throw new RuntimeException("Invalid ThreadGroup"); |
|
300 |
} |
|
301 |
context = threadGroup2appContext.get(threadGroup); |
|
302 |
} |
|
303 |
// In case we did anything in the above while loop, we add |
|
304 |
// all the intermediate ThreadGroups to threadGroup2appContext |
|
305 |
// so we won't spin again. |
|
306 |
for (ThreadGroup tg = currentThreadGroup; tg != threadGroup; tg = tg.getParent()) { |
|
307 |
threadGroup2appContext.put(tg, context); |
|
308 |
} |
|
309 |
// Now we're done, so we cache the latest key/value pair. |
|
310 |
// (we do this before checking with any AWTSecurityManager, so if |
|
311 |
// this Thread equates with the main AppContext in the cache, it |
|
312 |
// still will) |
|
313 |
mostRecentThreadAppContext = |
|
314 |
new MostRecentThreadAppContext(currentThread, context); |
|
315 |
||
316 |
return context; |
|
317 |
} |
|
318 |
}); |
|
319 |
} |
|
320 |
||
321 |
if (appContext == mainAppContext) { |
|
322 |
// Before we return the main "system" AppContext, check to |
|
323 |
// see if there's an AWTSecurityManager installed. If so, |
|
324 |
// allow it to choose the AppContext to return. |
|
325 |
SecurityManager securityManager = System.getSecurityManager(); |
|
326 |
if ((securityManager != null) && |
|
327 |
(securityManager instanceof AWTSecurityManager)) { |
|
328 |
AWTSecurityManager awtSecMgr = |
|
329 |
(AWTSecurityManager)securityManager; |
|
330 |
AppContext secAppContext = awtSecMgr.getAppContext(); |
|
331 |
if (secAppContext != null) { |
|
332 |
appContext = secAppContext; // Return what we're told |
|
333 |
} |
|
334 |
} |
|
335 |
} |
|
336 |
||
337 |
return appContext; |
|
338 |
} |
|
339 |
||
340 |
private long DISPOSAL_TIMEOUT = 5000; // Default to 5-second timeout |
|
341 |
// for disposal of all Frames |
|
342 |
// (we wait for this time twice, |
|
343 |
// once for dispose(), and once |
|
344 |
// to clear the EventQueue). |
|
345 |
||
346 |
private long THREAD_INTERRUPT_TIMEOUT = 1000; |
|
347 |
// Default to 1-second timeout for all |
|
348 |
// interrupted Threads to exit, and another |
|
349 |
// 1 second for all stopped Threads to die. |
|
350 |
||
351 |
/** |
|
352 |
* Disposes of this AppContext, all of its top-level Frames, and |
|
353 |
* all Threads and ThreadGroups contained within it. |
|
354 |
* |
|
355 |
* This method must be called from a Thread which is not contained |
|
356 |
* within this AppContext. |
|
357 |
* |
|
358 |
* @exception IllegalThreadStateException if the current thread is |
|
359 |
* contained within this AppContext |
|
360 |
* @since 1.2 |
|
361 |
*/ |
|
362 |
public void dispose() throws IllegalThreadStateException { |
|
363 |
// Check to be sure that the current Thread isn't in this AppContext |
|
364 |
if (this.threadGroup.parentOf(Thread.currentThread().getThreadGroup())) { |
|
365 |
throw new IllegalThreadStateException( |
|
366 |
"Current Thread is contained within AppContext to be disposed." |
|
367 |
); |
|
368 |
} |
|
369 |
||
370 |
synchronized(this) { |
|
371 |
if (this.isDisposed) { |
|
372 |
return; // If already disposed, bail. |
|
373 |
} |
|
374 |
this.isDisposed = true; |
|
375 |
} |
|
376 |
||
377 |
final PropertyChangeSupport changeSupport = this.changeSupport; |
|
378 |
if (changeSupport != null) { |
|
379 |
changeSupport.firePropertyChange(DISPOSED_PROPERTY_NAME, false, true); |
|
380 |
} |
|
381 |
||
382 |
// First, we post an InvocationEvent to be run on the |
|
383 |
// EventDispatchThread which disposes of all top-level Frames and TrayIcons |
|
384 |
||
385 |
final Object notificationLock = new Object(); |
|
386 |
||
387 |
Runnable runnable = new Runnable() { |
|
388 |
public void run() { |
|
389 |
Window[] windowsToDispose = Window.getOwnerlessWindows(); |
|
390 |
for (Window w : windowsToDispose) { |
|
108
deaaed5cedb7
6592751: EmbeddedFrame disposal is fragile and breaks clean AppContext termination
son
parents:
2
diff
changeset
|
391 |
try { |
deaaed5cedb7
6592751: EmbeddedFrame disposal is fragile and breaks clean AppContext termination
son
parents:
2
diff
changeset
|
392 |
w.dispose(); |
deaaed5cedb7
6592751: EmbeddedFrame disposal is fragile and breaks clean AppContext termination
son
parents:
2
diff
changeset
|
393 |
} catch (Throwable t) { |
deaaed5cedb7
6592751: EmbeddedFrame disposal is fragile and breaks clean AppContext termination
son
parents:
2
diff
changeset
|
394 |
if (log.isLoggable(Level.FINER)) { |
deaaed5cedb7
6592751: EmbeddedFrame disposal is fragile and breaks clean AppContext termination
son
parents:
2
diff
changeset
|
395 |
log.log(Level.FINER, "exception occured while disposing app context", t); |
deaaed5cedb7
6592751: EmbeddedFrame disposal is fragile and breaks clean AppContext termination
son
parents:
2
diff
changeset
|
396 |
} |
deaaed5cedb7
6592751: EmbeddedFrame disposal is fragile and breaks clean AppContext termination
son
parents:
2
diff
changeset
|
397 |
} |
2 | 398 |
} |
399 |
AccessController.doPrivileged(new PrivilegedAction() { |
|
400 |
public Object run() { |
|
401 |
if (!GraphicsEnvironment.isHeadless() && SystemTray.isSupported()) |
|
402 |
{ |
|
403 |
SystemTray systemTray = SystemTray.getSystemTray(); |
|
404 |
TrayIcon[] trayIconsToDispose = systemTray.getTrayIcons(); |
|
405 |
for (TrayIcon ti : trayIconsToDispose) { |
|
406 |
systemTray.remove(ti); |
|
407 |
} |
|
408 |
} |
|
409 |
return null; |
|
410 |
} |
|
411 |
}); |
|
412 |
// Alert PropertyChangeListeners that the GUI has been disposed. |
|
413 |
if (changeSupport != null) { |
|
414 |
changeSupport.firePropertyChange(GUI_DISPOSED, false, true); |
|
415 |
} |
|
416 |
synchronized(notificationLock) { |
|
417 |
notificationLock.notifyAll(); // Notify caller that we're done |
|
418 |
} |
|
419 |
} |
|
420 |
}; |
|
421 |
synchronized(notificationLock) { |
|
422 |
SunToolkit.postEvent(this, |
|
423 |
new InvocationEvent(Toolkit.getDefaultToolkit(), runnable)); |
|
424 |
try { |
|
425 |
notificationLock.wait(DISPOSAL_TIMEOUT); |
|
426 |
} catch (InterruptedException e) { } |
|
427 |
} |
|
428 |
||
429 |
// Next, we post another InvocationEvent to the end of the |
|
430 |
// EventQueue. When it's executed, we know we've executed all |
|
431 |
// events in the queue. |
|
432 |
||
433 |
runnable = new Runnable() { public void run() { |
|
434 |
synchronized(notificationLock) { |
|
435 |
notificationLock.notifyAll(); // Notify caller that we're done |
|
436 |
} |
|
437 |
} }; |
|
438 |
synchronized(notificationLock) { |
|
439 |
SunToolkit.postEvent(this, |
|
440 |
new InvocationEvent(Toolkit.getDefaultToolkit(), runnable)); |
|
441 |
try { |
|
442 |
notificationLock.wait(DISPOSAL_TIMEOUT); |
|
443 |
} catch (InterruptedException e) { } |
|
444 |
} |
|
445 |
||
446 |
// Next, we interrupt all Threads in the ThreadGroup |
|
447 |
this.threadGroup.interrupt(); |
|
448 |
// Note, the EventDispatchThread we've interrupted may dump an |
|
449 |
// InterruptedException to the console here. This needs to be |
|
450 |
// fixed in the EventDispatchThread, not here. |
|
451 |
||
452 |
// Next, we sleep 10ms at a time, waiting for all of the active |
|
453 |
// Threads in the ThreadGroup to exit. |
|
454 |
||
455 |
long startTime = System.currentTimeMillis(); |
|
456 |
long endTime = startTime + (long)THREAD_INTERRUPT_TIMEOUT; |
|
457 |
while ((this.threadGroup.activeCount() > 0) && |
|
458 |
(System.currentTimeMillis() < endTime)) { |
|
459 |
try { |
|
460 |
Thread.sleep(10); |
|
461 |
} catch (InterruptedException e) { } |
|
462 |
} |
|
463 |
||
464 |
// Then, we stop any remaining Threads |
|
465 |
this.threadGroup.stop(); |
|
466 |
||
467 |
// Next, we sleep 10ms at a time, waiting for all of the active |
|
468 |
// Threads in the ThreadGroup to die. |
|
469 |
||
470 |
startTime = System.currentTimeMillis(); |
|
471 |
endTime = startTime + (long)THREAD_INTERRUPT_TIMEOUT; |
|
472 |
while ((this.threadGroup.activeCount() > 0) && |
|
473 |
(System.currentTimeMillis() < endTime)) { |
|
474 |
try { |
|
475 |
Thread.sleep(10); |
|
476 |
} catch (InterruptedException e) { } |
|
477 |
} |
|
478 |
||
479 |
// Next, we remove this and all subThreadGroups from threadGroup2appContext |
|
480 |
int numSubGroups = this.threadGroup.activeGroupCount(); |
|
481 |
if (numSubGroups > 0) { |
|
482 |
ThreadGroup [] subGroups = new ThreadGroup[numSubGroups]; |
|
483 |
numSubGroups = this.threadGroup.enumerate(subGroups); |
|
484 |
for (int subGroup = 0; subGroup < numSubGroups; subGroup++) { |
|
485 |
threadGroup2appContext.remove(subGroups[subGroup]); |
|
486 |
} |
|
487 |
} |
|
488 |
threadGroup2appContext.remove(this.threadGroup); |
|
489 |
||
490 |
MostRecentThreadAppContext recent = mostRecentThreadAppContext; |
|
491 |
if ((recent != null) && (recent.appContext == this)) |
|
492 |
mostRecentThreadAppContext = null; |
|
493 |
// If the "most recent" points to this, clear it for GC |
|
494 |
||
495 |
// Finally, we destroy the ThreadGroup entirely. |
|
496 |
try { |
|
497 |
this.threadGroup.destroy(); |
|
498 |
} catch (IllegalThreadStateException e) { |
|
499 |
// Fired if not all the Threads died, ignore it and proceed |
|
500 |
} |
|
501 |
||
502 |
synchronized (table) { |
|
503 |
this.table.clear(); // Clear out the Hashtable to ease garbage collection |
|
504 |
} |
|
505 |
||
506 |
numAppContexts--; |
|
507 |
||
508 |
mostRecentKeyValue = null; |
|
509 |
} |
|
510 |
||
511 |
static final class PostShutdownEventRunnable implements Runnable { |
|
512 |
private final AppContext appContext; |
|
513 |
||
514 |
public PostShutdownEventRunnable(AppContext ac) { |
|
515 |
appContext = ac; |
|
516 |
} |
|
517 |
||
518 |
public void run() { |
|
519 |
final EventQueue eq = (EventQueue)appContext.get(EVENT_QUEUE_KEY); |
|
520 |
if (eq != null) { |
|
521 |
eq.postEvent(AWTAutoShutdown.getShutdownEvent()); |
|
522 |
} |
|
523 |
} |
|
524 |
} |
|
525 |
||
526 |
static final class CreateThreadAction implements PrivilegedAction { |
|
527 |
private final AppContext appContext; |
|
528 |
private final Runnable runnable; |
|
529 |
||
530 |
public CreateThreadAction(AppContext ac, Runnable r) { |
|
531 |
appContext = ac; |
|
532 |
runnable = r; |
|
533 |
} |
|
534 |
||
535 |
public Object run() { |
|
536 |
Thread t = new Thread(appContext.getThreadGroup(), runnable); |
|
537 |
t.setContextClassLoader(appContext.getContextClassLoader()); |
|
538 |
t.setPriority(Thread.NORM_PRIORITY + 1); |
|
539 |
t.setDaemon(true); |
|
540 |
return t; |
|
541 |
} |
|
542 |
} |
|
543 |
||
544 |
static void stopEventDispatchThreads() { |
|
545 |
for (AppContext appContext: getAppContexts()) { |
|
546 |
if (appContext.isDisposed()) { |
|
547 |
continue; |
|
548 |
} |
|
549 |
Runnable r = new PostShutdownEventRunnable(appContext); |
|
550 |
// For security reasons EventQueue.postEvent should only be called |
|
551 |
// on a thread that belongs to the corresponding thread group. |
|
552 |
if (appContext != AppContext.getAppContext()) { |
|
553 |
// Create a thread that belongs to the thread group associated |
|
554 |
// with the AppContext and invokes EventQueue.postEvent. |
|
555 |
PrivilegedAction action = new CreateThreadAction(appContext, r); |
|
556 |
Thread thread = (Thread)AccessController.doPrivileged(action); |
|
557 |
thread.start(); |
|
558 |
} else { |
|
559 |
r.run(); |
|
560 |
} |
|
561 |
} |
|
562 |
} |
|
563 |
||
564 |
private MostRecentKeyValue mostRecentKeyValue = null; |
|
565 |
private MostRecentKeyValue shadowMostRecentKeyValue = null; |
|
566 |
||
567 |
/** |
|
568 |
* Returns the value to which the specified key is mapped in this context. |
|
569 |
* |
|
570 |
* @param key a key in the AppContext. |
|
571 |
* @return the value to which the key is mapped in this AppContext; |
|
572 |
* <code>null</code> if the key is not mapped to any value. |
|
573 |
* @see #put(Object, Object) |
|
574 |
* @since 1.2 |
|
575 |
*/ |
|
576 |
public Object get(Object key) { |
|
577 |
/* |
|
578 |
* The most recent reference should be updated inside a synchronized |
|
579 |
* block to avoid a race when put() and get() are executed in |
|
580 |
* parallel on different threads. |
|
581 |
*/ |
|
582 |
synchronized (table) { |
|
583 |
// Note: this most recent key/value caching is thread-hot. |
|
584 |
// A simple test using SwingSet found that 72% of lookups |
|
585 |
// were matched using the most recent key/value. By instantiating |
|
586 |
// a simple MostRecentKeyValue object on cache misses, the |
|
587 |
// cache hits can be processed without synchronization. |
|
588 |
||
589 |
MostRecentKeyValue recent = mostRecentKeyValue; |
|
590 |
if ((recent != null) && (recent.key == key)) { |
|
591 |
return recent.value; |
|
592 |
} |
|
593 |
||
594 |
Object value = table.get(key); |
|
595 |
if(mostRecentKeyValue == null) { |
|
596 |
mostRecentKeyValue = new MostRecentKeyValue(key, value); |
|
597 |
shadowMostRecentKeyValue = new MostRecentKeyValue(key, value); |
|
598 |
} else { |
|
599 |
MostRecentKeyValue auxKeyValue = mostRecentKeyValue; |
|
600 |
shadowMostRecentKeyValue.setPair(key, value); |
|
601 |
mostRecentKeyValue = shadowMostRecentKeyValue; |
|
602 |
shadowMostRecentKeyValue = auxKeyValue; |
|
603 |
} |
|
604 |
return value; |
|
605 |
} |
|
606 |
} |
|
607 |
||
608 |
/** |
|
609 |
* Maps the specified <code>key</code> to the specified |
|
610 |
* <code>value</code> in this AppContext. Neither the key nor the |
|
611 |
* value can be <code>null</code>. |
|
612 |
* <p> |
|
613 |
* The value can be retrieved by calling the <code>get</code> method |
|
614 |
* with a key that is equal to the original key. |
|
615 |
* |
|
616 |
* @param key the AppContext key. |
|
617 |
* @param value the value. |
|
618 |
* @return the previous value of the specified key in this |
|
619 |
* AppContext, or <code>null</code> if it did not have one. |
|
620 |
* @exception NullPointerException if the key or value is |
|
621 |
* <code>null</code>. |
|
622 |
* @see #get(Object) |
|
623 |
* @since 1.2 |
|
624 |
*/ |
|
625 |
public Object put(Object key, Object value) { |
|
626 |
synchronized (table) { |
|
627 |
MostRecentKeyValue recent = mostRecentKeyValue; |
|
628 |
if ((recent != null) && (recent.key == key)) |
|
629 |
recent.value = value; |
|
630 |
return table.put(key, value); |
|
631 |
} |
|
632 |
} |
|
633 |
||
634 |
/** |
|
635 |
* Removes the key (and its corresponding value) from this |
|
636 |
* AppContext. This method does nothing if the key is not in the |
|
637 |
* AppContext. |
|
638 |
* |
|
639 |
* @param key the key that needs to be removed. |
|
640 |
* @return the value to which the key had been mapped in this AppContext, |
|
641 |
* or <code>null</code> if the key did not have a mapping. |
|
642 |
* @since 1.2 |
|
643 |
*/ |
|
644 |
public Object remove(Object key) { |
|
645 |
synchronized (table) { |
|
646 |
MostRecentKeyValue recent = mostRecentKeyValue; |
|
647 |
if ((recent != null) && (recent.key == key)) |
|
648 |
recent.value = null; |
|
649 |
return table.remove(key); |
|
650 |
} |
|
651 |
} |
|
652 |
||
653 |
/** |
|
654 |
* Returns the root ThreadGroup for all Threads contained within |
|
655 |
* this AppContext. |
|
656 |
* @since 1.2 |
|
657 |
*/ |
|
658 |
public ThreadGroup getThreadGroup() { |
|
659 |
return threadGroup; |
|
660 |
} |
|
661 |
||
662 |
/** |
|
663 |
* Returns the context ClassLoader that was used to create this |
|
664 |
* AppContext. |
|
665 |
* |
|
666 |
* @see java.lang.Thread#getContextClassLoader |
|
667 |
*/ |
|
668 |
public ClassLoader getContextClassLoader() { |
|
669 |
return contextClassLoader; |
|
670 |
} |
|
671 |
||
672 |
/** |
|
673 |
* Returns a string representation of this AppContext. |
|
674 |
* @since 1.2 |
|
675 |
*/ |
|
676 |
public String toString() { |
|
677 |
return getClass().getName() + "[threadGroup=" + threadGroup.getName() + "]"; |
|
678 |
} |
|
679 |
||
680 |
/** |
|
681 |
* Returns an array of all the property change listeners |
|
682 |
* registered on this component. |
|
683 |
* |
|
684 |
* @return all of this component's <code>PropertyChangeListener</code>s |
|
685 |
* or an empty array if no property change |
|
686 |
* listeners are currently registered |
|
687 |
* |
|
688 |
* @see #addPropertyChangeListener |
|
689 |
* @see #removePropertyChangeListener |
|
690 |
* @see #getPropertyChangeListeners(java.lang.String) |
|
691 |
* @see java.beans.PropertyChangeSupport#getPropertyChangeListeners |
|
692 |
* @since 1.4 |
|
693 |
*/ |
|
694 |
public synchronized PropertyChangeListener[] getPropertyChangeListeners() { |
|
695 |
if (changeSupport == null) { |
|
696 |
return new PropertyChangeListener[0]; |
|
697 |
} |
|
698 |
return changeSupport.getPropertyChangeListeners(); |
|
699 |
} |
|
700 |
||
701 |
/** |
|
702 |
* Adds a PropertyChangeListener to the listener list for a specific |
|
703 |
* property. The specified property may be one of the following: |
|
704 |
* <ul> |
|
705 |
* <li>if this AppContext is disposed ("disposed")</li> |
|
706 |
* </ul> |
|
707 |
* <ul> |
|
708 |
* <li>if this AppContext's unowned Windows have been disposed |
|
709 |
* ("guidisposed"). Code to cleanup after the GUI is disposed |
|
710 |
* (such as LookAndFeel.uninitialize()) should execute in response to |
|
711 |
* this property being fired. Notifications for the "guidisposed" |
|
712 |
* property are sent on the event dispatch thread.</li> |
|
713 |
* </ul> |
|
714 |
* <p> |
|
715 |
* If listener is null, no exception is thrown and no action is performed. |
|
716 |
* |
|
717 |
* @param propertyName one of the property names listed above |
|
718 |
* @param listener the PropertyChangeListener to be added |
|
719 |
* |
|
720 |
* @see #removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener) |
|
721 |
* @see #getPropertyChangeListeners(java.lang.String) |
|
722 |
* @see #addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener) |
|
723 |
*/ |
|
724 |
public synchronized void addPropertyChangeListener( |
|
725 |
String propertyName, |
|
726 |
PropertyChangeListener listener) { |
|
727 |
if (listener == null) { |
|
728 |
return; |
|
729 |
} |
|
730 |
if (changeSupport == null) { |
|
731 |
changeSupport = new PropertyChangeSupport(this); |
|
732 |
} |
|
733 |
changeSupport.addPropertyChangeListener(propertyName, listener); |
|
734 |
} |
|
735 |
||
736 |
/** |
|
737 |
* Removes a PropertyChangeListener from the listener list for a specific |
|
738 |
* property. This method should be used to remove PropertyChangeListeners |
|
739 |
* that were registered for a specific bound property. |
|
740 |
* <p> |
|
741 |
* If listener is null, no exception is thrown and no action is performed. |
|
742 |
* |
|
743 |
* @param propertyName a valid property name |
|
744 |
* @param listener the PropertyChangeListener to be removed |
|
745 |
* |
|
746 |
* @see #addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener) |
|
747 |
* @see #getPropertyChangeListeners(java.lang.String) |
|
748 |
* @see #removePropertyChangeListener(java.beans.PropertyChangeListener) |
|
749 |
*/ |
|
750 |
public synchronized void removePropertyChangeListener( |
|
751 |
String propertyName, |
|
752 |
PropertyChangeListener listener) { |
|
753 |
if (listener == null || changeSupport == null) { |
|
754 |
return; |
|
755 |
} |
|
756 |
changeSupport.removePropertyChangeListener(propertyName, listener); |
|
757 |
} |
|
758 |
||
759 |
/** |
|
760 |
* Returns an array of all the listeners which have been associated |
|
761 |
* with the named property. |
|
762 |
* |
|
763 |
* @return all of the <code>PropertyChangeListeners</code> associated with |
|
764 |
* the named property or an empty array if no listeners have |
|
765 |
* been added |
|
766 |
* |
|
767 |
* @see #addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener) |
|
768 |
* @see #removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener) |
|
769 |
* @see #getPropertyChangeListeners |
|
770 |
* @since 1.4 |
|
771 |
*/ |
|
772 |
public synchronized PropertyChangeListener[] getPropertyChangeListeners( |
|
773 |
String propertyName) { |
|
774 |
if (changeSupport == null) { |
|
775 |
return new PropertyChangeListener[0]; |
|
776 |
} |
|
777 |
return changeSupport.getPropertyChangeListeners(propertyName); |
|
778 |
} |
|
779 |
} |
|
780 |
||
781 |
final class MostRecentThreadAppContext { |
|
782 |
final Thread thread; |
|
783 |
final AppContext appContext; |
|
784 |
MostRecentThreadAppContext(Thread key, AppContext value) { |
|
785 |
thread = key; |
|
786 |
appContext = value; |
|
787 |
} |
|
788 |
} |
|
789 |
||
790 |
final class MostRecentKeyValue { |
|
791 |
Object key; |
|
792 |
Object value; |
|
793 |
MostRecentKeyValue(Object k, Object v) { |
|
794 |
key = k; |
|
795 |
value = v; |
|
796 |
} |
|
797 |
void setPair(Object k, Object v) { |
|
798 |
key = k; |
|
799 |
value = v; |
|
800 |
} |
|
801 |
} |