logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1521753 - in /logging/log4j/log4j2/trunk: log4j-core/src/main/java/org/apache/logging/log4j/core/config/ src/changes/
Date Wed, 11 Sep 2013 07:41:47 GMT
Author: rgoers
Date: Wed Sep 11 07:41:46 2013
New Revision: 1521753

URL: http://svn.apache.org/r1521753
Log:
LOG4J2-393 - Initialize PluginManager once during configuration. Move advertisement setup
into BaseConfiguration.

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java?rev=1521753&r1=1521752&r2=1521753&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
Wed Sep 11 07:41:46 2013
@@ -23,6 +23,7 @@ import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -85,7 +86,13 @@ public class BaseConfiguration extends A
     /**
      * The Advertiser which exposes appender configurations to external systems.
      */
-    protected Advertiser advertiser = new DefaultAdvertiser();
+    private Advertiser advertiser = new DefaultAdvertiser();
+
+    protected Map<String, String> advertisedConfiguration;
+
+    private Node advertiserNode = null;
+
+    private Object advertisement;
 
     /**
      *
@@ -108,10 +115,13 @@ public class BaseConfiguration extends A
 
     private final ConcurrentMap<String, Object> componentMap = new ConcurrentHashMap<String,
Object>();
 
+    protected PluginManager pluginManager;
+
     /**
      * Constructor.
      */
     protected BaseConfiguration() {
+        pluginManager = new PluginManager("Core");
         rootNode = new Node();
     }
 
@@ -126,7 +136,9 @@ public class BaseConfiguration extends A
      */
     @Override
     public void start() {
+        pluginManager.collectPlugins();
         setup();
+        setupAdvertisement();
         doConfigure();
         for (final LoggerConfig logger : loggers.values()) {
             logger.startFilter();
@@ -154,6 +166,10 @@ public class BaseConfiguration extends A
         }
         root.stopFilter();
         stopFilter();
+        if (advertiser != null && advertisement != null)
+        {
+            advertiser.unadvertise(advertisement);
+        }
     }
 
     @Override
@@ -174,6 +190,42 @@ public class BaseConfiguration extends A
         }
     }
 
