logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1452151 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java src/changes/changes.xml
Date Sun, 03 Mar 2013 23:05:00 GMT
Author: rgoers
Date: Sun Mar  3 23:05:00 2013
New Revision: 1452151

URL: http://svn.apache.org/r1452151
Log:
LOG4J2-169 - ConfigurationFactory was adding factories on every call.

Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java?rev=1452151&r1=1452150&r2=1452151&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
Sun Mar  3 23:05:00 2013
@@ -34,6 +34,7 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -87,7 +88,7 @@ public abstract class ConfigurationFacto
      */
     protected static final String DEFAULT_PREFIX = "log4j2";
 
-    private static List<ConfigurationFactory> factories = new ArrayList<ConfigurationFactory>();
+    private static volatile List<ConfigurationFactory> factories = null;
 
     private static ConfigurationFactory configFactory = new Factory();
 
@@ -96,36 +97,45 @@ public abstract class ConfigurationFacto
      * @return the ConfigurationFactory.
      */
     public static ConfigurationFactory getInstance() {
-        final String factoryClass = PropertiesUtil.getProperties().getStringProperty(CONFIGURATION_FACTORY_PROPERTY);
-        if (factoryClass != null) {
-            addFactory(factoryClass);
-        }
-        final PluginManager manager = new PluginManager("ConfigurationFactory");
-        manager.collectPlugins();
-        final Map<String, PluginType> plugins = manager.getPlugins();
-        final Set<WeightedFactory> ordered = new TreeSet<WeightedFactory>();
-        for (final PluginType type : plugins.values()) {
-            try {
-                final Class<ConfigurationFactory> clazz = type.getPluginClass();
-                final Order o = clazz.getAnnotation(Order.class);
-                final Integer weight = o.value();
-                if (o != null) {
-                    ordered.add(new WeightedFactory(weight, clazz));
+        if (factories == null) {
+            synchronized(TEST_PREFIX) {
+                if (factories == null) {
+                    List<ConfigurationFactory> list = new ArrayList<ConfigurationFactory>();
+                    final String factoryClass = PropertiesUtil.getProperties().getStringProperty(CONFIGURATION_FACTORY_PROPERTY);
+                    if (factoryClass != null) {
+                        addFactory(list, factoryClass);
+                    }
+                    final PluginManager manager = new PluginManager("ConfigurationFactory");
+                    manager.collectPlugins();
+                    final Map<String, PluginType> plugins = manager.getPlugins();
+                    final Set<WeightedFactory> ordered = new TreeSet<WeightedFactory>();
+                    for (final PluginType type : plugins.values()) {
+                        try {
+                            final Class<ConfigurationFactory> clazz = type.getPluginClass();
+                            final Order o = clazz.getAnnotation(Order.class);
+                            final Integer weight = o.value();
+                            if (o != null) {
+                                ordered.add(new WeightedFactory(weight, clazz));
+                            }
+                        } catch (final Exception ex) {
+                            LOGGER.warn("Unable to add class " + type.getPluginClass());
+                        }
+                    }
+                    for (final WeightedFactory wf : ordered) {
+                        addFactory(list, wf.factoryClass);
+                    }
+                    factories = Collections.unmodifiableList(list);
                 }
-            } catch (final Exception ex) {
-              LOGGER.warn("Unable to add class " + type.getPluginClass());
             }
         }
-        for (final WeightedFactory wf : ordered) {
-            addFactory(wf.factoryClass);
-        }
+
         return configFactory;
     }
 
     @SuppressWarnings("unchecked")
-    private static void addFactory(final String factoryClass) {
+    private static void addFactory(final List<ConfigurationFactory> list, final String
factoryClass) {
         try {
-            addFactory((Class<ConfigurationFactory>) Class.forName(factoryClass));
+            addFactory(list, (Class<ConfigurationFactory>) Class.forName(factoryClass));
         } catch (final ClassNotFoundException ex) {
             LOGGER.error("Unable to load class " + factoryClass, ex);
         } catch (final Exception ex) {
@@ -133,16 +143,17 @@ public abstract class ConfigurationFacto
         }
     }
 
-    private static void addFactory(final Class<ConfigurationFactory> factoryClass)
{
+    private static void addFactory(final List<ConfigurationFactory> list,
+                                   final Class<ConfigurationFactory> factoryClass)
{
         try {
-            factories.add(factoryClass.newInstance());
+            list.add(factoryClass.newInstance());
         } catch (final Exception ex) {
             LOGGER.error("Unable to create instance of " + factoryClass.getName(), ex);
         }
     }
 
     /**
-     * Set the configuration factory.
+     * Set the configuration factory. This method is not intended for general use and may
not be thread safe.
      * @param factory the ConfigurationFactory.
      */
     public static void setConfigurationFactory(final ConfigurationFactory factory) {
@@ -150,18 +161,21 @@ public abstract class ConfigurationFacto
     }
 
     /**
-     * Reset the ConfigurationFactory to the default.
+     * Reset the ConfigurationFactory to the default. This method is not intended for general
use and may
+     * not be thread safe.
      */
     public static void resetConfigurationFactory() {
         configFactory = new Factory();
     }
 
     /**
-     * Remove the ConfigurationFactory.
+     * Remove the ConfigurationFactory. This method is not intended for general use and may
not be thread safe.
      * @param factory The factory to remove.
      */
     public static void removeConfigurationFactory(final ConfigurationFactory factory) {
-        factories.remove(factory);
+        if (configFactory == factory) {
+            configFactory = new Factory();
+        }
     }
 
     protected abstract String[] getSupportedTypes();

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1452151&r1=1452150&r2=1452151&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sun Mar  3 23:05:00 2013
@@ -23,7 +23,10 @@
 
   <body>
     <release version="2.0-beta5" date="@TBD@" description="Bug fixes and enhancements">
-      <action issue="LOG4J2-161" dev="rgoers">
+      <action issue="LOG4J2-169" dev="rgoers" type="fix">
+        ConfigurationFactory was adding factories on every call.
+      </action>
+      <action issue="LOG4J2-161" dev="rgoers" type="fix">
         Modify ClassLoaderContextSelector to use the first ClassLoader in the child parent
hierarchy that
         has a Context with a configuration to allow JSPs to use the WebApp's context and
configuration.
       </action>



Mime
View raw message