You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2016/11/04 23:39:12 UTC

logging-log4j2 git commit: [LOG4J2-1651] Makes the dat format more user friendly.

Repository: logging-log4j2
Updated Branches:
  refs/heads/LOG4J2-1651 [created] f8f14e4b9


[LOG4J2-1651] Makes the dat format more user friendly.

Writes out XML (but does not read it back).

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/f8f14e4b
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/f8f14e4b
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/f8f14e4b

Branch: refs/heads/LOG4J2-1651
Commit: f8f14e4b9473f47eb7b30aea908032a2c6779bfa
Parents: dee36ee
Author: Gary Gregory <gg...@apache.org>
Authored: Fri Nov 4 16:39:05 2016 -0700
Committer: Gary Gregory <gg...@apache.org>
Committed: Fri Nov 4 16:39:05 2016 -0700

----------------------------------------------------------------------
 .../config/plugins/processor/PluginCache.java   | 99 ++++++++++++++++----
 .../plugins/processor/PluginProcessor.java      | 24 ++++-
 .../processor/PluginCacheFormatTest.java        | 30 ++++++
 pom.xml                                         |  3 +
 4 files changed, 134 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f8f14e4b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java
index 2fd4160..f572437 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCache.java
@@ -17,6 +17,7 @@
 
 package org.apache.logging.log4j.core.config.plugins.processor;
 
+import java.beans.XMLEncoder;
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.DataInputStream;
@@ -28,10 +29,72 @@ import java.util.Enumeration;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
+import org.apache.logging.log4j.util.Strings;
+
 /**
  *
  */
 public class PluginCache {
+    
+    public enum Format {
+        DAT {
+            @Override
+            public void writeCache(PluginCache pluginCache, OutputStream os) throws IOException {
+                try (final DataOutputStream out = new DataOutputStream(new BufferedOutputStream(os))) {
+                    // See PluginManager.readFromCacheFiles for the corresponding decoder. Format may not be changed
+                    // without breaking existing Log4j2Plugins.dat files.
+                    out.writeInt(pluginCache.categories.size());
+                    for (final Map.Entry<String, Map<String, PluginEntry>> category : pluginCache.categories
+                            .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());
+                        }
+                    }
+                }
+            }
+        },
+        
+        XML {
+            @Override
+            public void writeCache(PluginCache pluginCache, final OutputStream os) {
+                try (final XMLEncoder out = new XMLEncoder(os)) {
+                    out.writeObject(pluginCache.categories);
+                }
+            }
+        };
+
+        public abstract void writeCache(PluginCache pluginCache, final OutputStream os) throws IOException;
+
+        /**
+         * Parses a comma-separated list of {@code Format}s.
+         * 
+         * @param formatsStr
+         *            input
+         * @param defaultFormats
+         *            The default Formats if the input is null or empty.
+         * @return a non-null array
+         */
+        public static Format[] parse(String formatsStr, Format... defaultFormats) {
+            if (Strings.isBlank(formatsStr)) {
+                return defaultFormats;
+            }
+            final String[] split = formatsStr.split("\\s*,\\s*");
+            Format[] formats = new Format[split.length];
+            for (int i = 0; i < formats.length; i++) {
+                formats[i] = Format.valueOf(split[i]);
+            }
+            return formats;
+        }
+    }
+    
     private final Map<String, Map<String, PluginEntry>> categories =
         new LinkedHashMap<>();
 
