Search in sources :

Example 1 with AppContext

use of sun.awt.AppContext in project jdk8u_jdk by JetBrains.

the class TestMainAppContext method main.

public static void main(String... args) throws Exception {
    ThreadGroup rootTG = Thread.currentThread().getThreadGroup();
    while (rootTG.getParent() != null) {
        rootTG = rootTG.getParent();
    }
    ThreadGroup tg = new ThreadGroup(rootTG, "FakeApplet");
    final Thread t1 = new Thread(tg, "createNewAppContext") {

        @Override
        public void run() {
            try {
                AppContext context = SunToolkit.createNewAppContext();
            } catch (Throwable t) {
                thrown = t;
            }
        }
    };
    t1.start();
    t1.join();
    if (thrown != null) {
        throw new RuntimeException("Unexpected exception: " + thrown, thrown);
    }
    Thread t2 = new Thread(tg, "BugDetector") {

        @Override
        public void run() {
            try {
                Logger.getLogger("foo").info("Done");
            } catch (Throwable x) {
                thrown = x;
            }
        }
    };
    System.setSecurityManager(new SecurityManager());
    t2.start();
    t2.join();
    if (thrown != null) {
        throw new RuntimeException("Test failed: " + thrown, thrown);
    }
}
Also used : AppContext(sun.awt.AppContext)

Example 2 with AppContext

use of sun.awt.AppContext in project jdk8u_jdk by JetBrains.

the class Main method main.

public static void main(String[] args) throws IOException {
    mainAppContext = SunToolkit.createNewAppContext();
    System.out.println("Current context class loader: " + Thread.currentThread().getContextClassLoader());
    appsThreadGroup = new ThreadGroup("MyAppsThreadGroup");
    File jar = new File("TestApp.jar");
    if (!jar.exists()) {
        System.out.println(jar.getAbsolutePath() + " was not found!\n" + "Please install the jar with test application correctly!");
        throw new RuntimeException("Test failed: no TestApp.jar");
    }
    URL[] urls = new URL[] { jar.toURL() };
    int numApps = Integer.getInteger("numApps", 20).intValue();
    doneSignal = new CountDownLatch(numApps);
    int cnt = 0;
    while (cnt++ < numApps) {
        launch(urls, "testapp.Main", "launch");
        checkErrors();
    }
    System.out.println("Wait for apps completion....");
    try {
        doneSignal.await();
    } catch (InterruptedException e) {
    }
    System.out.println("All apps finished.");
    System.gc();
    System.out.flush();
    System.out.println("Enumerate strong refs:");
    for (String is : strongRefs.keySet()) {
        System.out.println("-> " + is);
    }
    System.out.println("=======================");
    // wait few seconds
    waitAndGC(gcTimeout);
    doneSignal = new CountDownLatch(1);
    Runnable workaround = new Runnable() {

        public void run() {
            AppContext ctx = null;
            try {
                ctx = SunToolkit.createNewAppContext();
            } catch (Throwable e) {
            // ignore...
            } finally {
                doneSignal.countDown();
            }
        }
    };
    Thread wt = new Thread(appsThreadGroup, workaround, "Workaround");
    wt.setContextClassLoader(new MyClassLoader(urls, "workaround"));
    wt.start();
    wt = null;
    workaround = null;
    System.out.println("Wait for workaround completion...");
    try {
        doneSignal.await();
    } catch (InterruptedException e) {
    }
    // give a chance to GC
    waitAndGC(gcTimeout);
    if (!refs.isEmpty()) {
        System.out.println("Classloaders still alive:");
        for (MyClassLoader l : refs.keySet()) {
            String val = refs.get(l);
            if (val == null) {
                throw new RuntimeException("Test FAILED: Invalid classloader name");
            }
            System.out.println("->" + val + (strongRefs.get(val) != null ? " (has strong ref)" : ""));
            if (strongRefs.get(val) == null) {
                throw new RuntimeException("Test FAILED: exta class loader is detected! ");
            }
        }
    } else {
        System.out.println("No alive class loaders!!");
    }
    System.out.println("Test PASSED.");
}
Also used : AppContext(sun.awt.AppContext) CountDownLatch(java.util.concurrent.CountDownLatch) URL(java.net.URL) File(java.io.File)

Example 3 with AppContext

use of sun.awt.AppContext in project jdk8u_jdk by JetBrains.

the class Main method launch.

private static void launch(URL[] urls, final String className, final String methodName) {
    final String uniqClassName = "testapp/Uniq" + counter;
    final boolean saveStrongRef = forgetSomeStreams ? (counter % 5 == 4) : false;
    System.out.printf("%s: launch the app\n", uniqClassName);
    Runnable launchIt = new Runnable() {

        public void run() {
            AppContext ctx = SunToolkit.createNewAppContext();
            try {
                Class appMain = ctx.getContextClassLoader().loadClass(className);
                Method launch = appMain.getDeclaredMethod(methodName, strongRefs.getClass());
                Constructor c = appMain.getConstructor(String.class, problems.getClass());
                Object o = c.newInstance(uniqClassName, problems);
                if (saveStrongRef) {
                    System.out.printf("%s: force strong ref\n", uniqClassName);
                    launch.invoke(o, strongRefs);
                } else {
                    HashMap<String, ImageInputStream> empty = null;
                    launch.invoke(o, empty);
                }
                ctx = null;
            } catch (Throwable e) {
                problems.add(e);
            } finally {
                doneSignal.countDown();
            }
        }
    };
    MyClassLoader appClassLoader = new MyClassLoader(urls, uniqClassName);
    refs.put(appClassLoader, uniqClassName);
    Thread appThread = new Thread(appsThreadGroup, launchIt, "AppThread" + counter++);
    appThread.setContextClassLoader(appClassLoader);
    appThread.start();
    launchIt = null;
    appThread = null;
    appClassLoader = null;
}
Also used : Constructor(java.lang.reflect.Constructor) AppContext(sun.awt.AppContext) ImageInputStream(javax.imageio.stream.ImageInputStream) Method(java.lang.reflect.Method)

