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/28 23:34:12 UTC

[logging-log4j2] branch master updated: LOG4J2-2790 Conditionally allocate PluginEntry during PluginCache load (#344)

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 009107f  LOG4J2-2790 Conditionally allocate PluginEntry during PluginCache load (#344)
009107f is described below

commit 009107ff2a6a2f6f76dd652ae044e96b60f93f5a
Author: Marius Volkhart <ma...@volkhart.com>
AuthorDate: Sun Mar 29 00:34:02 2020 +0100

    LOG4J2-2790 Conditionally allocate PluginEntry during PluginCache load (#344)
    
    * LOG4J2-2790 Conditionally allocate PluginEntry during PluginCache loading
    
    When populating the PluginCache from resources, only allocate a new PluginEntry if there is not already an existing one.
    
    * Populate PluginCache's PluginEntries via constructor
    
    Utilizing the newly added constructor removes several methods calls.
---
 .../log4j/plugins/processor/PluginCache.java       | 25 +++++++---------------
 1 file changed, 8 insertions(+), 17 deletions(-)

diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginCache.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginCache.java
index c731352..5f64a2b 100644
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginCache.java
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/processor/PluginCache.java
@@ -18,11 +18,8 @@
 package org.apache.logging.log4j.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;
@@ -53,10 +50,7 @@ public class PluginCache {
      */
     public Map<String, PluginEntry> getCategory(final String category) {
         final String key = category.toLowerCase();
-        if (!categories.containsKey(key)) {
-            categories.put(key, new TreeMap<String, PluginEntry>());
-        }
-        return categories.get(key);
+        return categories.computeIfAbsent(key, ignored -> new TreeMap<>());
     }
 
     /**
@@ -76,16 +70,13 @@ public class PluginCache {
                     final Map<String, PluginEntry> m = getCategory(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);
-                        if (!m.containsKey(entry.getKey())) {
-                            m.put(entry.getKey(), entry);
-                        }
+                        // Must always read all parts of the entry, even if not adding, so that the stream progresses
+                        final String key = in.readUTF();
+                        final String className = in.readUTF();
+                        final String name = in.readUTF();
+                        final boolean printable = in.readBoolean();
+                        final boolean defer = in.readBoolean();
+                        m.computeIfAbsent(key, k -> new PluginEntry(k, className, name, printable, defer, category));
                     }
                 }
             }