changeset 5918:b4c36757fe45

Merge
author sjiang
date Tue, 19 Mar 2013 11:18:45 -0700
parents be8500a31d10 846304f476f1
children 3079d056c9bd
files
diffstat 5 files changed, 125 insertions(+), 89 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/logging/LogManager.java	Tue Mar 19 18:45:19 2013 +0100
+++ b/src/share/classes/java/util/logging/LogManager.java	Tue Mar 19 11:18:45 2013 -0700
@@ -354,27 +354,23 @@
                 // from the execution stack.
                 Object ecx = javaAwtAccess.getExecutionContext();
                 if (ecx == null) {
-                    // fall back to AppContext.getAppContext()
+                    // fall back to thread group seach of AppContext
                     ecx = javaAwtAccess.getContext();
                 }
-                context = (LoggerContext)javaAwtAccess.get(ecx, LoggerContext.class);
-                if (context == null) {
-                    if (javaAwtAccess.isMainAppContext()) {
-                        context = userContext;
-                    } else {
-                        context = new LoggerContext();
-                        // during initialization, rootLogger is null when
-                        // instantiating itself RootLogger
-                        if (manager.rootLogger != null)
-                            context.addLocalLogger(manager.rootLogger);
+                if (ecx != null) {
+                    context = (LoggerContext)javaAwtAccess.get(ecx, LoggerContext.class);
+                    if (context == null) {
+                        if (javaAwtAccess.isMainAppContext()) {
+                            context = userContext;
+                        } else {
+                            context = new LoggerContext();
+                        }
+                        javaAwtAccess.put(ecx, LoggerContext.class, context);
                     }
-                    javaAwtAccess.put(ecx, LoggerContext.class, context);
                 }
             }
-        } else {
-            context = userContext;
         }
-        return context;
+        return context != null ? context : userContext;
     }
 
     private List<LoggerContext> contexts() {
@@ -499,9 +495,22 @@
             return logger;
         }
 
