From 12f0edf24bec1907b1a196375cc5ccd3ba9ffefa Mon Sep 17 00:00:00 2001 From: Simon Kitching Date: Sun, 14 Sep 2008 12:26:03 +0000 Subject: [PATCH] Fix LOGGING-126: loading of commons-logging.properties file locks jars on windows. Thanks to Philippe Mouawad for the report and patch. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/logging/trunk@695208 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/logging/LogFactory.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/commons/logging/LogFactory.java b/src/java/org/apache/commons/logging/LogFactory.java index 13f8ea3..2a15a9c 100644 --- a/src/java/org/apache/commons/logging/LogFactory.java +++ b/src/java/org/apache/commons/logging/LogFactory.java @@ -27,6 +27,7 @@ import java.io.PrintStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; +import java.net.URLConnection; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Enumeration; @@ -1420,18 +1421,36 @@ public abstract class LogFactory { PrivilegedAction action = new PrivilegedAction() { public Object run() { + InputStream stream = null; try { - InputStream stream = url.openStream(); + // We must ensure that useCaches is set to false, as the + // default behaviour of java is to cache file handles, and + // this "locks" files, preventing hot-redeploy on windows. + URLConnection connection = url.openConnection(); + connection.setDefaultUseCaches(false); + stream = connection.getInputStream(); if (stream != null) { Properties props = new Properties(); props.load(stream); stream.close(); + stream = null; return props; } } catch(IOException e) { if (isDiagnosticsEnabled()) { logDiagnostic("Unable to read URL " + url); } + } finally { + if (stream != null) { + try { + stream.close(); + } catch(Throwable t) { + // ignore exception; this should not happen + if (isDiagnosticsEnabled()) { + logDiagnostic("Unable to close stream for URL " + url); + } + } + } } return null;