Author: sdeboy
Date: Sat Oct 3 22:05:57 2009
New Revision: 821433
URL: http://svn.apache.org/viewvc?rev=821433&view=rev
Log:
Applying patch provided by Mark Thomas for bug 47465: Reading configuration
files from a JAR locks the JAR file
Thanks Mark!
Modified:
logging/log4j/trunk/src/main/java/org/apache/log4j/PropertyConfigurator.java
logging/log4j/trunk/src/main/java/org/apache/log4j/xml/DOMConfigurator.java
logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java
logging/log4j/trunk/tests/src/java/org/apache/log4j/xml/DOMTestCase.java
Modified:
logging/log4j/trunk/src/main/java/org/apache/log4j/PropertyConfigurator.java
URL:
http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/PropertyConfigurator.java?rev=821433&r1=821432&r2=821433&view=diff
==============================================================================
---
logging/log4j/trunk/src/main/java/org/apache/log4j/PropertyConfigurator.java
(original)
+++
logging/log4j/trunk/src/main/java/org/apache/log4j/PropertyConfigurator.java
Sat Oct 3 22:05:57 2009
@@ -26,6 +26,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
+import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
@@ -496,8 +497,11 @@
Properties props = new Properties();
LogLog.debug("Reading configuration from URL " + configURL);
InputStream istream = null;
+ URLConnection uConn = null;
try {
- istream = configURL.openStream();
+ uConn = configURL.openConnection();
+ uConn.setUseCaches(false);
+ istream = uConn.getInputStream();
props.load(istream);
}
catch (Exception e) {
Modified:
logging/log4j/trunk/src/main/java/org/apache/log4j/xml/DOMConfigurator.java
URL:
http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/xml/DOMConfigurator.java?rev=821433&r1=821432&r2=821433&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/xml/DOMConfigurator.java
(original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/xml/DOMConfigurator.java
Sat Oct 3 22:05:57 2009
@@ -56,6 +56,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
+import java.net.URLConnection;
import java.util.Hashtable;
import java.util.Properties;
@@ -759,7 +760,9 @@
void doConfigure(final URL url, LoggerRepository repository) {
ParseAction action = new ParseAction() {
public Document parse(final DocumentBuilder parser) throws
SAXException, IOException {
- InputSource src = new InputSource(url.openStream());
+ URLConnection uConn = url.openConnection();
+ uConn.setUseCaches(false);
+ InputSource src = new InputSource(uConn.getInputStream());
src.setSystemId(url.toString());
return parser.parse(src);
}
Modified:
logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java
URL:
http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java?rev=821433&r1=821432&r2=821433&view=diff
==============================================================================
---
logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java
(original)
+++
logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java
Sat Oct 3 22:05:57 2009
@@ -19,10 +19,13 @@
import junit.framework.TestCase;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
import org.apache.log4j.spi.OptionHandler;
import org.apache.log4j.spi.Filter;
@@ -92,6 +95,28 @@
}
/**
+ * Test for bug 47465.
+ * configure(URL) did not close opened JarURLConnection.
+ * @throws IOException if IOException creating properties jar.
+ */
+ public void testJarURL() throws IOException {
+ File dir = new File("output");
+ dir.mkdirs();
+ File file = new File("output/properties.jar");
+ ZipOutputStream zos =
+ new ZipOutputStream(new FileOutputStream(file));
+ zos.putNextEntry(new ZipEntry(LogManager.DEFAULT_CONFIGURATION_FILE));
+ zos.write("log4j.rootLogger=debug".getBytes());
+ zos.closeEntry();
+ zos.close();
+ URL url = new URL("jar:" + file.toURL() + "!/" +
+ LogManager.DEFAULT_CONFIGURATION_FILE);
+ PropertyConfigurator.configure(url);
+ assertTrue(file.delete());
+ assertFalse(file.exists());
+ }
+
+ /**
* Test processing of log4j.reset property, see bug 17531.
*
*/
Modified:
logging/log4j/trunk/tests/src/java/org/apache/log4j/xml/DOMTestCase.java
URL:
http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/xml/DOMTestCase.java?rev=821433&r1=821432&r2=821433&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/xml/DOMTestCase.java
(original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/xml/DOMTestCase.java
Sat Oct 3 22:05:57 2009
@@ -40,7 +40,15 @@
import org.apache.log4j.util.SunReflectFilter;
import org.apache.log4j.util.Transformer;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
import java.util.Properties;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
public class DOMTestCase extends TestCase {
@@ -382,4 +390,32 @@
assertEquals(false, renderer.getShowVersion());
}
+ /**
+ * Test for bug 47465.
+ * configure(URL) did not close opened JarURLConnection.
+ * @throws IOException if IOException creating properties jar.
+ */
+ public void testJarURL() throws IOException {
+ File input = new File("input/xml/defaultInit.xml");
+ System.out.println(input.getAbsolutePath());
+ InputStream is = new FileInputStream(input);
+ File dir = new File("output");
+ dir.mkdirs();
+ File file = new File("output/xml.jar");
+ ZipOutputStream zos =
+ new ZipOutputStream(new FileOutputStream(file));
+ zos.putNextEntry(new ZipEntry("log4j.xml"));
+ int len;
+ byte[] buf = new byte[1024];
+ while ((len = is.read(buf)) > 0) {
+ zos.write(buf, 0, len);
+ }
+ zos.closeEntry();
+ zos.close();
+ URL url = new URL("jar:" + file.toURL() + "!/log4j.xml");
+ DOMConfigurator.configure(url);
+ assertTrue(file.delete());
+ assertFalse(file.exists());
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]