+        synchronized void ensureRootLogger(Logger logger) {
+            if (logger == manager.rootLogger)
+                return;
+
+            // during initialization, rootLogger is null when
+            // instantiating itself RootLogger
+            if (findLogger("") == null && manager.rootLogger != null) {
+                addLocalLogger(manager.rootLogger);
+            }
+        }
+
         // Add a logger to this context.  This method will only set its level
         // and process parent loggers.  It doesn't set its handlers.
         synchronized boolean addLocalLogger(Logger logger) {
+            ensureRootLogger(logger);
+
             final String name = logger.getName();
             if (name == null) {
                 throw new NullPointerException();
--- a/src/share/classes/sun/applet/AppletSecurity.java	Tue Mar 19 18:45:19 2013 +0100
+++ b/src/share/classes/sun/applet/AppletSecurity.java	Tue Mar 19 11:18:45 2013 -0700
@@ -52,7 +52,6 @@
  */
 public
 class AppletSecurity extends AWTSecurityManager {
-    private AppContext mainAppContext;
 
     //URLClassLoader.acc
     private static Field facc = null;
@@ -77,7 +76,6 @@
      */
     public AppletSecurity() {
         reset();
-        mainAppContext = AppContext.getAppContext();
     }
 
     // Cache to store known restricted packages
@@ -312,7 +310,7 @@
         AppContext appContext = AppContext.getAppContext();
         AppletClassLoader appletClassLoader = currentAppletClassLoader();
 
-        if ((appContext == mainAppContext) && (appletClassLoader != null)) {
+        if (AppContext.isMainContext(appContext) && (appletClassLoader != null)) {
             // If we're about to allow access to the main EventQueue,
             // and anything untrusted is on the class context stack,
             // disallow access.
--- a/src/share/classes/sun/awt/AppContext.java	Tue Mar 19 18:45:19 2013 +0100
+++ b/src/share/classes/sun/awt/AppContext.java	Tue Mar 19 11:18:45 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -46,6 +46,7 @@
 import java.util.concurrent.locks.Condition;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * The AppContext is a table referenced by ThreadGroup which stores
@@ -161,8 +162,9 @@
     }
 
     /* The main "system" AppContext, used by everything not otherwise
-       contained in another AppContext.
-     */
+       contained in another AppContext. It is implicitly created for
+       standalone apps only (i.e. not applets)
+    */
     private static volatile AppContext mainAppContext = null;
 
     /*
@@ -194,27 +196,6 @@
         return isDisposed;
     }
 
-    static {
-        // On the main Thread, we get the ThreadGroup, make a corresponding
-        // AppContext, and instantiate the Java EventQueue.  This way, legacy
-        // code is unaffected by the move to multiple AppContext ability.
-        AccessController.doPrivileged(new PrivilegedAction() {
-            public Object run() {
-                ThreadGroup currentThreadGroup =
-                        Thread.currentThread().getThreadGroup();
-                ThreadGroup parentThreadGroup = currentThreadGroup.getParent();
-                while (parentThreadGroup != null) {
-                    // Find the root ThreadGroup to construct our main AppContext
-                    currentThreadGroup = parentThreadGroup;
-                    parentThreadGroup = currentThreadGroup.getParent();
-                }
-                mainAppContext = new AppContext(currentThreadGroup);
-                numAppContexts = 1;
-                return mainAppContext;
-            }
-        });
-    }
-
     /*
      * The total number of AppContexts, system-wide.  This number is
      * incremented at the beginning of the constructor, and decremented
@@ -222,7 +203,7 @@
      * number is 1.  If so, it returns the sole AppContext without
      * checking Thread.currentThread().
      */
-    private static volatile int numAppContexts;
+    private static final AtomicInteger numAppContexts = new AtomicInteger(0);
 
     /*
      * The context ClassLoader that was used to create this AppContext.
@@ -243,7 +224,7 @@
      * @since   1.2
      */
     AppContext(ThreadGroup threadGroup) {
-        numAppContexts++;
+        numAppContexts.incrementAndGet();
 
         this.threadGroup = threadGroup;
         threadGroup2appContext.put(threadGroup, this);
@@ -266,6 +247,27 @@
     private static final ThreadLocal<AppContext> threadAppContext =
             new ThreadLocal<AppContext>();
 
+    private final static void initMainAppContext() {
+        // On the main Thread, we get the ThreadGroup, make a corresponding
+        // AppContext, and instantiate the Java EventQueue.  This way, legacy
+        // code is unaffected by the move to multiple AppContext ability.
+        AccessController.doPrivileged(new PrivilegedAction<Void>() {
+            public Void run() {
+                ThreadGroup currentThreadGroup =
+                        Thread.currentThread().getThreadGroup();
+                ThreadGroup parentThreadGroup = currentThreadGroup.getParent();
+                while (parentThreadGroup != null) {
+                    // Find the root ThreadGroup to construct our main AppContext
+                    currentThreadGroup = parentThreadGroup;
+                    parentThreadGroup = currentThreadGroup.getParent();
+                }
+
+                mainAppContext = SunToolkit.createNewAppContext(currentThreadGroup);
+                return null;
+            }
+        });
+    }
+
     /**
      * Returns the appropriate AppContext for the caller,
      * as determined by its ThreadGroup.  If the main "system" AppContext
@@ -278,8 +280,10 @@
      * @since   1.2
      */
     public final static AppContext getAppContext() {
-        if (numAppContexts == 1)   // If there's only one system-wide,
-            return mainAppContext; // return the main system AppContext.
+        // we are standalone app, return the main app context
+        if (numAppContexts.get() == 1 && mainAppContext != null) {
+            return mainAppContext;
+        }
 
         AppContext appContext = threadAppContext.get();
 
@@ -293,29 +297,37 @@
                     // when new AppContext objects are created.
                     ThreadGroup currentThreadGroup = Thread.currentThread().getThreadGroup();
                     ThreadGroup threadGroup = currentThreadGroup;
+
+                    // Special case: we implicitly create the main app context
+                    // if no contexts have been created yet. This covers standalone apps
+                    // and excludes applets because by the time applet starts
+                    // a number of contexts have already been created by the plugin.
+                    if (numAppContexts.get() == 0) {
+                        // This check is not necessary, its purpose is to help
+                        // Plugin devs to catch all the cases of main AC creation.
+                        if (System.getProperty("javaplugin.version") == null &&
+                                System.getProperty("javawebstart.version") == null) {
+                            initMainAppContext();
+                        }
+                    }
+
                     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");
+                            return null;
                         }
                         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)
                     threadAppContext.set(context);
 
                     return context;
@@ -323,17 +335,18 @@
             });
         }
 
-        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.
-            AppContext secAppContext = getExecutionAppContext();
-            if (secAppContext != null) {
-                appContext = secAppContext; // Return what we're told
-            }
-        }
+        return appContext;
+    }
 
