logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattsic...@apache.org
Subject svn commit: r1585603 - in /logging/log4j/log4j2/trunk/log4j-plugin-processor/src: main/java/org/apache/logging/log4j/core/config/plugins/processor/ test/java/org/apache/logging/log4j/core/config/plugins/processor/
Date Mon, 07 Apr 2014 22:06:30 GMT
Author: mattsicker
Date: Mon Apr  7 22:06:29 2014
New Revision: 1585603

URL: http://svn.apache.org/r1585603
Log:
Refactor plugin cache to its own class.

Added:
    logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java
  (with props)
Modified:
    logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java
    logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorTest.java

Added: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java?rev=1585603&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java
(added)
+++ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java
Mon Apr  7 22:06:29 2014
@@ -0,0 +1,122 @@
+/*
+ * 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.processor;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+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;
+
+/**
+ *
+ */
+public class PluginCache {
+    private final ConcurrentMap<String, ConcurrentMap<String, PluginEntry>> pluginCategories
=
+            new ConcurrentHashMap<String, ConcurrentMap<String, PluginEntry>>();
+
+    /**
+     * Gets or creates a category of plugins.
+     *
+     * @param category name of category to look up.
+     * @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);
+    }
+
+    /**
+     * Stores the plugin cache to a given OutputStream.
+     *
+     * @param os destination to save cache to.
+     * @throws IOException
+     */
+    public void writeCache(final OutputStream os) throws IOException {
+        final DataOutputStream out = new DataOutputStream(new BufferedOutputStream(os));
+        try {
+            out.writeInt(pluginCategories.size());
+            for (final Map.Entry<String, ConcurrentMap<String, PluginEntry>>
category : pluginCategories.entrySet()) {
+                out.writeUTF(category.getKey());
+                final Map<String, PluginEntry> m = category.getValue();
+                out.writeInt(m.size());
+                for (final Map.Entry<String, PluginEntry> entry : m.entrySet()) {
+                    final PluginEntry plugin = entry.getValue();
+                    out.writeUTF(plugin.getKey());
+                    out.writeUTF(plugin.getClassName());
+                    out.writeUTF(plugin.getName());
+                    out.writeBoolean(plugin.isPrintable());
+                    out.writeBoolean(plugin.isDefer());
+                }
+            }
+        } finally {
+            out.close();
+        }
+    }
+
+    /**
+     * Loads and merges all the Log4j plugin cache files specified. Usually, this is obtained
via a ClassLoader.
+     *
+     * @param resources URLs to all the desired plugin cache files to load.
+     * @throws IOException
+     */
+    public void loadCacheFiles(final Enumeration<URL> resources) throws IOException
{
+        pluginCategories.clear();
+        while (resources.hasMoreElements()) {
+            final URL url = resources.nextElement();
+            final DataInputStream in = new DataInputStream(new BufferedInputStream(url.openStream()));
+            try {
+                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++) {
+                        final PluginEntry entry = new PluginEntry();
+                        entry.setKey(in.readUTF());
+                        entry.setClassName(in.readUTF());
+                        entry.setName(in.readUTF());
+                        entry.setPrintable(in.readBoolean());
+                        entry.setDefer(in.readBoolean());
+                        entry.setCategory(category);
+                        m.putIfAbsent(entry.getKey(), entry);
+                    }
+                    pluginCategories.putIfAbsent(category, m);
+                }
+            } finally {
+                in.close();
+            }
+        }
+    }
+
+    /**
+     * Gets the number of plugin categories registered.
+     *
+     * @return number of plugin categories in cache.
+     */
+    public int size() {
+        return pluginCategories.size();
+    }
+}

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

Modified: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java?rev=1585603&r1=1585602&r2=1585603&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java
Mon Apr  7 22:06:29 2014
@@ -17,15 +17,12 @@
 
 package org.apache.logging.log4j.core.config.plugins.processor;
 
-import java.io.BufferedOutputStream;
-import java.io.DataOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import javax.annotation.processing.AbstractProcessor;
 import javax.annotation.processing.RoundEnvironment;
@@ -61,8 +58,7 @@ public class PluginProcessor extends Abs
      */
     public static final String FILENAME = "Log4j2Plugins.dat";
 
-    private final ConcurrentMap<String, ConcurrentMap<String, PluginEntry>> pluginCategories
=
-            new ConcurrentHashMap<String, ConcurrentMap<String, PluginEntry>>();
+    private final PluginCache pluginCache = new PluginCache();
 
     @Override
     public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment
