Author: rpopma
Date: Sun Jan 5 04:51:44 2014
New Revision: 1555458
URL: http://svn.apache.org/r1555458
Log:
LOG4J2-445: ResolverUtil cannot find packages in file URLs which include the
'+' character
Added:
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/ResolverUtilTest.java
(with props)
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/ResolverUtil.java
logging/log4j/log4j2/trunk/src/changes/changes.xml
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/ResolverUtil.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/ResolverUtil.java?rev=1555458&r1=1555457&r2=1555458&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/ResolverUtil.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/ResolverUtil.java
Sun Jan 5 04:51:44 2014
@@ -20,13 +20,16 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
@@ -237,18 +240,7 @@ public class ResolverUtil {
while (urls.hasMoreElements()) {
try {
final URL url = urls.nextElement();
- String urlPath = url.getFile();
- urlPath = URLDecoder.decode(urlPath, Charsets.UTF_8.name());
-
- // If it's a file in a directory, trim the stupid file: spec
- if (urlPath.startsWith("file:")) {
- urlPath = urlPath.substring(5);
- }
-
- // Else it's in a JAR, grab the path to the jar
- if (urlPath.indexOf('!') > 0) {
- urlPath = urlPath.substring(0, urlPath.indexOf('!'));
- }
+ String urlPath = extractPath(url);
LOGGER.info("Scanning for classes in [" + urlPath + "]
matching criteria: " + test);
// Check for a jar in a war in JBoss
@@ -278,6 +270,38 @@ public class ResolverUtil {
}
}
+ String extractPath(final URL url) throws UnsupportedEncodingException {
+ String urlPath = url.getPath(); // same as getFile but without the
Query portion
+ //System.out.println(url.getProtocol() + "->" + urlPath);
+
+ // I would be surprised if URL.getPath() ever starts with "jar:" but
no harm in checking
+ if (urlPath.startsWith("jar:")) {
+ urlPath = urlPath.substring(4);
+ }
+ // For jar: URLs, the path part starts with "file:"
+ if (urlPath.startsWith("file:")) {
+ urlPath = urlPath.substring(5);
+ }
+ // If it was in a JAR, grab the path to the jar
+ if (urlPath.indexOf('!') > 0) {
+ urlPath = urlPath.substring(0, urlPath.indexOf('!'));
+ }
+
+ // LOG4J2-445
+ // Finally, decide whether to URL-decode the file name or not...
+ final String protocol = url.getProtocol();
+ final List<String> neverDecode = Arrays.asList(VFSZIP,
BUNDLE_RESOURCE);
+ if (neverDecode.contains(protocol)) {
+ return urlPath;
+ }
+ if (new File(urlPath).exists()) {
+ // if URL-encoded file exists, don't decode it
+ return urlPath;
+ }
+ urlPath = URLDecoder.decode(urlPath, Charsets.UTF_8.name());
+ return urlPath;
+ }
+
private void loadImplementationsInBundle(final Test test, final String
packageName) {
//Do not remove the cast on the next line as removing it will cause a
compile error on Java 7.
final BundleWiring wiring = (BundleWiring) FrameworkUtil.getBundle(
Added:
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/ResolverUtilTest.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/ResolverUtilTest.java?rev=1555458&view=auto
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/ResolverUtilTest.java
(added)
+++
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/ResolverUtilTest.java
Sun Jan 5 04:51:44 2014
@@ -0,0 +1,155 @@
+/*
+ * 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;
+
+import static org.junit.Assert.*;
+
+import java.net.URL;
+
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * Tests the ResolverUtil class.
+ */
+public class ResolverUtilTest {
+
+ @Test
+ public void testExtractPathFromJarUrl() throws Exception {
+ URL url = new
URL("jar:file:/C:/Users/me/.m2/repository/junit/junit/4.11/junit-4.11.jar!/org/junit/Test.class");
+ String expected =
"/C:/Users/me/.m2/repository/junit/junit/4.11/junit-4.11.jar";
+ assertEquals(expected, new ResolverUtil().extractPath(url));
+ }
+
+ @Test
+ public void testExtractPathFromJarUrlNotDecodedIfFileExists() throws
Exception {
+ final String existingFile = "/log4j+config+with+plus+characters.xml";
+ URL url = ResolverUtilTest.class.getResource(existingFile);
+ if (!url.getProtocol().equals("jar")) {
+ // create fake jar: URL that resolves to existing file
+ url = new URL("jar:" + url.toExternalForm() + "!/some/entry");
+ }
+ final String actual = new ResolverUtil().extractPath(url);
+ assertTrue("should not be decoded: " + actual,
actual.endsWith(existingFile));
+ }
+
+ @Test
+ public void testExtractPathFromJarUrlDecodedIfFileDoesNotExist() throws
Exception {
+ URL url = new
URL("jar:file:/path+with+plus/file+does+not+exist.jar!/some/file");
+ String expected = "/path with plus/file does not exist.jar";
+ assertEquals(expected, new ResolverUtil().extractPath(url));
+ }
+
+ @Test
+ public void testExtractPathFromFileUrl() throws Exception {
+ URL url = new
URL("file:/C:/Users/me/workspace/log4j2/log4j-core/target/test-classes/log4j2-config.xml");
+ String expected =
"/C:/Users/me/workspace/log4j2/log4j-core/target/test-classes/log4j2-config.xml";
+ assertEquals(expected, new ResolverUtil().extractPath(url));
+ }
+
+ @Test
+ public void testExtractPathFromFileUrlNotDecodedIfFileExists() throws
Exception {
+ final String existingFile = "/log4j+config+with+plus+characters.xml";
+ URL url = ResolverUtilTest.class.getResource(existingFile);
+ assertTrue("should be file url but was " + url,
"file".equals(url.getProtocol()));
+
+ final String actual = new ResolverUtil().extractPath(url);
+ assertTrue("should not be decoded: " + actual,
actual.endsWith(existingFile));
+ }
+
+ @Test
+ public void testExtractPathFromFileUrlDecodedIfFileDoesNotExist() throws
Exception {
+ URL url = new URL("file:///path+with+plus/file+does+not+exist.xml");
+ String expected = "/path with plus/file does not exist.xml";
+ assertEquals(expected, new ResolverUtil().extractPath(url));
+ }
+
+ @Ignore
+ @Test
+ public void testExtractPathFromVfszipUrl() throws Exception {
+ // need to install URLStreamHandlerFactory to prevent "unknown
protocol" MalformedURLException
+ URL url = new URL(
+
"vfszip:/home2/jboss-5.0.1.CR2/jboss-as/server/ais/ais-deploy/myear.ear/mywar.war/WEB-INF/some.xsd");
+ String expected =
"/home2/jboss-5.0.1.CR2/jboss-as/server/ais/ais-deploy/myear.ear/mywar.war/WEB-INF/some.xsd";
+ assertEquals(expected, new ResolverUtil().extractPath(url));
+ }
+
+ @Ignore
+ @Test
+ public void testExtractPathFromVfszipUrlWithPlusCharacters()
+ throws Exception {
+ // need to install URLStreamHandlerFactory to prevent "unknown
protocol" MalformedURLException
+ URL url = new URL("vfszip:/path+with+plus/file+name+with+plus.xml");
+ String expected = "/path+with+plus/file+name+with+plus.xml";
+ assertEquals(expected, new ResolverUtil().extractPath(url));
+ }
+
+ @Ignore
+ @Test
+ public void testExtractPathFromResourceBundleUrl() throws Exception {
+ // need to install URLStreamHandlerFactory to prevent "unknown
protocol" MalformedURLException
+ URL url = new URL("resourcebundle:/some/path/some/file.properties");
+ String expected = "/some/path/some/file.properties";
+ assertEquals(expected, new ResolverUtil().extractPath(url));
+ }
+
+ @Ignore
+ @Test
+ public void testExtractPathFromResourceBundleUrlWithPlusCharacters()
throws Exception {
+ // need to install URLStreamHandlerFactory to prevent "unknown
protocol" MalformedURLException
+ URL url = new URL("resourcebundle:/some+path/some+file.properties");
+ String expected = "/some+path/some+file.properties";
+ assertEquals(expected, new ResolverUtil().extractPath(url));
+ }
+
+ @Test
+ public void testExtractPathFromHttpUrl() throws Exception {
+ URL url = new URL("http://java.sun.com/index.html#chapter1");
+ String expected = "/index.html";
+ assertEquals(expected, new ResolverUtil().extractPath(url));
+ }
+
+ @Test
+ public void testExtractPathFromHttpUrlWithPlusCharacters() throws
Exception {
+ URL url = new
URL("http://www.server.com/path+with+plus/file+name+with+plus.jar!/org/junit/Test.class");
+ String expected = "/path with plus/file name with plus.jar";
+ assertEquals(expected, new ResolverUtil().extractPath(url));
+ }
+
+ @Test
+ public void testExtractPathFromHttpsComplexUrl() throws Exception {
+ URL url = new
URL("https://issues.apache.org/jira/browse/LOG4J2-445?focusedCommentId=13862479&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-13862479");
+ String expected = "/jira/browse/LOG4J2-445";
+ assertEquals(expected, new ResolverUtil().extractPath(url));
+ }
+
+ @Test
+ public void testExtractPathFromFtpUrl() throws Exception {
+ URL url = new
URL("ftp://user001:[email protected]/mydirectory/myfile.txt");
+ String expected = "/mydirectory/myfile.txt";
+ assertEquals(expected, new ResolverUtil().extractPath(url));
+ }
+
+ @Test
+ public void testExtractPathFromFtpUrlWithPlusCharacters() throws Exception
{
+ URL url = new
URL("ftp://user001:[email protected]/my+directory/my+file.txt");
+ String expected = "/my directory/my file.txt";
+ assertEquals(expected, new ResolverUtil().extractPath(url));
+ }
+
+}
Propchange:
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/ResolverUtilTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1555458&r1=1555457&r2=1555458&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sun Jan 5 04:51:44 2014
@@ -21,6 +21,9 @@
</properties>
<body>
<release version="2.0-RC1" date="2013-MM-DD" description="Bug fixes and
enhancements">
+ <action issue="LOG4J2-445" dev="rpopma" type="fix" due-to="Anthony
Baldocchi">
+ ResolverUtil cannot find packages in file URLs which include the '+'
character.
+ </action>
<action issue="LOG4J2-430" dev="rgoers" type="fix" due-to="David Gstir">
Use the formatted Message in RFC5424Layout for
non-StructuredDataMessages.
</action>