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 2019/06/17 14:59:27 UTC

[logging-log4j2] 01/02: LOG4J2-1852 - Locate plugins inside a Jar using a URLConnection

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

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

commit 9abf2a91a4f7625f154c2e853a5747d777241a55
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sun Jun 16 21:46:15 2019 -0700

    LOG4J2-1852 - Locate plugins inside a Jar using a URLConnection
---
 .../core/config/plugins/util/ResolverUtil.java     | 40 ++++++++++++++++++++++
 src/changes/changes.xml                            |  3 ++
 2 files changed, 43 insertions(+)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/ResolverUtil.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/ResolverUtil.java
index 1aa9a36..016efcf 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/ResolverUtil.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/ResolverUtil.java
@@ -21,6 +21,7 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
+import java.net.JarURLConnection;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
@@ -33,6 +34,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 import java.util.jar.JarInputStream;
 
 import org.apache.logging.log4j.Logger;
@@ -86,6 +88,8 @@ public class ResolverUtil {
 
     private static final String VFS = "vfs";
 
+    private static final String JAR = "jar";
+
     private static final String BUNDLE_RESOURCE = "bundleresource";
 
     /** The set of matches being accumulated. */
@@ -225,6 +229,8 @@ public class ResolverUtil {
                     }
                 } else if (BUNDLE_RESOURCE.equals(url.getProtocol())) {
                     loadImplementationsInBundle(test, packageName);
+                } else if (JAR.equals(url.getProtocol())) {
+                    loadImplementationsInJar(test, packageName, url);
                 } else {
                     final File file = new File(urlPath);
                     if (file.isDirectory()) {
@@ -328,6 +334,40 @@ public class ResolverUtil {
      *        a Test used to filter the classes that are discovered
      * @param parent
      *        the parent package under which classes must be in order to be considered
+     * @param url
+     *        the url that identifies the jar containing the resource.
+     */
+    private void loadImplementationsInJar(final Test test, final String parent, final URL url) {
+        JarURLConnection connection = null;
+        try {
+            connection = (JarURLConnection) url.openConnection();
+            if (connection != null) {
+                JarFile jarFile = connection.getJarFile();
+                Enumeration<JarEntry> entries = jarFile.entries();
+                while (entries.hasMoreElements()) {
+                    JarEntry entry = entries.nextElement();
+                    final String name = entry.getName();
+                    if (!entry.isDirectory() && name.startsWith(parent) && isTestApplicable(test, name)) {
+                        addIfMatching(test, name);
+                    }
+                }
+            } else {
+                LOGGER.error("Could not establish connection to {}", url.toString());
+            }
+        } catch (final IOException ex) {
+            LOGGER.error("Could not search JAR file '{}' for classes matching criteria {}, file not found",
+                url.toString(), test, ex);
+        }
+    }
+
+    /**
+     * Finds matching classes within a jar files that contains a folder structure matching the package structure. If the
+     * File is not a JarFile or does not exist a warning will be logged, but no error will be raised.
+     *
+     * @param test
+     *        a Test used to filter the classes that are discovered
+     * @param parent
+     *        the parent package under which classes must be in order to be considered
      * @param jarFile
      *        the jar file to be examined for classes
      */
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index f45e2fa..a932d67 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,6 +30,9 @@
          - "remove" - Removed
     -->
     <release version="2.12.0" date="2019-MM-DD" description="GA Release 2.12.0">
+      <action issue="LOG4J2-1852" dev="rgoers" type="fix" due-to="Tanner Altares">
+        Locate plugins within a Jar using a URL Connection.
+      </action>
       <action issue="LOG4J2-2610" dev="rgoers" type="fix">
         Explicitly set file creation time.
       </action>