src/share/classes/sun/awt/AppContext.java

Print this page




  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;
  43 import java.beans.PropertyChangeSupport;
  44 import java.beans.PropertyChangeListener;

  45 import sun.util.logging.PlatformLogger;
  46 import java.util.concurrent.locks.Condition;
  47 import java.util.concurrent.locks.Lock;
  48 import java.util.concurrent.locks.ReentrantLock;
  49 import java.util.concurrent.atomic.AtomicInteger;

  50 
  51 /**
  52  * The AppContext is a table referenced by ThreadGroup which stores
  53  * application service instances.  (If you are not writing an application
  54  * service, or don't know what one is, please do not use this class.)
  55  * The AppContext allows applet access to what would otherwise be
  56  * potentially dangerous services, such as the ability to peek at
  57  * EventQueues or change the look-and-feel of a Swing application.<p>
  58  *
  59  * Most application services use a singleton object to provide their
  60  * services, either as a default (such as getSystemEventQueue or
  61  * getDefaultToolkit) or as static methods with class data (System).
  62  * The AppContext works with the former method by extending the concept
  63  * of "default" to be ThreadGroup-specific.  Application services
  64  * lookup their singleton in the AppContext.<p>
  65  *
  66  * For example, here we have a Foo service, with its pre-AppContext
  67  * code:<p>
  68  * <code><pre>
  69  *    public class Foo {


 866                    // in tests for instance.
 867                    ecx = ecx != null ? ecx : getAppContext();
 868                 }
 869 
 870                 // getAppletContext() may be called when initializing the main
 871                 // app context - in which case mainAppContext will still be
 872                 // null. To work around this issue we simply use
 873                 // AppContext.threadGroup.getParent() == null instead, since
 874                 // mainAppContext is the only AppContext which should have
 875                 // the root TG as its thread group.
 876                 // See: JDK-8023258
 877                 final boolean isMainAppContext = ecx == null
 878                     || mainAppContext == ecx
 879                     || mainAppContext == null && hasRootThreadGroup(ecx);
 880 
 881                 return isMainAppContext ? null : ecx;
 882             }
 883 
 884         });
 885     }

















 886 }
 887 
 888 final class MostRecentKeyValue {
 889     Object key;
 890     Object value;
 891     MostRecentKeyValue(Object k, Object v) {
 892         key = k;
 893         value = v;
 894     }
 895     void setPair(Object k, Object v) {
 896         key = k;
 897         value = v;
 898     }
 899 }


  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;
  43 import java.beans.PropertyChangeSupport;
  44 import java.beans.PropertyChangeListener;
  45 import java.lang.ref.SoftReference;
  46 import sun.util.logging.PlatformLogger;
  47 import java.util.concurrent.locks.Condition;
  48 import java.util.concurrent.locks.Lock;
  49 import java.util.concurrent.locks.ReentrantLock;
  50 import java.util.concurrent.atomic.AtomicInteger;
  51 import java.util.function.Supplier;
  52 
  53 /**
  54  * The AppContext is a table referenced by ThreadGroup which stores
  55  * application service instances.  (If you are not writing an application
  56  * service, or don't know what one is, please do not use this class.)
  57  * The AppContext allows applet access to what would otherwise be
  58  * potentially dangerous services, such as the ability to peek at
  59  * EventQueues or change the look-and-feel of a Swing application.<p>
  60  *
  61  * Most application services use a singleton object to provide their
  62  * services, either as a default (such as getSystemEventQueue or
  63  * getDefaultToolkit) or as static methods with class data (System).
  64  * The AppContext works with the former method by extending the concept
  65  * of "default" to be ThreadGroup-specific.  Application services
  66  * lookup their singleton in the AppContext.<p>
  67  *
  68  * For example, here we have a Foo service, with its pre-AppContext
  69  * code:<p>
  70  * <code><pre>
  71  *    public class Foo {


 868                    // in tests for instance.
 869                    ecx = ecx != null ? ecx : getAppContext();
 870                 }
 871 
 872                 // getAppletContext() may be called when initializing the main
 873                 // app context - in which case mainAppContext will still be
 874                 // null. To work around this issue we simply use
 875                 // AppContext.threadGroup.getParent() == null instead, since
 876                 // mainAppContext is the only AppContext which should have
 877                 // the root TG as its thread group.
 878                 // See: JDK-8023258
 879                 final boolean isMainAppContext = ecx == null
 880                     || mainAppContext == ecx
 881                     || mainAppContext == null && hasRootThreadGroup(ecx);
 882 
 883                 return isMainAppContext ? null : ecx;
 884             }
 885 
 886         });
 887     }
 888 
 889     public static <T> T getSoftReferenceValue(Object key,
 890             Supplier<T> supplier) {
 891 
 892         final AppContext appContext = AppContext.getAppContext();
 893         SoftReference<T> ref = (SoftReference<T>) appContext.get(key);
 894         if (ref != null) {
 895             final T object = ref.get();
 896             if (object != null) {
 897                 return object;
 898             }
 899         }
 900         final T object = supplier.get();
 901         ref = new SoftReference<>(object);
 902         appContext.put(key, ref);
 903         return object;
 904     }
 905 }
 906 
 907 final class MostRecentKeyValue {
 908     Object key;
 909     Object value;
 910     MostRecentKeyValue(Object k, Object v) {
 911         key = k;
 912         value = v;
 913     }
 914     void setPair(Object k, Object v) {
 915         key = k;
 916         value = v;
 917     }
 918 }