@@ -64,26 +127,28 @@ public class PluginCache {
      *
      * @param os destination to save cache to.
      * @throws IOException if an I/O exception occurs.
+     * @deprecated Use {@link #writeCache(OutputStream, String)} or {@link Format#writeCache(PluginCache, OutputStream)}.
      */
+    @Deprecated
     // NOTE: if this file format is to be changed, the filename should change and this format should still be readable
     public void writeCache(final OutputStream os) throws IOException {
-        try (final DataOutputStream out = new DataOutputStream(new BufferedOutputStream(os))) {
-            // See PluginManager.readFromCacheFiles for the corresponding decoder. Format may not be changed
-            // without breaking existing Log4j2Plugins.dat files.
-            out.writeInt(categories.size());
-            for (final Map.Entry<String, Map<String, PluginEntry>> category : categories.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());
-                }
-            }
+        Format.DAT.writeCache(this, os);
+    }
+
+    /**
+     * Stores the plugin cache to a given OutputStream.
+     *
+     * @param os destination to save cache to.
+     * @throws IOException if an I/O exception occurs.
+     */
+    // NOTE: if this file format is to be changed, the filename should change and this format should still be readable
+    public void writeCache(final OutputStream os, String formatsStr) throws IOException {
+        if (Strings.isBlank(formatsStr)) {
+            Format.DAT.writeCache(this, os);
+            return;
+        }
+        for (String formatStr : formatsStr.split("\\s*,\\s*")) {
+            Format.valueOf(formatStr).writeCache(this, os);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f8f14e4b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java
index 13a83a6..840a389 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java
@@ -22,6 +22,7 @@ import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
@@ -40,6 +41,7 @@ import javax.tools.StandardLocation;
 
 import org.apache.logging.log4j.core.config.plugins.Plugin;
 import org.apache.logging.log4j.core.config.plugins.PluginAliases;
+import org.apache.logging.log4j.core.config.plugins.processor.PluginCache.Format;
 import org.apache.logging.log4j.util.Strings;
 
 /**
@@ -51,11 +53,17 @@ public class PluginProcessor extends AbstractProcessor {
     // TODO: this could be made more abstract to allow for compile-time and run-time plugin processing
 
     /**
+     * The location of the plugin cache data file base name.
+     */
+    public static final String PLUGIN_CACHE_FILE_BASE =
+            "META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins";
+
+    /**
      * The location of the plugin cache data file. This file is written to by this processor, and read from by
      * {@link org.apache.logging.log4j.core.config.plugins.util.PluginManager}.
      */
     public static final String PLUGIN_CACHE_FILE =
-            "META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat";
+            PLUGIN_CACHE_FILE_BASE + ".dat";
 
     private final PluginCache pluginCache = new PluginCache();
 
@@ -105,10 +113,16 @@ public class PluginProcessor extends AbstractProcessor {
     }
 
     private void writeCacheFile(final Element... elements) throws IOException {
-        final FileObject fileObject = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, Strings.EMPTY,
-                PLUGIN_CACHE_FILE, elements);
-        try (final OutputStream out = fileObject.openOutputStream()) {
-            pluginCache.writeCache(out);
+        final String pluginCacheFileFormats = processingEnv.getOptions().get("pluginCacheFileFormats");
+        System.err.println("pluginCacheFileFormats = " + pluginCacheFileFormats);
+        final Format[] formats = PluginCache.Format.parse(pluginCacheFileFormats, PluginCache.Format.DAT);
+        for (final Format format : formats) {
+            final String fileName = PLUGIN_CACHE_FILE_BASE + "." + format.toString().toLowerCase(Locale.ROOT);
+            final FileObject fileObject = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT,
+                    Strings.EMPTY, fileName, elements);
+            try (final OutputStream out = fileObject.openOutputStream()) {
+                format.writeCache(pluginCache, out);
+            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f8f14e4b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCacheFormatTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCacheFormatTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCacheFormatTest.java
new file mode 100644
index 0000000..2a8fe01
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginCacheFormatTest.java
@@ -0,0 +1,30 @@
+/*
+ * 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 org.apache.logging.log4j.core.config.plugins.processor.PluginCache.Format;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PluginCacheFormatTest {
+
+    @Test
+    public void testParseFormats() {
+        Assert.assertArrayEquals(new Format[] { Format.DAT, Format.XML }, Format.parse("DAT,XML", Format.DAT));
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f8f14e4b/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index fd71440..dd73fef 100644
--- a/pom.xml
+++ b/pom.xml
@@ -889,6 +889,9 @@
               <phase>process-classes</phase>
               <configuration>
                 <proc>only</proc>
+                <compilerArguments>
+                  <ApluginCacheFileFormats>DAT,XML</ApluginCacheFileFormats>
+                </compilerArguments>
               </configuration>
             </execution>
           </executions>