You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ca...@apache.org on 2007/02/23 06:09:15 UTC

svn commit: r510831 - in /logging/log4j/branches/v1_2-branch: src/java/org/apache/log4j/PropertyConfigurator.java tests/.cvsignore tests/src/java/org/apache/log4j/CoreTestSuite.java tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java

Author: carnold
Date: Thu Feb 22 21:09:14 2007
New Revision: 510831

URL: http://svn.apache.org/viewvc?view=rev&rev=510831
Log:
Bug 40944: PropertyConfigurator.configure(URL) does not close the resource stream

Added:
    logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java
Removed:
    logging/log4j/branches/v1_2-branch/tests/.cvsignore
Modified:
    logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/PropertyConfigurator.java
    logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/CoreTestSuite.java

Modified: logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/PropertyConfigurator.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/PropertyConfigurator.java?view=diff&rev=510831&r1=510830&r2=510831
==============================================================================
--- logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/PropertyConfigurator.java (original)
+++ logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/PropertyConfigurator.java Thu Feb 22 21:09:14 2007
@@ -38,6 +38,7 @@
 import java.util.Properties;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.StringTokenizer;
 import java.util.Hashtable;
 
@@ -302,15 +303,24 @@
   public
   void doConfigure(String configFileName, LoggerRepository hierarchy) {
     Properties props = new Properties();
+    FileInputStream istream = null;
     try {
-      FileInputStream istream = new FileInputStream(configFileName);
+      istream = new FileInputStream(configFileName);
       props.load(istream);
       istream.close();
     }
-    catch (IOException e) {
+    catch (Exception e) {
       LogLog.error("Could not read configuration file ["+configFileName+"].", e);
       LogLog.error("Ignoring configuration file [" + configFileName+"].");
       return;
+    } finally {
+        if(istream != null) {
+            try {
+                istream.close();
+            } catch(Throwable ignore) {
+            }
+
+        }
     }
     // If we reach here, then the config file is alright.
     doConfigure(props, hierarchy);
@@ -429,14 +439,24 @@
   void doConfigure(java.net.URL configURL, LoggerRepository hierarchy) {
     Properties props = new Properties();
     LogLog.debug("Reading configuration from URL " + configURL);
+    InputStream istream = null;
     try {
-      props.load(configURL.openStream());
+      istream = configURL.openStream();
+      props.load(istream);
     }
-    catch (java.io.IOException e) {
+    catch (Exception e) {
       LogLog.error("Could not read configuration file from URL [" + configURL
 		   + "].", e);
       LogLog.error("Ignoring configuration file [" + configURL +"].");
       return;
+    }
+    finally {
+        if (istream != null) {
+            try {
+                istream.close();
+            } catch(Exception ignore) {
+            }
+        }
     }
     doConfigure(props, hierarchy);
   }

Modified: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/CoreTestSuite.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/CoreTestSuite.java?view=diff&rev=510831&r1=510830&r2=510831
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/CoreTestSuite.java (original)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/CoreTestSuite.java Thu Feb 22 21:09:14 2007
@@ -47,6 +47,7 @@
         s.addTestSuite(org.apache.log4j.PatternLayoutTest.class);
         s.addTestSuite(org.apache.log4j.spi.LoggingEventTest.class);
         s.addTestSuite(org.apache.log4j.spi.ThrowableInformationTest.class);
+        s.addTestSuite(org.apache.log4j.PropertyConfigurator.class);
         return s;
     }
 }

Added: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java?view=auto&rev=510831
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java (added)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java Thu Feb 22 21:09:14 2007
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.log4j;
+import java.io.*;
+import junit.framework.*;
+import org.apache.log4j.PropertyConfigurator;
+import java.net.URL;
+
+/**
+ * Test property configurator.
+ *
+ */
+public class PropertyConfiguratorTest extends TestCase {
+    public PropertyConfiguratorTest(final String testName) {
+        super(testName);
+    }
+
+    /**
+     * Test for bug 19108.
+     * Did not catch IllegalArgumentException on Properties.load
+     * and close input stream.
+     * @throws IOException if IOException creating properties file.
+     */
+    public void testBadUnicodeEscape() throws IOException {
+        String fileName = "output/badescape.properties";
+        FileWriter writer = new FileWriter(fileName);
+        writer.write("log4j.rootLogger=\\uXX41");
+        writer.close();
+        PropertyConfigurator.configure(fileName);
+        File file = new File(fileName);
+        assertTrue(file.delete()) ;
+        assertFalse(file.exists());
+    }
+
+    /**
+     * Test for bug 19108.
+     * configure(URL) never closed opened stream.
+     * @throws IOException if IOException creating properties file.
+     */
+        public void testURL() throws IOException {
+        File file = new File("output/unclosed.properties");
+        FileWriter writer = new FileWriter(file);
+        writer.write("log4j.rootLogger=debug");
+        writer.close();
+        URL url = file.toURL();
+        PropertyConfigurator.configure(url);
+        assertTrue(file.delete());
+        assertFalse(file.exists());
+    }
+
+    /**
+     * Test for bug 19108.
+     * configure(URL) did not catch IllegalArgumentException and
+     * did not close stream.
+     * @throws IOException if IOException creating properties file.
+     */
+        public void testURLBadEscape() throws IOException {
+        File file = new File("output/urlbadescape.properties");
+        FileWriter writer = new FileWriter(file);
+        writer.write("log4j.rootLogger=\\uXX41");
+        writer.close();
+        URL url = file.toURL();
+        PropertyConfigurator.configure(url);
+        assertTrue(file.delete());
+        assertFalse(file.exists());
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org