sun.awt
public final class: AppContext [javadoc |
source]
java.lang.Object
sun.awt.AppContext
The AppContext is a table referenced by ThreadGroup which stores
application service instances. (If you are not writing an application
service, or don't know what one is, please do not use this class.)
The AppContext allows applet access to what would otherwise be
potentially dangerous services, such as the ability to peek at
EventQueues or change the look-and-feel of a Swing application.
Most application services use a singleton object to provide their
services, either as a default (such as getSystemEventQueue or
getDefaultToolkit) or as static methods with class data (System).
The AppContext works with the former method by extending the concept
of "default" to be ThreadGroup-specific. Application services
lookup their singleton in the AppContext.
For example, here we have a Foo service, with its pre-AppContext
code:
public class Foo {
private static Foo defaultFoo = new Foo();
public static Foo getDefaultFoo() {
return defaultFoo;
}
... Foo service methods
}
The problem with the above is that the Foo service is global in scope,
so that applets and other untrusted code can execute methods on the
single, shared Foo instance. The Foo service therefore either needs
to block its use by untrusted code using a SecurityManager test, or
restrict its capabilities so that it doesn't matter if untrusted code
executes it.
Here's the Foo class written to use the AppContext:
public class Foo {
public static Foo getDefaultFoo() {
Foo foo = (Foo)AppContext.getAppContext().get(Foo.class);
if (foo == null) {
foo = new Foo();
getAppContext().put(Foo.class, foo);
}
return foo;
}
... Foo service methods
}
Since a separate AppContext can exist for each ThreadGroup, trusted
and untrusted code have access to different Foo instances. This allows
untrusted code access to "system-wide" services -- the service remains
within the AppContext "sandbox". For example, say a malicious applet
wants to peek all of the key events on the EventQueue to listen for
passwords; if separate EventQueues are used for each ThreadGroup
using AppContexts, the only key events that applet will be able to
listen to are its own. A more reasonable applet request would be to
change the Swing default look-and-feel; with that default stored in
an AppContext, the applet's look-and-feel will change without
disrupting other applets or potentially the browser itself.
Because the AppContext is a facility for safely extending application
service support to applets, none of its methods may be blocked by a
a SecurityManager check in a valid Java implementation. Applets may
therefore safely invoke any of its methods without worry of being
blocked.
Note: If a SecurityManager is installed which derives from
sun.awt.AWTSecurityManager, it may override the
AWTSecurityManager.getAppContext() method to return the proper
AppContext based on the execution context, in the case where
the default ThreadGroup-based AppContext indexing would return
the main "system" AppContext. For example, in an applet situation,
if a system thread calls into an applet, rather than returning the
main "system" AppContext (the one corresponding to the system thread),
an installed AWTSecurityManager may return the applet's AppContext
based on the execution context.
- author:
Thomas - Ball
- author:
Fred - Ecks
| Field Summary |
|---|
| public static final Object | EVENT_QUEUE_KEY | |
| public static final String | DISPOSED_PROPERTY_NAME | |
| public static final String | GUI_DISPOSED | |
| Constructor: |
AppContext(ThreadGroup threadGroup) {
numAppContexts++;
this.threadGroup = threadGroup;
threadGroup2appContext.put(threadGroup, this);
this.contextClassLoader =
(ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return Thread.currentThread().getContextClassLoader();
}
});
}
Constructor for AppContext. This method is not public,
nor should it ever be used as such. The proper way to construct
an AppContext is through the use of SunToolkit.createNewAppContext.
A ThreadGroup is created for the new AppContext, a Thread is
created within that ThreadGroup, and that Thread calls
SunToolkit.createNewAppContext before calling anything else.
That creates both the new AppContext and its EventQueue. Parameters:
threadGroup - The ThreadGroup for the new AppContext
Also see:
- sun.awt.SunToolkit
- since:
1.2 -
|
| Method from sun.awt.AppContext Summary: |
|---|
|
addPropertyChangeListener, dispose, get, getAppContext, getAppContexts, getContextClassLoader, getPropertyChangeListeners, getPropertyChangeListeners, getThreadGroup, isDisposed, put, remove, removePropertyChangeListener, stopEventDispatchThreads, toString |
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from sun.awt.AppContext Detail: |
public synchronized void addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
if (listener == null) {
return;
}
if (changeSupport == null) {
changeSupport = new PropertyChangeSupport(this);
}
changeSupport.addPropertyChangeListener(propertyName, listener);
}
Adds a PropertyChangeListener to the listener list for a specific
property. The specified property may be one of the following:
- if this AppContext is disposed ("disposed")
- if this AppContext's unowned Windows have been disposed
("guidisposed"). Code to cleanup after the GUI is disposed
(such as LookAndFeel.uninitialize()) should execute in response to
this property being fired. Notifications for the "guidisposed"
property are sent on the event dispatch thread.
If listener is null, no exception is thrown and no action is performed. |
public void dispose() throws IllegalThreadStateException {
// Check to be sure that the current Thread isn't in this AppContext
if (this.threadGroup.parentOf(Thread.currentThread().getThreadGroup())) {
throw new IllegalThreadStateException(
"Current Thread is contained within AppContext to be disposed."
);
}
synchronized(this) {
if (this.isDisposed) {
return; // If already disposed, bail.
}
this.isDisposed = true;
}
final PropertyChangeSupport changeSupport = this.changeSupport;
if (changeSupport != null) {
changeSupport.firePropertyChange(DISPOSED_PROPERTY_NAME, false, true);
}
// First, we post an InvocationEvent to be run on the
// EventDispatchThread which disposes of all top-level Frames and TrayIcons
final Object notificationLock = new Object();
Runnable runnable = new Runnable() {
public void run() {
Window[] windowsToDispose = Window.getOwnerlessWindows();
for (Window w : windowsToDispose) {
w.dispose();
}
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
if (!GraphicsEnvironment.isHeadless() && SystemTray.isSupported())
{
SystemTray systemTray = SystemTray.getSystemTray();
TrayIcon[] trayIconsToDispose = systemTray.getTrayIcons();
for (TrayIcon ti : trayIconsToDispose) {
systemTray.remove(ti);
}
}
return null;
}
});
// Alert PropertyChangeListeners that the GUI has been disposed.
if (changeSupport != null) {
changeSupport.firePropertyChange(GUI_DISPOSED, false, true);
}
synchronized(notificationLock) {
notificationLock.notifyAll(); // Notify caller that we're done
}
}
};
synchronized(notificationLock) {
SunToolkit.postEvent(this,
new InvocationEvent(Toolkit.getDefaultToolkit(), runnable));
try {
notificationLock.wait(DISPOSAL_TIMEOUT);
} catch (InterruptedException e) { }
}
// Next, we post another InvocationEvent to the end of the
// EventQueue. When it's executed, we know we've executed all
// events in the queue.
runnable = new Runnable() { public void run() {
synchronized(notificationLock) {
notificationLock.notifyAll(); // Notify caller that we're done
}
} };
synchronized(notificationLock) {
SunToolkit.postEvent(this,
new InvocationEvent(Toolkit.getDefaultToolkit(), runnable));
try {
notificationLock.wait(DISPOSAL_TIMEOUT);
} catch (InterruptedException e) { }
}
// Next, we interrupt all Threads in the ThreadGroup
this.threadGroup.interrupt();
// Note, the EventDispatchThread we've interrupted may dump an
// InterruptedException to the console here. This needs to be
// fixed in the EventDispatchThread, not here.
// Next, we sleep 10ms at a time, waiting for all of the active
// Threads in the ThreadGroup to exit.
long startTime = System.currentTimeMillis();
long endTime = startTime + (long)THREAD_INTERRUPT_TIMEOUT;
while ((this.threadGroup.activeCount() > 0) &&
(System.currentTimeMillis() < endTime)) {
try {
Thread.sleep(10);
} catch (InterruptedException e) { }
}
// Then, we stop any remaining Threads
this.threadGroup.stop();
// Next, we sleep 10ms at a time, waiting for all of the active
// Threads in the ThreadGroup to die.
startTime = System.currentTimeMillis();
endTime = startTime + (long)THREAD_INTERRUPT_TIMEOUT;
while ((this.threadGroup.activeCount() > 0) &&
(System.currentTimeMillis() < endTime)) {
try {
Thread.sleep(10);
} catch (InterruptedException e) { }
}
// Next, we remove this and all subThreadGroups from threadGroup2appContext
int numSubGroups = this.threadGroup.activeGroupCount();
if (numSubGroups > 0) {
ThreadGroup [] subGroups = new ThreadGroup[numSubGroups];
numSubGroups = this.threadGroup.enumerate(subGroups);
for (int subGroup = 0; subGroup < numSubGroups; subGroup++) {
threadGroup2appContext.remove(subGroups[subGroup]);
}
}
threadGroup2appContext.remove(this.threadGroup);
MostRecentThreadAppContext recent = mostRecentThreadAppContext;
if ((recent != null) && (recent.appContext == this))
mostRecentThreadAppContext = null;
// If the "most recent" points to this, clear it for GC
// Finally, we destroy the ThreadGroup entirely.
try {
this.threadGroup.destroy();
} catch (IllegalThreadStateException e) {
// Fired if not all the Threads died, ignore it and proceed
}
synchronized (table) {
this.table.clear(); // Clear out the Hashtable to ease garbage collection
}
numAppContexts--;
mostRecentKeyValue = null;
}
Disposes of this AppContext, all of its top-level Frames, and
all Threads and ThreadGroups contained within it.
This method must be called from a Thread which is not contained
within this AppContext. |
public Object get(Object key) {
/*
* The most recent reference should be updated inside a synchronized
* block to avoid a race when put() and get() are executed in
* parallel on different threads.
*/
synchronized (table) {
// Note: this most recent key/value caching is thread-hot.
// A simple test using SwingSet found that 72% of lookups
// were matched using the most recent key/value. By instantiating
// a simple MostRecentKeyValue object on cache misses, the
// cache hits can be processed without synchronization.
MostRecentKeyValue recent = mostRecentKeyValue;
if ((recent != null) && (recent.key == key)) {
return recent.value;
}
Object value = table.get(key);
if(mostRecentKeyValue == null) {
mostRecentKeyValue = new MostRecentKeyValue(key, value);
shadowMostRecentKeyValue = new MostRecentKeyValue(key, value);
} else {
MostRecentKeyValue auxKeyValue = mostRecentKeyValue;
shadowMostRecentKeyValue.setPair(key, value);
mostRecentKeyValue = shadowMostRecentKeyValue;
shadowMostRecentKeyValue = auxKeyValue;
}
return value;
}
}
Returns the value to which the specified key is mapped in this context. |
public static final AppContext getAppContext() {
if (numAppContexts == 1) // If there's only one system-wide,
return mainAppContext; // return the main system AppContext.
final Thread currentThread = Thread.currentThread();
AppContext appContext = null;
// Note: this most recent Thread/AppContext caching is thread-hot.
// A simple test using SwingSet found that 96.8% of lookups
// were matched using the most recent Thread/AppContext. By
// instantiating a simple MostRecentThreadAppContext object on
// cache misses, the cache hits can be processed without
// synchronization.
MostRecentThreadAppContext recent = mostRecentThreadAppContext;
if ((recent != null) && (recent.thread == currentThread)) {
appContext = recent.appContext; // Cache hit
} else {
appContext = (AppContext)AccessController.doPrivileged(
new PrivilegedAction() {
public Object run() {
// Get the current ThreadGroup, and look for it and its
// parents in the hash from ThreadGroup to AppContext --
// it should be found, because we use createNewContext()
// when new AppContext objects are created.
ThreadGroup currentThreadGroup = currentThread.getThreadGroup();
ThreadGroup threadGroup = currentThreadGroup;
AppContext context = threadGroup2appContext.get(threadGroup);
while (context == null) {
threadGroup = threadGroup.getParent();
if (threadGroup == null) {
// If we get here, we're running under a ThreadGroup that
// has no AppContext associated with it. This should never
// happen, because createNewContext() should be used by the
// toolkit to create the ThreadGroup that everything runs
// under.
throw new RuntimeException("Invalid ThreadGroup");
}
context = threadGroup2appContext.get(threadGroup);
}
// In case we did anything in the above while loop, we add
// all the intermediate ThreadGroups to threadGroup2appContext
// so we won't spin again.
for (ThreadGroup tg = currentThreadGroup; tg != threadGroup; tg = tg.getParent()) {
threadGroup2appContext.put(tg, context);
}
// Now we're done, so we cache the latest key/value pair.
// (we do this before checking with any AWTSecurityManager, so if
// this Thread equates with the main AppContext in the cache, it
// still will)
mostRecentThreadAppContext =
new MostRecentThreadAppContext(currentThread, context);
return context;
}
});
}
if (appContext == mainAppContext) {
// Before we return the main "system" AppContext, check to
// see if there's an AWTSecurityManager installed. If so,
// allow it to choose the AppContext to return.
SecurityManager securityManager = System.getSecurityManager();
if ((securityManager != null) &&
(securityManager instanceof AWTSecurityManager)) {
AWTSecurityManager awtSecMgr =
(AWTSecurityManager)securityManager;
AppContext secAppContext = awtSecMgr.getAppContext();
if (secAppContext != null) {
appContext = secAppContext; // Return what we're told
}
}
}
return appContext;
}
Returns the appropriate AppContext for the caller,
as determined by its ThreadGroup. If the main "system" AppContext
would be returned and there's an AWTSecurityManager installed, it
is called to get the proper AppContext based on the execution
context. |
public static Set<AppContext> getAppContexts() {
return new HashSet< AppContext >(threadGroup2appContext.values());
}
Returns a set containing all AppContexts. |
public ClassLoader getContextClassLoader() {
return contextClassLoader;
}
Returns the context ClassLoader that was used to create this
AppContext. |
public synchronized PropertyChangeListener[] getPropertyChangeListeners() {
if (changeSupport == null) {
return new PropertyChangeListener[0];
}
return changeSupport.getPropertyChangeListeners();
}
Returns an array of all the property change listeners
registered on this component. |
public synchronized PropertyChangeListener[] getPropertyChangeListeners(String propertyName) {
if (changeSupport == null) {
return new PropertyChangeListener[0];
}
return changeSupport.getPropertyChangeListeners(propertyName);
}
Returns an array of all the listeners which have been associated
with the named property. |
public ThreadGroup getThreadGroup() {
return threadGroup;
}
Returns the root ThreadGroup for all Threads contained within
this AppContext. |
public boolean isDisposed() {
return isDisposed;
}
|
public Object put(Object key,
Object value) {
synchronized (table) {
MostRecentKeyValue recent = mostRecentKeyValue;
if ((recent != null) && (recent.key == key))
recent.value = value;
return table.put(key, value);
}
}
|
public Object remove(Object key) {
synchronized (table) {
MostRecentKeyValue recent = mostRecentKeyValue;
if ((recent != null) && (recent.key == key))
recent.value = null;
return table.remove(key);
}
}
Removes the key (and its corresponding value) from this
AppContext. This method does nothing if the key is not in the
AppContext. |
public synchronized void removePropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
if (listener == null || changeSupport == null) {
return;
}
changeSupport.removePropertyChangeListener(propertyName, listener);
}
Removes a PropertyChangeListener from the listener list for a specific
property. This method should be used to remove PropertyChangeListeners
that were registered for a specific bound property.
If listener is null, no exception is thrown and no action is performed. |
static void stopEventDispatchThreads() {
for (AppContext appContext: getAppContexts()) {
if (appContext.isDisposed()) {
continue;
}
Runnable r = new PostShutdownEventRunnable(appContext);
// For security reasons EventQueue.postEvent should only be called
// on a thread that belongs to the corresponding thread group.
if (appContext != AppContext.getAppContext()) {
// Create a thread that belongs to the thread group associated
// with the AppContext and invokes EventQueue.postEvent.
PrivilegedAction action = new CreateThreadAction(appContext, r);
Thread thread = (Thread)AccessController.doPrivileged(action);
thread.start();
} else {
r.run();
}
}
}
|
public String toString() {
return getClass().getName() + "[threadGroup=" + threadGroup.getName() + "]";
}
Returns a string representation of this AppContext. |