From 06632822e9bb7e4555a0060f0c75f4514f69a3e4 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 28 Oct 2014 16:18:55 +0100 Subject: [PATCH] Log4jConfigurer initLogging(location) throws FileNotFoundException for file URL as well Issue: SPR-9725 --- .../java/org/springframework/util/Log4jConfigurer.java | 10 +++++++--- .../java/org/springframework/util/ResourceUtils.java | 9 ++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/Log4jConfigurer.java b/spring-core/src/main/java/org/springframework/util/Log4jConfigurer.java index 655bc0eaca..c0e2f82c9b 100644 --- a/spring-core/src/main/java/org/springframework/util/Log4jConfigurer.java +++ b/spring-core/src/main/java/org/springframework/util/Log4jConfigurer.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"); * you may not use this file except in compliance with the License. @@ -35,8 +35,7 @@ import org.apache.log4j.xml.DOMConfigurator; *

For web environments, the analogous Log4jWebConfigurer class can be found * 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 - * via Log4jConfigListener or Log4jConfigServlet, delegating to - * Log4jWebConfigurer underneath. + * via Log4jConfigListener, delegating to Log4jWebConfigurer underneath. * * @author Juergen Hoeller * @since 13.03.2003 @@ -65,6 +64,10 @@ public abstract class Log4jConfigurer { public static void initLogging(String location) throws FileNotFoundException { String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location); 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)) { DOMConfigurator.configure(url); } @@ -98,6 +101,7 @@ public abstract class Log4jConfigurer { if (!file.exists()) { throw new FileNotFoundException("Log4j config file [" + resolvedLocation + "] not found"); } + if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) { DOMConfigurator.configureAndWatch(file.getAbsolutePath(), refreshInterval); } diff --git a/spring-core/src/main/java/org/springframework/util/ResourceUtils.java b/spring-core/src/main/java/org/springframework/util/ResourceUtils.java index 9cade42b6d..4962e503fa 100644 --- a/spring-core/src/main/java/org/springframework/util/ResourceUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ResourceUtils.java @@ -129,8 +129,8 @@ public abstract class ResourceUtils { URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path)); if (url == null) { String description = "class path resource [" + path + "]"; - throw new FileNotFoundException( - description + " cannot be resolved to URL because it does not exist"); + throw new FileNotFoundException(description + + " cannot be resolved to URL because it does not exist"); } return url; } @@ -169,9 +169,8 @@ public abstract class ResourceUtils { ClassLoader cl = ClassUtils.getDefaultClassLoader(); URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path)); if (url == null) { - throw new FileNotFoundException( - description + " cannot be resolved to absolute file path " + - "because it does not reside in the file system"); + throw new FileNotFoundException(description + + " cannot be resolved to absolute file path because it does not exist"); } return getFile(url, description); }