From 2503b7eb891824da01ccde3b5035f0030ea757a4 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Thu, 17 May 2012 10:11:46 +0300 Subject: [PATCH] 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 --- .../web/util/Log4jWebConfigurer.java | 4 ++-- .../web/util/Log4jWebConfigurerTests.java | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/Log4jWebConfigurer.java b/spring-web/src/main/java/org/springframework/web/util/Log4jWebConfigurer.java index 729df79bb54..37648904300 100644 --- a/spring-web/src/main/java/org/springframework/web/util/Log4jWebConfigurer.java +++ b/spring-web/src/main/java/org/springframework/web/util/Log4jWebConfigurer.java @@ -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); } diff --git a/spring-web/src/test/java/org/springframework/web/util/Log4jWebConfigurerTests.java b/spring-web/src/test/java/org/springframework/web/util/Log4jWebConfigurerTests.java index 0fa17f77aee..075dbc1195c 100644 --- a/spring-web/src/test/java/org/springframework/web/util/Log4jWebConfigurerTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/Log4jWebConfigurerTests.java @@ -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);