Example 4 with AppContext

use of sun.awt.AppContext in project adempiere by adempiere.

the class PreviewPanel method setLFSelection.

/**
	 *  Update the look list and theme list to show the current selection
	 */
private void setLFSelection() {
    m_setting = true;
    //  Search for PLAF
    ValueNamePair plaf = null;
    LookAndFeel lookFeel = UIManager.getLookAndFeel();
    String look = lookFeel.getClass().getName();
    for (int i = 0; i < AdempierePLAF.getPLAFs().length; i++) {
        ValueNamePair vp = AdempierePLAF.getPLAFs()[i];
        if (vp.getValue().equals(look)) {
            plaf = vp;
            break;
        }
    }
    if (plaf != null)
        lookList.setSelectedValue(plaf, true);
    //  Search for Theme
    MetalTheme metalTheme = null;
    ValueNamePair theme = null;
    boolean metal = UIManager.getLookAndFeel() instanceof MetalLookAndFeel;
    themeList.setModel(new DefaultComboBoxModel(AdempierePLAF.getThemes()));
    if (metal) {
        theme = null;
        AppContext context = AppContext.getAppContext();
        metalTheme = (MetalTheme) context.get("currentMetalTheme");
        if (metalTheme != null) {
            String lookTheme = metalTheme.getName();
            for (int i = 0; i < AdempierePLAF.getThemes().length; i++) {
                ValueNamePair vp = AdempierePLAF.getThemes()[i];
                if (vp.getName().equals(lookTheme)) {
                    theme = vp;
                    break;
                }
            }
        }
        if (theme != null)
            themeList.setSelectedValue(theme, true);
    }
    m_setting = false;
    log.info(lookFeel + " - " + metalTheme);
}
Also used : MetalTheme(javax.swing.plaf.metal.MetalTheme) AppContext(sun.awt.AppContext) ValueNamePair(org.compiere.util.ValueNamePair) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) MetalLookAndFeel(javax.swing.plaf.metal.MetalLookAndFeel) LookAndFeel(javax.swing.LookAndFeel) MetalLookAndFeel(javax.swing.plaf.metal.MetalLookAndFeel)

Example 5 with AppContext

use of sun.awt.AppContext in project jdk8u_jdk by JetBrains.

the class DefaultKeyboardFocusManager method sendMessage.

/**
     * Sends a synthetic AWTEvent to a Component. If the Component is in
     * the current AppContext, then the event is immediately dispatched.
     * If the Component is in a different AppContext, then the event is
     * posted to the other AppContext's EventQueue, and this method blocks
     * until the event is handled or target AppContext is disposed.
     * Returns true if successfuly dispatched event, false if failed
     * to dispatch.
     */
static boolean sendMessage(Component target, AWTEvent e) {
    e.isPosted = true;
    AppContext myAppContext = AppContext.getAppContext();
    final AppContext targetAppContext = target.appContext;
    final SentEvent se = new DefaultKeyboardFocusManagerSentEvent(e, myAppContext);
    if (myAppContext == targetAppContext) {
        se.dispatch();
    } else {
        if (targetAppContext.isDisposed()) {
            return false;
        }
        SunToolkit.postEvent(targetAppContext, se);
        if (EventQueue.isDispatchThread()) {
            EventDispatchThread edt = (EventDispatchThread) Thread.currentThread();
            edt.pumpEvents(SentEvent.ID, new Conditional() {

                public boolean evaluate() {
                    return !se.dispatched && !targetAppContext.isDisposed();
                }
            });
        } else {
            synchronized (se) {
                while (!se.dispatched && !targetAppContext.isDisposed()) {
                    try {
                        se.wait(1000);
                    } catch (InterruptedException ie) {
                        break;
                    }
                }
            }
        }
    }
    return se.dispatched;
}
Also used : AppContext(sun.awt.AppContext)

Aggregations

AppContext (sun.awt.AppContext)74 HashMap (java.util.HashMap)3 Hashtable (java.util.Hashtable)3 Locale (java.util.Locale)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 EventListenerAggregate (sun.awt.EventListenerAggregate)3 FlavorListener (java.awt.datatransfer.FlavorListener)2 WeakReference (java.lang.ref.WeakReference)2 Map (java.util.Map)2 DefaultComboBoxModel (javax.swing.DefaultComboBoxModel)2 LookAndFeel (javax.swing.LookAndFeel)2 MetalLookAndFeel (javax.swing.plaf.metal.MetalLookAndFeel)2 MetalTheme (javax.swing.plaf.metal.MetalTheme)2 ValueNamePair (org.compiere.util.ValueNamePair)2 PeerEvent (sun.awt.PeerEvent)2 AccumulativeRunnable (sun.swing.AccumulativeRunnable)2 Container (java.awt.Container)1 EventQueue (java.awt.EventQueue)1 Font (java.awt.Font)1 Rectangle (java.awt.Rectangle)1