Fix property replacement bug in Log4jWebConfigurer

Previously, if the resolution of a ${...} placeholder resulted in a
valid URL for the location of a log4j properties/XML file, the URL
would ultimately be malformed by an unnecessary call to to
WebUtils#getRealPath.

The implementation of Log4jWebConfigurer#initLogging now eagerly
attempts SystemPropertyUtils#resolvePlaceholders before checking to see
if the location is a valid URL, and bypassing the call to
WebUtils#getRealPath if so.

Issue: SPR-9417
This commit is contained in:
Chris Beams 2012-05-17 10:11:46 +03:00
parent f1246a4317
commit 2503b7eb89
2 changed files with 12 additions and 6 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -124,9 +124,9 @@ public abstract class Log4jWebConfigurer {
try {
// Return a URL (e.g. "classpath:" or "file:") as-is;
// consider a plain file path as relative to the web application root directory.
location = SystemPropertyUtils.resolvePlaceholders(location);
if (!ResourceUtils.isUrl(location)) {
// Resolve system property placeholders before resolving real path.
location = SystemPropertyUtils.resolvePlaceholders(location);
location = WebUtils.getRealPath(servletContext, location);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -37,8 +37,9 @@ import org.springframework.mock.web.MockServletContext;
public class Log4jWebConfigurerTests {
private static final String TESTLOG4J_PROPERTIES = "testlog4j.properties";
private static final String CLASSPATH_RESOURCE = "classpath:org/springframework/web/util/testlog4j.properties";
private static final String RELATIVE_PATH = "src/test/resources/org/springframework/web/util/testlog4j.properties";
private static final String PROPERTIES_LOCATION = "org/springframework/web/util/testlog4j.properties";
private static final String CLASSPATH_RESOURCE = "classpath:" + PROPERTIES_LOCATION;
private static final String RELATIVE_PATH = "src/test/resources/" + PROPERTIES_LOCATION;
@Test
public void initLoggingWithClasspathResource() {
@ -59,7 +60,12 @@ public class Log4jWebConfigurerTests {
public void initLoggingWithRelativeFilePathAndRefreshInterval() {
initLogging(RELATIVE_PATH, true);
}
@Test // See SPR-9417
public void initLoggingWithPlaceholderResolvingToValidUrl() {
initLogging("${some.prop.name:classpath:}" + PROPERTIES_LOCATION, true);
}
@Test
public void initLoggingWithUrl() {
URL url = Log4jWebConfigurerTests.class.getResource(TESTLOG4J_PROPERTIES);