Log4jConfigurer initLogging(location) throws FileNotFoundException for file URL as well

Issue: SPR-9725
master
Juergen Hoeller 10 years ago
parent 17b9bde336
commit 06632822e9
  1. 10
      spring-core/src/main/java/org/springframework/util/Log4jConfigurer.java
  2. 9
      spring-core/src/main/java/org/springframework/util/ResourceUtils.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -35,8 +35,7 @@ import org.apache.log4j.xml.DOMConfigurator;
* <p>For web environments, the analogous Log4jWebConfigurer class can be found * <p>For web environments, the analogous Log4jWebConfigurer class can be found
* in the web package, reading in its configuration from context-params in * in the web package, reading in its configuration from context-params in
* {@code web.xml}. In a J2EE web application, log4j is usually set up * {@code web.xml}. In a J2EE web application, log4j is usually set up
* via Log4jConfigListener or Log4jConfigServlet, delegating to * via Log4jConfigListener, delegating to Log4jWebConfigurer underneath.
* Log4jWebConfigurer underneath.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 13.03.2003 * @since 13.03.2003
@ -65,6 +64,10 @@ public abstract class Log4jConfigurer {
public static void initLogging(String location) throws FileNotFoundException { public static void initLogging(String location) throws FileNotFoundException {
String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location); String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location);
URL url = ResourceUtils.getURL(resolvedLocation); URL url = ResourceUtils.getURL(resolvedLocation);
if (ResourceUtils.URL_PROTOCOL_FILE.equals(url.getProtocol()) && !ResourceUtils.getFile(url).exists()) {
throw new FileNotFoundException("Log4j config file [" + resolvedLocation + "] not found");
}
if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) { if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {
DOMConfigurator.configure(url); DOMConfigurator.configure(url);
} }
@ -98,6 +101,7 @@ public abstract class Log4jConfigurer {
if (!file.exists()) { if (!file.exists()) {
throw new FileNotFoundException("Log4j config file [" + resolvedLocation + "] not found"); throw new FileNotFoundException("Log4j config file [" + resolvedLocation + "] not found");
} }
if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) { if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {
DOMConfigurator.configureAndWatch(file.getAbsolutePath(), refreshInterval); DOMConfigurator.configureAndWatch(file.getAbsolutePath(), refreshInterval);
} }

@ -129,8 +129,8 @@ public abstract class ResourceUtils {
URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path)); URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path));
if (url == null) { if (url == null) {
String description = "class path resource [" + path + "]"; String description = "class path resource [" + path + "]";
throw new FileNotFoundException( throw new FileNotFoundException(description +
description + " cannot be resolved to URL because it does not exist"); " cannot be resolved to URL because it does not exist");
} }
return url; return url;
} }
@ -169,9 +169,8 @@ public abstract class ResourceUtils {
ClassLoader cl = ClassUtils.getDefaultClassLoader(); ClassLoader cl = ClassUtils.getDefaultClassLoader();
URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path)); URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path));
if (url == null) { if (url == null) {
throw new FileNotFoundException( throw new FileNotFoundException(description +
description + " cannot be resolved to absolute file path " + " cannot be resolved to absolute file path because it does not exist");
"because it does not reside in the file system");
} }
return getFile(url, description); return getFile(url, description);
} }

Loading…
Cancel
Save