You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2012/10/25 16:21:25 UTC

svn commit: r1402160 - /cxf/trunk/api/src/main/java/org/apache/cxf/common/logging/LogUtils.java

Author: dkulp
Date: Thu Oct 25 14:21:24 2012
New Revision: 1402160

URL: http://svn.apache.org/viewvc?rev=1402160&view=rev
Log:
[CXF-4602] Some improvements to the logger creation

Modified:
    cxf/trunk/api/src/main/java/org/apache/cxf/common/logging/LogUtils.java

Modified: cxf/trunk/api/src/main/java/org/apache/cxf/common/logging/LogUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/common/logging/LogUtils.java?rev=1402160&r1=1402159&r2=1402160&view=diff
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/common/logging/LogUtils.java (original)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/common/logging/LogUtils.java Thu Oct 25 14:21:24 2012
@@ -232,42 +232,60 @@ public final class LogUtils {
     protected static Logger createLogger(Class<?> cls, 
                                          String name, 
                                          String loggerName) {
-        if (loggerClass != null) {
-            try {
-                Constructor<?> cns = loggerClass.getConstructor(String.class, String.class);
-                if (name == null) {
-                    try {
-                        return (Logger) cns.newInstance(loggerName, BundleUtils.getBundleName(cls));
-                    } catch (InvocationTargetException ite) {
-                        if (ite.getTargetException() instanceof MissingResourceException) {
-                            return (Logger) cns.newInstance(loggerName, null);
-                        } else {
-                            throw ite;
-                        }
-                    } 
-                } else {
-                    try {
-                        return (Logger) cns.newInstance(loggerName, BundleUtils.getBundleName(cls, name));
-                    } catch (InvocationTargetException ite) {
-                        if (ite.getTargetException() instanceof MissingResourceException) {
-                            throw (MissingResourceException)ite.getTargetException();
-                        } else {
-                            throw ite;
-                        }
-                    } 
+        ClassLoader orig = Thread.currentThread().getContextClassLoader();
+        ClassLoader n = cls.getClassLoader();
+        if (n != null) {
+            Thread.currentThread().setContextClassLoader(n);
+        }
+        try {
+            if (loggerClass != null) {
+                try {
+                    Constructor<?> cns = loggerClass.getConstructor(String.class, String.class);
+                    if (name == null) {
+                        try {
+                            return (Logger) cns.newInstance(loggerName, BundleUtils.getBundleName(cls));
+                        } catch (InvocationTargetException ite) {
+                            if (ite.getTargetException() instanceof MissingResourceException) {
+                                return (Logger) cns.newInstance(loggerName, null);
+                            } else {
+                                throw ite;
+                            }
+                        } 
+                    } else {
+                        try {
+                            return (Logger) cns.newInstance(loggerName, BundleUtils.getBundleName(cls, name));
+                        } catch (InvocationTargetException ite) {
+                            if (ite.getTargetException() instanceof MissingResourceException) {
+                                throw (MissingResourceException)ite.getTargetException();
+                            } else {
+                                throw ite;
+                            }
+                        } 
+                    }
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
                 }
-            } catch (Exception e) {
-                throw new RuntimeException(e);
             }
-        }
-        if (name == null) {
-            try {
-                return Logger.getLogger(loggerName, BundleUtils.getBundleName(cls)); //NOPMD
-            } catch (MissingResourceException rex) {
-                return Logger.getLogger(loggerName, null); //NOPMD
+            if (name == null) {
+                ResourceBundle b = null;
+                try {
+                    //grab the bundle prior to the call to Logger.getLogger(...) so the 
+                    //ResourceBundle can be loaded outside the big sync block that getLogger really is
+                    b = BundleUtils.getBundle(cls);
+                    b.getLocale();
+                    return Logger.getLogger(loggerName, BundleUtils.getBundleName(cls)); //NOPMD
+                } catch (MissingResourceException rex) {
+                    return Logger.getLogger(loggerName); //NOPMD
+                } finally {
+                    b = null;
+                }
+            } else {
+                return Logger.getLogger(loggerName, BundleUtils.getBundleName(cls, name)); //NOPMD
+            }
+        } finally {
+            if (n != orig) {
+                Thread.currentThread().setContextClassLoader(orig);
             }
-        } else {
-            return Logger.getLogger(loggerName, BundleUtils.getBundleName(cls, name)); //NOPMD
         }
     }