changeset 5793:10bf0ec57a6e

8007393: Possible race condition after JDK-6664509 Reviewed-by: alanb, jgish
author mchung
date Tue, 05 Feb 2013 22:56:47 -0800
parents 835448d525a1
children 58602549247e
files src/share/classes/java/util/logging/LogManager.java
diffstat 1 files changed, 34 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/logging/LogManager.java	Fri Feb 01 23:29:46 2013 +0100
+++ b/src/share/classes/java/util/logging/LogManager.java	Tue Feb 05 22:56:47 2013 -0800
@@ -423,7 +423,40 @@
     }
 
     Logger demandSystemLogger(String name, String resourceBundleName) {
-        return systemContext.demandLogger(name, resourceBundleName);
+        // Add a system logger in the system context's namespace
+        final Logger sysLogger = systemContext.demandLogger(name, resourceBundleName);
+
+        // Add the system logger to the LogManager's namespace if not exist
+        // so that there is only one single logger of the given name.
+        // System loggers are visible to applications unless a logger of
+        // the same name has been added.
+        Logger logger;
+        do {
+            // First attempt to call addLogger instead of getLogger
+            // This would avoid potential bug in custom LogManager.getLogger
+            // implementation that adds a logger if does not exist
+            if (addLogger(sysLogger)) {
+                // successfully added the new system logger
+                logger = sysLogger;
+            } else {
+                logger = getLogger(name);
+            }
+        } while (logger == null);
+
+        // LogManager will set the sysLogger's handlers via LogManager.addLogger method.
+        if (logger != sysLogger && sysLogger.getHandlers().length == 0) {
+            // if logger already exists but handlers not set
+            final Logger l = logger;
+            AccessController.doPrivileged(new PrivilegedAction<Void>() {
+                public Void run() {
+                    for (Handler hdl : l.getHandlers()) {
+                        sysLogger.addHandler(hdl);
+                    }
+                    return null;
+                }
+            });
+        }
+        return sysLogger;
     }
 
     // LoggerContext maintains the logger namespace per context.
@@ -628,21 +661,6 @@
                     }
                 } while (result == null);
             }
-            // Add the system logger to the LogManager's namespace if not exists
-            // The LogManager will set its handlers via the LogManager.addLogger method.
-            if (!manager.addLogger(result) && result.getHandlers().length == 0) {
-                // if logger already exists but handlers not set
-                final Logger l = manager.getLogger(name);
-                final Logger logger = result;
-                AccessController.doPrivileged(new PrivilegedAction<Void>() {
-                    public Void run() {
-                        for (Handler hdl : l.getHandlers()) {
-                            logger.addHandler(hdl);
-                        }
-                        return null;
-                    }
-                });
-            }
             return result;
         }
     }