-        return appContext;
+    /**
+     * Returns true if the specified AppContext is the main AppContext.
+     *
+     * @param   ctx the context to compare with the main context
+     * @return  true if the specified AppContext is the main AppContext.
+     * @since   1.8
+     */
+    public final static boolean isMainContext(AppContext ctx) {
+        return (ctx != null && ctx == mainAppContext);
     }
 
     private final static AppContext getExecutionAppContext() {
@@ -348,16 +361,6 @@
         return null;
     }
 
-    /**
-     * Returns the main ("system") AppContext.
-     *
-     * @return  the main AppContext
-     * @since   1.8
-     */
-    final static AppContext getMainAppContext() {
-        return mainAppContext;
-    }
-
     private long DISPOSAL_TIMEOUT = 5000;  // Default to 5-second timeout
                                            // for disposal of all Frames
                                            // (we wait for this time twice,
@@ -519,7 +522,7 @@
             this.table.clear(); // Clear out the Hashtable to ease garbage collection
         }
 
-        numAppContexts--;
+        numAppContexts.decrementAndGet();
 
         mostRecentKeyValue = null;
     }
@@ -810,7 +813,7 @@
                 return getAppContext().isDisposed();
             }
             public boolean isMainAppContext() {
-                return (numAppContexts == 1);
+                return (numAppContexts.get() == 1 && mainAppContext != null);
             }
             public Object getContext() {
                 return getAppContext();
--- a/src/share/classes/sun/awt/SunToolkit.java	Tue Mar 19 18:45:19 2013 +0100
+++ b/src/share/classes/sun/awt/SunToolkit.java	Tue Mar 19 11:18:45 2013 -0700
@@ -117,8 +117,6 @@
     }
 
     public SunToolkit() {
-        // 7122796: Always create an EQ for the main AppContext
-        initEQ(AppContext.getMainAppContext());
     }
 
     public boolean useBufferPerWindow() {
@@ -277,11 +275,14 @@
      */
     public static AppContext createNewAppContext() {
         ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
+        return createNewAppContext(threadGroup);
+    }
+
+    static final AppContext createNewAppContext(ThreadGroup threadGroup) {
         // Create appContext before initialization of EventQueue, so all
         // the calls to AppContext.getAppContext() from EventQueue ctor
         // return correct values
         AppContext appContext = new AppContext(threadGroup);
-
         initEQ(appContext);
 
         return appContext;
--- a/test/sun/misc/Version/Version.java	Tue Mar 19 18:45:19 2013 +0100
+++ b/test/sun/misc/Version/Version.java	Tue Mar 19 11:18:45 2013 -0700
@@ -35,7 +35,7 @@
 public class Version {
 
     public static void main(String[] args) throws Exception {
-        VersionInfo jdk = newVersionInfo(System.getProperty("java.runtime.version"));
+        VersionInfo jdk = jdkVersionInfo(System.getProperty("java.runtime.version"));
         VersionInfo v1 = new VersionInfo(jdkMajorVersion(),
                                          jdkMinorVersion(),
                                          jdkMicroVersion(),
@@ -46,7 +46,7 @@
         if (!jdk.equals(v1)) {
             throw new RuntimeException("Unmatched version: " + jdk + " vs " + v1);
         }
-        VersionInfo jvm = newVersionInfo(System.getProperty("java.vm.version"));
+        VersionInfo jvm = jvmVersionInfo(System.getProperty("java.vm.version"));
         VersionInfo v2 = new VersionInfo(jvmMajorVersion(),
                                          jvmMinorVersion(),
                                          jvmMicroVersion(),
@@ -97,9 +97,9 @@
         }
     }
 
-    private static VersionInfo newVersionInfo(String version) throws Exception {
+    private static VersionInfo jdkVersionInfo(String version) throws Exception {
         // valid format of the version string is:
-        // n.n.n[_uu[c]][-<identifer>]-bxx
+        // <major>.<minor>[.<micro>][_uu[c]][-<identifier>]-bxx
         int major = 0;
         int minor = 0;
         int micro = 0;
@@ -107,8 +107,7 @@
         String special = "";
         int build = 0;
 
-        // n.n.n[_uu[c]][-<identifer>]-bxx
-        String regex = "([0-9]{1,2})";      // major
+        String regex = "^([0-9]{1,2})";     // major
         regex += "\\.";                     // separator
         regex += "([0-9]{1,2})";            // minor
         regex += "(\\.";                    // separator
@@ -118,8 +117,8 @@
         regex +=   "([0-9]{2})";            // update
         regex +=   "([a-z])?";              // special char (optional)
         regex += ")?";                      // _uu[c] is optional
-        regex += "(\\-[a-z]{2,})?";         // -<identifier>
-        regex += "(\\-b([0-9]{1,2}))?";     // -bxx
+        regex += ".*";                      // -<identifier>
+        regex += "(\\-b([0-9]{1,3}$))";     // JDK -bxx
 
         Pattern p = Pattern.compile(regex);
         Matcher m = p.matcher(version);
@@ -130,10 +129,36 @@
         micro = (m.group(4) == null) ? 0 : Integer.parseInt(m.group(4));
         update = (m.group(6) == null) ? 0 : Integer.parseInt(m.group(6));
         special = (m.group(7) == null) ? "" : m.group(7);
-        build = Integer.parseInt(m.group(10));
+        build = Integer.parseInt(m.group(9));
 
         VersionInfo vi = new VersionInfo(major, minor, micro, update, special, build);
         System.out.printf("newVersionInfo: input=%s output=%s\n", version, vi);
         return vi;
     }
+
+    private static VersionInfo jvmVersionInfo(String version) throws Exception {
+        // valid format of the version string is:
+        // <major>.<minor>-bxx[-<identifier>][-<debug_flavor>]
+        int major = 0;
+        int minor = 0;
+        int build = 0;
+
+        String regex = "^([0-9]{1,2})";     // major
+        regex += "\\.";                     // separator
+        regex += "([0-9]{1,2})";            // minor
+        regex += "(\\-b([0-9]{1,3}))";      // JVM -bxx
+        regex += ".*";
+
+        Pattern p = Pattern.compile(regex);
+        Matcher m = p.matcher(version);
+        m.matches();
+
+        major = Integer.parseInt(m.group(1));
+        minor = Integer.parseInt(m.group(2));
+        build = Integer.parseInt(m.group(4));
+
+        VersionInfo vi = new VersionInfo(major, minor, 0, 0, "", build);
+        System.out.printf("newVersionInfo: input=%s output=%s\n", version, vi);
+        return vi;
+    }
 }