You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2020/03/04 22:05:17 UTC

[logging-log4j2] branch master updated: LOG4J2-2783 - PluginService should use classes in their definition instead of class names

This is an automated email from the ASF dual-hosted git repository.

rgoers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new 7f7ddb9  LOG4J2-2783 - PluginService should use classes in their definition instead of class names
7f7ddb9 is described below

commit 7f7ddb9dbbb1bf3970f0405c95ae8d8b8c9a5662
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Wed Mar 4 15:04:56 2020 -0700

    LOG4J2-2783 - PluginService should use classes in their definition instead of class names
---
 log4j-core/src/test/resources/log4j-list.xml       | 29 ++++++++++++++++++++++
 .../log4j/plugins/processor/PluginEntry.java       | 19 ++++++++++++++
 .../log4j/plugins/processor/PluginProcessor.java   |  4 +--
 .../log4j/plugins/processor/PluginService.java     | 18 ++++++--------
 .../logging/log4j/plugins/util/PluginRegistry.java | 28 +++++++++++----------
 src/changes/changes.xml                            |  3 +++
 6 files changed, 76 insertions(+), 25 deletions(-)

diff --git a/log4j-core/src/test/resources/log4j-list.xml b/log4j-core/src/test/resources/log4j-list.xml
new file mode 100644
index 0000000..c2a92c3
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j-list.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<Configuration status="WARN">
+  <Appenders>
+    <List name="List">
+      <PatternLayout pattern="[%-5level] %c{1.} %msg%n" />
+    </List>
+  </Appenders>
+  <Loggers>
+    <Root level="debug">
+      <AppenderRef ref="List" />
+    </Root>
+  </Loggers>
+</Configuration>
\ No newline at end of file
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginEntry.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginEntry.java
index bd452d3..dfebe5a 100644
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginEntry.java
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginEntry.java
@@ -27,6 +27,7 @@ public class PluginEntry implements Serializable {
 
     private String key;
     private String className;
+    private Class<?> pluginClass;
     private String name;
     private boolean printable;
     private boolean defer;
@@ -44,6 +45,16 @@ public class PluginEntry implements Serializable {
         this.category = category;
     }
 
+    public PluginEntry(String key, Class<?> pluginClass, String name, boolean printable, boolean defer, String category) {
+        this.key = key;
+        this.className = pluginClass.getName();
+        this.pluginClass = pluginClass;
+        this.name = name;
+        this.printable = printable;
+        this.defer = defer;
+        this.category = category;
+    }
+
     public String getKey() {
         return key;
     }
@@ -60,6 +71,14 @@ public class PluginEntry implements Serializable {
         this.className = className;
     }
 
+    public Class<?> getPluginClass() {
+        return pluginClass;
+    }
+
+    public void setPluginClass(Class<?> pluginClass) {
+        this.pluginClass = pluginClass;
+    }
+
     public String getName() {
         return name;
     }
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginProcessor.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginProcessor.java
index 6ccfdd2..e747ba9 100644
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginProcessor.java
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginProcessor.java
@@ -174,8 +174,8 @@ public class PluginProcessor extends AbstractProcessor {
             for (int i = 0; i < list.size(); ++i) {
                 PluginEntry entry = list.get(i);
                 sb.append("        ").append("new PluginEntry(\"");
-                sb.append(entry.getKey()).append("\", \"");
-                sb.append(entry.getClassName()).append("\", \"");
+                sb.append(entry.getKey()).append("\", ");
+                sb.append(entry.getClassName().replace('$', '.')).append(".class, \"");
                 sb.append(entry.getName()).append("\", ");
                 sb.append(entry.isPrintable()).append(", ");
                 sb.append(entry.isDefer()).append(", \"");
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginService.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginService.java
index 1884ff6..0b07093 100644
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginService.java
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginService.java
@@ -18,7 +18,11 @@ package org.apache.logging.log4j.plugins.processor;
 
 import org.apache.logging.log4j.plugins.util.PluginType;
 
-import java.util.*;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
 
 /**
  * Class Description goes here.
@@ -31,15 +35,9 @@ public abstract class PluginService {
         PluginEntry[] entries = getEntries();
         for (PluginEntry entry : entries) {
             String category = entry.getCategory().toLowerCase();
-            try {
-                Class<?> clazz = this.getClass().getClassLoader().loadClass(entry.getClassName());
-                List<PluginType<?>> list = categories.computeIfAbsent(category, ignored -> new LinkedList<>());
-                PluginType<?> type = new PluginType<>(entry, clazz, entry.getName());
-                list.add(type);
-            } catch (ClassNotFoundException ex) {
-                throw new IllegalStateException("No class named " + entry.getClassName() +
-                        " located for element " + entry.getName(), ex);
-            }
+            List<PluginType<?>> list = categories.computeIfAbsent(category, ignored -> new LinkedList<>());
+            PluginType<?> type = new PluginType<>(entry, entry.getPluginClass(), entry.getName());
+            list.add(type);
         }
     }
 
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/util/PluginRegistry.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/util/PluginRegistry.java
index 344b987..f9d3e0a 100644
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/util/PluginRegistry.java
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/util/PluginRegistry.java
@@ -32,7 +32,14 @@ import java.io.IOException;
 import java.net.URI;
 import java.net.URL;
 import java.text.DecimalFormat;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.ServiceLoader;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.atomic.AtomicReference;
@@ -195,19 +202,14 @@ public class PluginRegistry {
         for (final PluginService pluginService : serviceLoader) {
             PluginEntry[] entries = pluginService.getEntries();
             for (PluginEntry entry : entries) {
-                try {
-                    final Class<?> clazz = classLoader.loadClass(entry.getClassName());
-                    final PluginType<?> type = new PluginType<>(entry, clazz, entry.getName());
-                    String category = entry.getCategory().toLowerCase();
-                    if (!map.containsKey(category)) {
-                        map.put(category, new ArrayList<>());
-                    }
-                    List<PluginType<?>> list = map.get(category);
-                    list.add(type);
-                    ++pluginCount;
-                } catch (final ClassNotFoundException e) {
-                    LOGGER.info("Plugin [{}] could not be loaded due to missing classes.", entry.getClassName(), e);
+                final PluginType<?> type = new PluginType<>(entry, entry.getPluginClass(), entry.getName());
+                String category = entry.getCategory().toLowerCase();
+                if (!map.containsKey(category)) {
+                    map.put(category, new ArrayList<>());
                 }
+                List<PluginType<?>> list = map.get(category);
+                list.add(type);
+                ++pluginCount;
             }
         }
         final int numPlugins = pluginCount;
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7bcbf2e..2c375f5 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -31,6 +31,9 @@
          - "remove" - Removed
     -->
     <release version="3.0.0" date="2019-xx-xx" description="GA Release 3.0.0">
+      <action issue="LOG4J2-2783" dev="rgoers" type="update">
+        PluginService should use classes in their definition instead of class names.
+      </action>
       <action issue="LOG4J2-2678" dev="rgoers" type="update" due-to="Federico D'Ambrosio">
         Add LogEvent timestamp to ProducerRecord in KafkaAppender.
       </action>