Author: nickwilliams
Date: Tue Jan 28 05:39:31 2014
New Revision: 1561945
URL: http://svn.apache.org/r1561945
Log:
Fixed LOG4J2-409: Created a utility to properly escape backslashes before
creating URIs, and changed URI creation to use the utility instead of
instantiating URI directly.
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/FileUtils.java
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/helpers/FileUtilsTest.java
logging/log4j/log4j2/trunk/src/changes/changes.xml
logging/log4j/log4j2/trunk/src/site/xdoc/manual/webapp.xml
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java?rev=1561945&r1=1561944&r2=1561945&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
Tue Jan 28 05:39:31 2014
@@ -371,7 +371,7 @@ public abstract class ConfigurationFacto
if (config != null) {
ConfigurationSource source = null;
try {
- source = getInputFromURI(new URI(config));
+ source =
getInputFromURI(FileUtils.getCorrectedFilePathUri(config));
} catch (Exception ex) {
// Ignore the error and try as a String.
}
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java?rev=1561945&r1=1561944&r2=1561945&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
Tue Jan 28 05:39:31 2014
@@ -21,6 +21,7 @@ import java.net.URISyntaxException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.helpers.FileUtils;
import org.apache.logging.log4j.core.impl.ContextAnchor;
import org.apache.logging.log4j.core.impl.Log4jContextFactory;
import org.apache.logging.log4j.spi.LoggerContextFactory;
@@ -61,7 +62,7 @@ public final class Configurator {
final Object externalContext) {
try {
- final URI uri = configLocation == null ? null : new
URI(configLocation);
+ final URI uri = configLocation == null ? null :
FileUtils.getCorrectedFilePathUri(configLocation);
return initialize(name, loader, uri, externalContext);
} catch (final URISyntaxException ex) {
ex.printStackTrace();
@@ -143,7 +144,8 @@ public final class Configurator {
try {
URI configLocation = null;
try {
- configLocation = source.getLocation() == null ? null : new
URI(source.getLocation());
+ configLocation = source.getLocation() == null ?
+ null :
FileUtils.getCorrectedFilePathUri(source.getLocation());
} catch (final Exception ex) {
// Invalid source location.
}
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java?rev=1561945&r1=1561944&r2=1561945&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
Tue Jan 28 05:39:31 2014
@@ -96,7 +96,7 @@ public class JSONConfiguration extends B
stream = System.err;
} else {
try {
- final File destFile =
FileUtils.fileFromURI(new URI(dest));
+ final File destFile =
FileUtils.fileFromURI(FileUtils.getCorrectedFilePathUri(dest));
final String enc =
Charset.defaultCharset().name();
stream = new PrintStream(new
FileOutputStream(destFile), true, enc);
} catch (final URISyntaxException use) {
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java?rev=1561945&r1=1561944&r2=1561945&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
Tue Jan 28 05:39:31 2014
@@ -165,7 +165,7 @@ public class XMLConfiguration extends Ba
stream = System.err;
} else {
try {
- final File destFile =
FileUtils.fileFromURI(new URI(dest));
+ final File destFile =
FileUtils.fileFromURI(FileUtils.getCorrectedFilePathUri(dest));
final String enc =
Charset.defaultCharset().name();
stream = new PrintStream(new
FileOutputStream(destFile), true, enc);
} catch (final URISyntaxException use) {
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/FileUtils.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/FileUtils.java?rev=1561945&r1=1561944&r2=1561945&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/FileUtils.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/FileUtils.java
Tue Jan 28 05:39:31 2014
@@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
@@ -100,4 +101,16 @@ public final class FileUtils {
throw new IOException("File " + dir + " exists and is not a
directory. Unable to create directory.");
}
}
+
+ /**
+ * Takes a given URI string which may contain backslashes (illegal in
URIs) in it due to user input or variable
+ * substitution and returns a URI with the backslashes replaced with
forward slashes.
+ *
+ * @param uri The URI string
+ * @return the URI.
+ * @throws URISyntaxException if instantiating the URI threw a {@code
URISyntaxException}.
+ */
+ public static URI getCorrectedFilePathUri(String uri) throws
URISyntaxException {
+ return new URI(uri.replaceAll("\\\\+", "/"));
+ }
}
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java?rev=1561945&r1=1561944&r2=1561945&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java
Tue Jan 28 05:39:31 2014
@@ -45,6 +45,7 @@ import org.apache.logging.log4j.core.con
import org.apache.logging.log4j.core.helpers.Assert;
import org.apache.logging.log4j.core.helpers.Charsets;
import org.apache.logging.log4j.core.helpers.Closer;
+import org.apache.logging.log4j.core.helpers.FileUtils;
import org.apache.logging.log4j.status.StatusLogger;
/**
@@ -121,7 +122,7 @@ public class LoggerContextAdmin extends
LOGGER.debug("---------");
LOGGER.debug("Remote request to reconfigure using location "
+ configLocation);
- final URI uri = new URI(configLocation);
+ final URI uri = FileUtils.getCorrectedFilePathUri(configLocation);
// validate the location first: invalid location will result in
// default configuration being configured, try to avoid that...
@@ -157,7 +158,7 @@ public class LoggerContextAdmin extends
}
try {
final Charset charset = Charset.forName(charsetName);
- return readContents(new URI(getConfigLocationURI()), charset);
+ return
readContents(FileUtils.getCorrectedFilePathUri(getConfigLocationURI()),
charset);
} catch (final Exception ex) {
final StringWriter sw = new StringWriter(BUFFER_SIZE);
ex.printStackTrace(new PrintWriter(sw));
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java?rev=1561945&r1=1561944&r2=1561945&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
Tue Jan 28 05:39:31 2014
@@ -25,6 +25,7 @@ import javax.servlet.UnavailableExceptio
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.core.helpers.FileUtils;
import org.apache.logging.log4j.core.helpers.NetUtils;
import org.apache.logging.log4j.core.impl.ContextAnchor;
import org.apache.logging.log4j.core.impl.Log4jContextFactory;
@@ -93,7 +94,7 @@ final class Log4jWebInitializerImpl impl
URI configLocation = null;
if (location != null) {
try {
- configLocation = new URI(location);
+ configLocation = FileUtils.getCorrectedFilePathUri(location);
} catch (final Exception e) {
this.servletContext.log("Unable to convert configuration
location [" + location + "] to a URI!", e);
}
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/helpers/FileUtilsTest.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/helpers/FileUtilsTest.java?rev=1561945&r1=1561944&r2=1561945&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/helpers/FileUtilsTest.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/helpers/FileUtilsTest.java
Tue Jan 28 05:39:31 2014
@@ -21,6 +21,7 @@ import static org.junit.Assert.*;
import java.io.File;
import java.net.URI;
+import java.net.URISyntaxException;
import org.junit.Test;
@@ -31,8 +32,8 @@ public class FileUtilsTest {
@Test
public void testFileFromUriWithPlusCharactersInName() throws Exception {
- String CONFIG =
"target/test-classes/log4j+config+with+plus+characters.xml";
- URI uri = new URI(CONFIG);
+ String config =
"target/test-classes/log4j+config+with+plus+characters.xml";
+ URI uri = new URI(config);
File file = FileUtils.fileFromURI(uri);
assertEquals("log4j+config+with+plus+characters.xml", file.getName());
assertTrue("file exists", file.exists());
@@ -41,10 +42,28 @@ public class FileUtilsTest {
@Test
public void
testFileFromUriWithPlusCharactersConvertedToSpacesIfFileDoesNotExist()
throws Exception {
- String CONFIG = "NON-EXISTING-PATH/this+file+does+not+exist.xml";
- URI uri = new URI(CONFIG);
+ String config = "NON-EXISTING-PATH/this+file+does+not+exist.xml";
+ URI uri = new URI(config);
File file = FileUtils.fileFromURI(uri);
assertEquals("this file does not exist.xml", file.getName());
assertFalse("file does not exist", file.exists());
}
+
+ @Test
+ public void testGetCorrectedFilePathUriWithoutBackslashes() throws
URISyntaxException {
+ String config = "file:///path/to/something/on/unix";
+ URI uri = FileUtils.getCorrectedFilePathUri(config);
+
+ assertNotNull("The URI should not be null.", uri);
+ assertEquals("The URI is not correct.",
"file:///path/to/something/on/unix", uri.toString());
+ }
+
+ @Test
+ public void testGetCorrectedFilePathUriWithBackslashes() throws
URISyntaxException {
+ String config = "file:///D:\\path\\to\\something/on/windows";
+ URI uri = FileUtils.getCorrectedFilePathUri(config);
+
+ assertNotNull("The URI should not be null.", uri);
+ assertEquals("The URI is not correct.",
"file:///D:/path/to/something/on/windows", uri.toString());
+ }
}
Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1561945&r1=1561944&r2=1561945&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Tue Jan 28 05:39:31 2014
@@ -166,6 +166,10 @@
<action issue="LOG4J2-344" dev="nickwilliams" type="fix" due-to="Keir
Lawson, Tomasz Wladzinski">
Changed the Servlet 3.0 auto-initializer to add the filter by class to
get around a WebLogic bug.
</action>
+ <action issue="LOG4J2-409" dev="nickwilliams" type="fix" due-to="Frank
Steinmann, Thomas Neidhart">
+ Created a utility to properly escape backslashes before creating URIs,
and changed URI creation to use the
+ utility instead of instantiating URI directly.
+ </action>
</release>
<release version="2.0-beta9" date="2013-09-14" description="Bug fixes and
enhancements">
<action issue="LOG4J2-317" dev="ggregory" type="update">
Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/webapp.xml
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/webapp.xml?rev=1561945&r1=1561944&r2=1561945&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/webapp.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/webapp.xml Tue Jan 28
05:39:31 2014
@@ -171,7 +171,7 @@
</context-param>
<context-param>
<param-name>log4jConfiguration</param-name>
- <param-value>file://D:\conf\myLogging.xml</param-value>
+ <param-value>file:///D:/conf/myLogging.xml</param-value>
</context-param>]]></pre>
Note that in this case you must also set the "Log4jContextSelector"
system property to
"org.apache.logging.log4j.core.selector.JNDIContextSelector."