+    protected void createAdvertiser(String advertiserString, ConfigurationFactory.ConfigurationSource
configSource,
+                                    byte[] buffer, String contentType) {
+        if (advertiserString != null) {
+            Node node = new Node(null, advertiserString, null);
+            Map<String, String> attributes = node.getAttributes();
+            attributes.put("content", new String(buffer));
+            attributes.put("contentType", contentType);
+            attributes.put("name", "configuration");
+            if (configSource.getLocation() != null) {
+                attributes.put("location", configSource.getLocation());
+            }
+            advertiserNode = node;
+        }
+    }
+
+    private void setupAdvertisement() {
+        if (advertiserNode != null)
+        {
+            String name = advertiserNode.getName();
+            @SuppressWarnings("unchecked")
+            final PluginType<Advertiser> type = (PluginType<Advertiser>) pluginManager.getPluginType(name);
+            if (type != null)
+            {
+                final Class<Advertiser> clazz = type.getPluginClass();
+                try {
+                    advertiser = clazz.newInstance();
+                    advertisement = advertiser.advertise(advertiserNode.getAttributes());
+                } catch (final InstantiationException e) {
+                    System.err.println("InstantiationException attempting to instantiate
advertiser: " + name);
+                } catch (final IllegalAccessException e) {
+                    System.err.println("IllegalAccessException attempting to instantiate
advertiser: " + name);
+                }
+            }
+        }
+    }
+
     @Override
     public Object getComponent(final String name) {
         return componentMap.get(name);
@@ -267,14 +319,6 @@ public class BaseConfiguration extends A
         root.setLevel(level);
     }
 
-    protected PluginManager getPluginManager() {
-        //don't cache a pluginmanager instance - packages may be updated, requiring
-        // re-discovery of plugins
-        final PluginManager mgr = new PluginManager("Core");
-        mgr.collectPlugins();
-        return mgr;
-    }
-
     /**
      * Set the name of the configuration.
      * @param name The name.

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java?rev=1521753&r1=1521752&r2=1521753&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
Wed Sep 11 07:41:46 2013
@@ -29,7 +29,6 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -39,7 +38,6 @@ import org.apache.logging.log4j.core.con
 import org.apache.logging.log4j.core.config.plugins.PluginType;
 import org.apache.logging.log4j.core.config.plugins.ResolverUtil;
 import org.apache.logging.log4j.core.helpers.FileUtils;
-import org.apache.logging.log4j.core.net.Advertiser;
 import org.apache.logging.log4j.status.StatusConsoleListener;
 import org.apache.logging.log4j.status.StatusListener;
 import org.apache.logging.log4j.status.StatusLogger;
@@ -59,10 +57,6 @@ public class JSONConfiguration extends B
 
     private final List<Status> status = new ArrayList<Status>();
 
-    private Map<String, String> advertisedConfiguration;
-
-    private Object advertisement;
-
     private JsonNode root;
 
     private final List<String> messages = new ArrayList<String>();
@@ -128,25 +122,8 @@ public class JSONConfiguration extends B
                         monitor = new FileConfigurationMonitor(this, configFile, listeners,
interval);
                     }
                 } else if ("advertiser".equalsIgnoreCase(entry.getKey())) {
-                    final String advertiserString = getStrSubstitutor().replace(entry.getValue());
-                    if (advertiserString != null)
-                    {
-                        @SuppressWarnings("unchecked")
-                        final PluginType<Advertiser> type = (PluginType<Advertiser>)
getPluginManager().getPluginType(advertiserString);
-                        if (type != null)
-                        {
-                            final Class<Advertiser> clazz = type.getPluginClass();
-                            advertiser = clazz.newInstance();
-                            advertisedConfiguration = new HashMap<String, String>();
-                            advertisedConfiguration.put("content", new String(buffer));
-                            advertisedConfiguration.put("contentType", "application/json");
-                            advertisedConfiguration.put("name", "configuration");
-                            if (configSource.getLocation() != null)
-                            {
-                                advertisedConfiguration.put("location", configSource.getLocation());
-                            }
-                        }
-                    }
+                    createAdvertiser(getStrSubstitutor().replace(entry.getValue()), configSource,
buffer,
+                        "application/json");
                 }
             }
 
@@ -184,10 +161,6 @@ public class JSONConfiguration extends B
     @Override
     public void stop() {
         super.stop();
-        if (advertiser != null && advertisement != null)
-        {
-            advertiser.unadvertise(advertisement);
-        }
     }
 
     @Override
@@ -211,10 +184,6 @@ public class JSONConfiguration extends B
             }
             return;
         }
-        if (advertiser != null && advertisedConfiguration != null)
-        {
-            advertisement = advertiser.advertise(advertisedConfiguration);
-        }
     }
 
     @Override
@@ -232,7 +201,7 @@ public class JSONConfiguration extends B
     }
 
     private Node constructNode(final String name, final Node parent, final JsonNode jsonNode)
{
-        final PluginType<?> type = getPluginManager().getPluginType(name);
+        final PluginType<?> type = pluginManager.getPluginType(name);
         final Node node = new Node(parent, name, type);
         processAttributes(node, jsonNode);
         final Iterator<Map.Entry<String, JsonNode>> iter = jsonNode.fields();
@@ -248,7 +217,7 @@ public class JSONConfiguration extends B
                     LOGGER.debug("Processing node for array " + entry.getKey());
                     for (int i = 0; i < n.size(); ++i) {
                         final String pluginType = getType(n.get(i), entry.getKey());
-                        final PluginType<?> entryType = getPluginManager().getPluginType(pluginType);
+                        final PluginType<?> entryType = pluginManager.getPluginType(pluginType);
                         final Node item = new Node(node, entry.getKey(), entryType);
                         processAttributes(item, n.get(i));
                         if (pluginType.equals(entry.getKey())) {

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java?rev=1521753&r1=1521752&r2=1521753&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
Wed Sep 11 07:41:46 2013
@@ -29,7 +29,6 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -49,7 +48,6 @@ import org.apache.logging.log4j.core.con
 import org.apache.logging.log4j.core.config.plugins.PluginType;
 import org.apache.logging.log4j.core.config.plugins.ResolverUtil;
 import org.apache.logging.log4j.core.helpers.FileUtils;
-import org.apache.logging.log4j.core.net.Advertiser;
 import org.apache.logging.log4j.status.StatusConsoleListener;
 import org.apache.logging.log4j.status.StatusListener;
 import org.apache.logging.log4j.status.StatusLogger;
@@ -71,6 +69,22 @@ public class XMLConfiguration extends Ba
 
     private static final String XINCLUDE_FIXUP_BASE_URIS = "http://apache.org/xml/features/xinclude/fixup-base-uris";
 
+    private static final String[] VERBOSE_CLASSES = new String[] {ResolverUtil.class.getName()};
+
+    private static final String LOG4J_XSD = "Log4j-config.xsd";
+
+    private static final int BUF_SIZE = 16384;
+
+    private final List<Status> status = new ArrayList<Status>();
+
+    private Element rootElement;
+
+    private boolean strict;
+
+    private String schema;
+
+    private final File configFile;
+
     /**
      * Creates a new DocumentBuilder suitable for parsing a configuration file.
      *
@@ -81,15 +95,13 @@ public class XMLConfiguration extends Ba
         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         factory.setNamespaceAware(true);
         enableXInclude(factory);
-        final DocumentBuilder builder = factory.newDocumentBuilder();
-        return builder;
+        return factory.newDocumentBuilder();
     }
 
     /**
      * Enables XInclude for the given DocumentBuilderFactory
      *
-     * @param factory
-     *            a DocumentBuilderFactory
+     * @param factory a DocumentBuilderFactory
      * @throws ParserConfigurationException
      */
     private static void enableXInclude(final DocumentBuilderFactory factory) {
@@ -116,35 +128,12 @@ public class XMLConfiguration extends Ba
         }
     }
 
-    private static final String[] VERBOSE_CLASSES = new String[] {ResolverUtil.class.getName()};
-
-    private static final String LOG4J_XSD = "Log4j-config.xsd";
-
-    private static final int BUF_SIZE = 16384;
-
-    private final List<Status> status = new ArrayList<Status>();
-
-    private Map<String, String> advertisedConfiguration;
-
-    private Object advertisement;
-
-    private Element rootElement;
-
-    private boolean strict;
-
-    private String schema;
-
-    private Validator validator;
-
-    private final List<String> messages = new ArrayList<String>();
-
-    private final File configFile;
-
     public XMLConfiguration(final ConfigurationFactory.ConfigurationSource configSource)
{
         this.configFile = configSource.getFile();
         byte[] buffer = null;
 
         try {
+            final List<String> messages = new ArrayList<String>();
             final InputStream configStream = configSource.getInputStream();
             buffer = toByteArray(configStream);
             configStream.close();
@@ -201,31 +190,7 @@ public class XMLConfiguration extends Ba
                         monitor = new FileConfigurationMonitor(this, configFile, listeners,
interval);
                     }
                 } else if ("advertiser".equalsIgnoreCase(entry.getKey())) {
-                    final String advertiserString = getStrSubstitutor().replace(entry.getValue());
-                    if (advertiserString != null)
-                    {
-                        @SuppressWarnings("unchecked")
-                        final PluginType<Advertiser> type = (PluginType<Advertiser>)
getPluginManager().getPluginType(advertiserString);
-                        if (type != null)
-                        {
-                            final Class<Advertiser> clazz = type.getPluginClass();
-                            try {
-                                advertiser = clazz.newInstance();
-                                advertisedConfiguration = new HashMap<String, String>();
-                                advertisedConfiguration.put("content", new String(buffer));
-                                advertisedConfiguration.put("contentType", "text/xml");
-                                advertisedConfiguration.put("name", "configuration");
-                                if (configSource.getLocation() != null)
-                                {
-                                    advertisedConfiguration.put("location", configSource.getLocation());
-                                }
-                            } catch (final InstantiationException e) {
-                                System.err.println("InstantiationException attempting to
instantiate advertiser: " + advertiserString);
-                            } catch (final IllegalAccessException e) {
-                                System.err.println("IllegalAccessException attempting to
instantiate advertiser: " + advertiserString);
-                            }
-                        }
-                    }
+                    createAdvertiser(getStrSubstitutor().replace(entry.getValue()), configSource,
buffer, "text/xml");
                 }
             }
             final Iterator<StatusListener> iter = ((StatusLogger) LOGGER).getListeners();
@@ -275,7 +240,7 @@ public class XMLConfiguration extends Ba
                     LOGGER.error("Error parsing Log4j schema", ex);
                 }
                 if (schema != null) {
-                    validator = schema.newValidator();
+                    Validator validator = schema.newValidator();
                     try {
                         validator.validate(new StreamSource(new ByteArrayInputStream(buffer)));
                     } catch (final IOException ioe) {
@@ -293,15 +258,6 @@ public class XMLConfiguration extends Ba
     }
 
     @Override
-    public void stop() {
-        super.stop();
-        if (advertiser != null && advertisement != null)
-        {
-            advertiser.unadvertise(advertisement);
-        }
-    }
-
-    @Override
     public void setup() {
         if (rootElement == null) {
             LOGGER.error("No logging configuration");
@@ -315,10 +271,6 @@ public class XMLConfiguration extends Ba
             return;
         }
         rootElement = null;
-        if (advertiser != null && advertisedConfiguration != null)
-        {
-            advertisement = advertiser.advertise(advertisedConfiguration);
-        }
     }
 
     @Override
@@ -345,8 +297,7 @@ public class XMLConfiguration extends Ba
             if (w3cNode instanceof Element) {
                 final Element child = (Element) w3cNode;
                 final String name = getType(child);
-                PluginManager mgr = getPluginManager();
-                final PluginType<?> type = mgr.getPluginType(name);
+                final PluginType<?> type = pluginManager.getPluginType(name);
                 final Node childNode = new Node(node, name, type);
                 constructHierarchy(childNode, child);
                 if (type == null) {

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1521753&r1=1521752&r2=1521753&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Wed Sep 11 07:41:46 2013
@@ -40,6 +40,9 @@
         reversed (previous "true"s should become "false"s, and vice versa). Since this was
an undocumented attribute up
         until now, it's unlikely this change will affect any users.
       </action>
+      <action issue="LOG4J2-393" dev="rgoers" type="fix">
+        Initialize PluginManager once during configuration. Move advertisement setup into
BaseConfiguration.
+      </action>
       <action issue="LOG4J2-391" dev="rgoers" type="fix" due-to="Kamal Bahadur">
         FlumePersistentManager now handles LockConflictExceptions in Berkeley Db.
       </action>



Mime
View raw message