You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2014/04/27 02:57:25 UTC

svn commit: r1590331 - in /logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins: processor/PluginCache.java util/PluginManager.java util/PluginRegistry.java

Author: mattsicker
Date: Sun Apr 27 00:57:25 2014
New Revision: 1590331

URL: http://svn.apache.org/r1590331
Log:
Factor out plugin category maps.

Added:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginRegistry.java   (with props)
Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginManager.java

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java?rev=1590331&r1=1590330&r2=1590331&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java Sun Apr 27 00:57:25 2014
@@ -26,15 +26,15 @@ import java.io.OutputStream;
 import java.net.URL;
 import java.util.Enumeration;
 import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
+import org.apache.logging.log4j.core.config.plugins.util.PluginRegistry;
+
 /**
  *
  */
 public class PluginCache {
-    private final ConcurrentMap<String, ConcurrentMap<String, PluginEntry>> pluginCategories =
-            new ConcurrentHashMap<String, ConcurrentMap<String, PluginEntry>>();
+    private final transient PluginRegistry<PluginEntry> pluginCategories = new PluginRegistry<PluginEntry>();
 
     /**
      * Gets or creates a category of plugins.
@@ -43,7 +43,6 @@ public class PluginCache {
      * @return plugin mapping of names to plugin entries.
      */
     public ConcurrentMap<String, PluginEntry> getCategory(final String category) {
-        pluginCategories.putIfAbsent(category, new ConcurrentHashMap<String, PluginEntry>());
         return pluginCategories.get(category);
     }
 
@@ -90,7 +89,6 @@ public class PluginCache {
                 final int count = in.readInt();
                 for (int i = 0; i < count; i++) {
                     final String category = in.readUTF();
-                    pluginCategories.putIfAbsent(category, new ConcurrentHashMap<String, PluginEntry>());
                     final ConcurrentMap<String, PluginEntry> m = pluginCategories.get(category);
                     final int entries = in.readInt();
                     for (int j = 0; j < entries; j++) {
@@ -103,7 +101,6 @@ public class PluginCache {
                         entry.setCategory(category);
                         m.putIfAbsent(entry.getKey(), entry);
                     }
-                    pluginCategories.putIfAbsent(category, m);
                 }
             } finally {
                 in.close();

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginManager.java?rev=1590331&r1=1590330&r2=1590331&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginManager.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginManager.java Sun Apr 27 00:57:25 2014
@@ -31,7 +31,6 @@ import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.CopyOnWriteArrayList;
 
@@ -52,8 +51,8 @@ public class PluginManager {
     private static final long NANOS_PER_SECOND = 1000000000L;
 
     // TODO: re-use PluginCache code from plugin processor
-    private static ConcurrentMap<String, ConcurrentMap<String, PluginType<?>>> pluginTypeMap =
-        new ConcurrentHashMap<String, ConcurrentMap<String, PluginType<?>>>();
+    private static PluginRegistry<PluginType<?>> pluginTypeMap =
+        new PluginRegistry<PluginType<?>>();
 
     private static final CopyOnWriteArrayList<String> PACKAGES = new CopyOnWriteArrayList<String>();
     private static final String PATH = "org/apache/logging/log4j/core/config/plugins/";
@@ -157,7 +156,7 @@ public class PluginManager {
             resolver.setClassLoader(classLoader);
         }
         if (preLoad) {
-            final ConcurrentMap<String, ConcurrentMap<String, PluginType<?>>> map = decode(classLoader);
+            final PluginRegistry<PluginType<?>> map = decode(classLoader);
             if (map != null) {
                 pluginTypeMap = map;
                 plugins = map.get(category);
@@ -182,9 +181,6 @@ public class PluginManager {
         for (final Class<?> clazz : resolver.getClasses()) {
             final Plugin plugin = clazz.getAnnotation(Plugin.class);
             final String pluginCategory = plugin.category();
-            if (!pluginTypeMap.containsKey(pluginCategory)) {
-                pluginTypeMap.putIfAbsent(pluginCategory, new ConcurrentHashMap<String, PluginType<?>>());
-            }
             final Map<String, PluginType<?>> map = pluginTypeMap.get(pluginCategory);
             String type = plugin.elementType().equals(Plugin.EMPTY) ? plugin.name() : plugin.elementType();
             PluginType pluginType = new PluginType(clazz, type, plugin.printObject(), plugin.deferChildren());
@@ -214,7 +210,7 @@ public class PluginManager {
         LOGGER.debug(sb.toString());
     }
 
-    private static ConcurrentMap<String, ConcurrentMap<String, PluginType<?>>> decode(final ClassLoader classLoader) {
+    private static PluginRegistry<PluginType<?>> decode(final ClassLoader classLoader) {
         Enumeration<URL> resources;
         try {
             resources = classLoader.getResources(PATH + FILENAME);
@@ -222,8 +218,7 @@ public class PluginManager {
             LOGGER.warn("Unable to preload plugins", ioe);
             return null;
         }
-        final ConcurrentMap<String, ConcurrentMap<String, PluginType<?>>> map =
-            new ConcurrentHashMap<String, ConcurrentMap<String, PluginType<?>>>();
+        final PluginRegistry<PluginType<?>> map = new PluginRegistry<PluginType<?>>();
         while (resources.hasMoreElements()) {
             final URL url = resources.nextElement();
             LOGGER.debug("Found Plugin Map at {}", url.toExternalForm());
@@ -238,12 +233,9 @@ public class PluginManager {
             try {
                 final int count = dis.readInt();
                 for (int j = 0; j < count; ++j) {
-                    final String type = dis.readUTF();
+                    final String category = dis.readUTF();
                     final int entries = dis.readInt();
-                    ConcurrentMap<String, PluginType<?>> types = map.get(type);
-                    if (types == null) {
-                        types = new ConcurrentHashMap<String, PluginType<?>>(count);
-                    }
+                    final Map<String, PluginType<?>> types = map.get(category);
                     for (int i = 0; i < entries; ++i) {
                         final String key = dis.readUTF();
                         final String className = dis.readUTF();
@@ -258,7 +250,6 @@ public class PluginManager {
                             LOGGER.info("Plugin [{}] could not be loaded due to missing classes.", className);
                         }
                     }
-                    map.putIfAbsent(type, types);
                 }
             } catch (final IOException ex) {
                 LOGGER.warn("Unable to preload plugins", ex);
@@ -279,7 +270,7 @@ public class PluginManager {
         return pluginType;
     }
 
-    private static void encode(final ConcurrentMap<String, ConcurrentMap<String, PluginType<?>>> map) {
+    private static void encode(final PluginRegistry<PluginType<?>> map) {
         final String fileName = rootDir + PATH + FILENAME;
         final File file = new File(rootDir + PATH);
         file.mkdirs();

Added: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginRegistry.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginRegistry.java?rev=1590331&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginRegistry.java (added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginRegistry.java Sun Apr 27 00:57:25 2014
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.logging.log4j.core.config.plugins.util;
+
+import java.io.Serializable;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ * Registry for PluginType maps partitioned by category names.
+ *
+ * @param <T> plugin information object such as PluginType or PluginEntry.
+ */
+public class PluginRegistry<T extends Serializable> {
+    private final ConcurrentMap<String, ConcurrentMap<String, T>> categories =
+        new ConcurrentHashMap<String, ConcurrentMap<String, T>>();
+
+    public ConcurrentMap<String, T> get(final String category) {
+        if (category == null) {
+            throw new IllegalArgumentException("Category name cannot be null.");
+        }
+        final String key = category.toLowerCase();
+        categories.putIfAbsent(key, new ConcurrentHashMap<String, T>());
+        return categories.get(key);
+    }
+
+    public int size() {
+        return categories.size();
+    }
+
+    public boolean isEmpty() {
+        return categories.isEmpty();
+    }
+
+    public void clear() {
+        categories.clear();
+    }
+
+    public boolean containsKey(final String key) {
+        return categories.containsKey(key);
+    }
+
+    public Set<Map.Entry<String, ConcurrentMap<String, T>>> entrySet() {
+        return categories.entrySet();
+    }
+}

Propchange: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginRegistry.java
------------------------------------------------------------------------------
    svn:eol-style = native