roundEnv) {
@@ -93,8 +89,7 @@ public class PluginProcessor extends Abs
         for (final Element element : elements) {
             final Plugin plugin = element.getAnnotation(Plugin.class);
             final PluginEntry entry = element.accept(pluginVisitor, plugin);
-            pluginCategories.putIfAbsent(entry.getCategory(), new ConcurrentHashMap<String,
PluginEntry>());
-            final ConcurrentMap<String, PluginEntry> category = pluginCategories.get(entry.getCategory());
+            final ConcurrentMap<String, PluginEntry> category = pluginCache.getCategory(entry.getCategory());
             category.put(entry.getKey(), entry);
             final Collection<PluginEntry> entries = element.accept(pluginAliasesVisitor,
plugin);
             for (final PluginEntry pluginEntry : entries) {
@@ -105,22 +100,9 @@ public class PluginProcessor extends Abs
 
     private void writeCacheFile(final Element... elements) throws IOException {
         final FileObject fo = processingEnv.getFiler().createResource(CLASS_OUTPUT, PLUGINS_PACKAGE_NAME,
FILENAME, elements);
-        final DataOutputStream out = new DataOutputStream(new BufferedOutputStream(fo.openOutputStream()));
+        final OutputStream out = fo.openOutputStream();
         try {
-            out.writeInt(pluginCategories.size());
-            for (final Map.Entry<String, ConcurrentMap<String, PluginEntry>>
category : pluginCategories.entrySet()) {
-                out.writeUTF(category.getKey());
-                final Map<String, PluginEntry> m = category.getValue();
-                out.writeInt(m.size());
-                for (final Map.Entry<String, PluginEntry> entry : m.entrySet()) {
-                    final PluginEntry plugin = entry.getValue();
-                    out.writeUTF(plugin.getKey());
-                    out.writeUTF(plugin.getClassName());
-                    out.writeUTF(plugin.getName());
-                    out.writeBoolean(plugin.isPrintable());
-                    out.writeBoolean(plugin.isDefer());
-                }
-            }
+            pluginCache.writeCache(out);
         } finally {
             out.close();
         }

Modified: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorTest.java?rev=1585603&r1=1585602&r2=1585603&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorTest.java
Mon Apr  7 22:06:29 2014
@@ -17,11 +17,8 @@
 
 package org.apache.logging.log4j.core.config.plugins.processor;
 
-import java.io.BufferedInputStream;
-import java.io.DataInputStream;
 import java.net.URL;
 import java.util.Enumeration;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
 import org.apache.logging.log4j.core.config.plugins.PluginAliases;
@@ -30,63 +27,35 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.*;
 
 @RunWith(JUnit4.class)
 public class PluginProcessorTest {
 
     private static final String CACHE_FILE = "org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat";
 
-    private static ConcurrentMap<String, ConcurrentMap<String, PluginEntry>>
pluginCategories;
+    private static final PluginCache pluginCache = new PluginCache();
 
     private final Plugin p = FakePlugin.class.getAnnotation(Plugin.class);
 
     @BeforeClass
     public static void setUpClass() throws Exception {
-        // TODO: refactor PluginManager.decode() into this module?
-        pluginCategories = new ConcurrentHashMap<String, ConcurrentMap<String, PluginEntry>>();
         final Enumeration<URL> resources = PluginProcessor.class.getClassLoader().getResources(CACHE_FILE);
-        while (resources.hasMoreElements()) {
-            final URL url = resources.nextElement();
-            final DataInputStream in = new DataInputStream(new BufferedInputStream(url.openStream()));
-            try {
-                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++) {
-                        final PluginEntry entry = new PluginEntry();
-                        entry.setKey(in.readUTF());
-                        entry.setClassName(in.readUTF());
-                        entry.setName(in.readUTF());
-                        entry.setPrintable(in.readBoolean());
-                        entry.setDefer(in.readBoolean());
-                        entry.setCategory(category);
-                        m.putIfAbsent(entry.getKey(), entry);
-                    }
-                    pluginCategories.putIfAbsent(category, m);
-                }
-            } finally {
-                in.close();
-            }
-        }
+        pluginCache.loadCacheFiles(resources);
     }
 
     @Test
     public void testTestCategoryFound() throws Exception {
         assertNotNull("No plugin annotation on FakePlugin.", p);
-        final ConcurrentMap<String, PluginEntry> testCategory = pluginCategories.get(p.category());
-        assertNotEquals("No plugins were found.", 0, pluginCategories.size());
+        final ConcurrentMap<String, PluginEntry> testCategory = pluginCache.getCategory(p.category());
+        assertNotEquals("No plugins were found.", 0, pluginCache.size());
         assertNotNull("The category '" + p.category() + "' was not found.", testCategory);
+        assertFalse(testCategory.isEmpty());
     }
 
     @Test
     public void testFakePluginFoundWithCorrectInformation() throws Exception {
-        final PluginEntry fake = pluginCategories.get(p.category()).get(p.name().toLowerCase());
+        final PluginEntry fake = pluginCache.getCategory(p.category()).get(p.name().toLowerCase());
         verifyFakePluginEntry(p.name(), fake);
     }
 
@@ -94,7 +63,7 @@ public class PluginProcessorTest {
     public void testFakePluginAliasesContainSameInformation() throws Exception {
         final PluginAliases aliases = FakePlugin.class.getAnnotation(PluginAliases.class);
         for (final String alias : aliases.value()) {
-            final PluginEntry fake = pluginCategories.get(p.category()).get(alias.toLowerCase());
+            final PluginEntry fake = pluginCache.getCategory(p.category()).get(alias.toLowerCase());
             verifyFakePluginEntry(alias, fake);
         }
     }
@@ -112,7 +81,7 @@ public class PluginProcessorTest {
     @Test
     public void testNestedPlugin() throws Exception {
         final Plugin p = FakePlugin.Nested.class.getAnnotation(Plugin.class);
-        final PluginEntry nested = pluginCategories.get(p.category()).get(p.name().toLowerCase());
+        final PluginEntry nested = pluginCache.getCategory(p.category()).get(p.name().toLowerCase());
         assertNotNull(nested);
         assertEquals(p.name().toLowerCase(), nested.getKey());
         assertEquals(FakePlugin.Nested.class.getName(), nested.getClassName());



Mime
View raw message