ASF Bugzilla – Attachment 28578 Details for
Bug 52913
Configure from an InputStream
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Updated patch for 1.2.17 (with unit tests)
log4j-52913.diff (text/plain), 15.17 KB, created by
Gary Gregory
on 2012-04-11 02:05:59 UTC
(
hide
)
Description:
Updated patch for 1.2.17 (with unit tests)
Filename:
MIME Type:
Creator:
Gary Gregory
Created:
2012-04-11 02:05:59 UTC
Size:
15.17 KB
patch
obsolete
>Index: . >=================================================================== >--- . (revision 1312006) >+++ . (working copy) > >Property changes on: . >___________________________________________________________________ >Modified: svn:ignore >## -8,5 +8,4 ## > .project > .settings > log4j.i* >- >- >+output >Index: src/main/java/org/apache/log4j/helpers/OptionConverter.java >=================================================================== >--- src/main/java/org/apache/log4j/helpers/OptionConverter.java (revision 1312006) >+++ src/main/java/org/apache/log4j/helpers/OptionConverter.java (working copy) >@@ -17,14 +17,15 @@ > > package org.apache.log4j.helpers; > >+import java.io.InputStream; >+import java.io.InterruptedIOException; >+import java.net.URL; > import java.util.Properties; >-import java.net.URL; >-import java.io.InterruptedIOException; > > import org.apache.log4j.Level; >+import org.apache.log4j.PropertyConfigurator; > import org.apache.log4j.spi.Configurator; > import org.apache.log4j.spi.LoggerRepository; >-import org.apache.log4j.PropertyConfigurator; > > // Contributors: Avy Sharell (sharell@online.fr) > // Matthieu Verbert (mve@zurich.ibm.com) >@@ -435,7 +436,49 @@ > } > } > >+ /** >+ * Configure log4j given an {@link InputStream}. >+ * >+ * <p> >+ * The InputStream will be interpreted by a new instance of a log4j configurator. >+ * </p> >+ * <p> >+ * All configurations steps are taken on the <code>hierarchy</code> passed as a parameter. >+ * </p> >+ * >+ * @param inputStream >+ * The configuration input stream. >+ * @param clazz >+ * The class name, of the log4j configurator which will parse the <code>inputStream</code>. This must be a >+ * subclass of {@link Configurator}, or null. If this value is null then a default configurator of >+ * {@link PropertyConfigurator} is used. >+ * @param hierarchy >+ * The {@link org.apache.log4j.Hierarchy} to act on. >+ * @since 1.2.17 >+ */ > >+static >+public >+void selectAndConfigure(InputStream inputStream, String clazz, LoggerRepository hierarchy) { >+Configurator configurator = null; >+ >+if(clazz != null) { >+ LogLog.debug("Preferred configurator class: " + clazz); >+ configurator = (Configurator) instantiateByClassName(clazz, >+ Configurator.class, >+ null); >+ if(configurator == null) { >+ LogLog.error("Could not instantiate configurator ["+clazz+"]."); >+ return; >+ } >+} else { >+ configurator = new PropertyConfigurator(); >+} >+ >+configurator.doConfigure(inputStream, hierarchy); >+} >+ >+ > /** > Configure log4j given a URL. > >Index: src/main/java/org/apache/log4j/lf5/DefaultLF5Configurator.java >=================================================================== >--- src/main/java/org/apache/log4j/lf5/DefaultLF5Configurator.java (revision 1312006) >+++ src/main/java/org/apache/log4j/lf5/DefaultLF5Configurator.java (working copy) >@@ -17,13 +17,14 @@ > > package org.apache.log4j.lf5; > >+import java.io.IOException; >+import java.io.InputStream; >+import java.net.URL; >+ > import org.apache.log4j.PropertyConfigurator; > import org.apache.log4j.spi.Configurator; > import org.apache.log4j.spi.LoggerRepository; > >-import java.io.IOException; >-import java.net.URL; >- > /** > * The <code>DefaultLF5Configurator</code> provides a default > * configuration for the <code>LF5Appender</code>. >@@ -92,10 +93,19 @@ > /** > * This is a dummy method that will throw an > * <code>IllegalStateException</code> if used. >+ * >+ * @since 1.2.17 > */ >+ public void doConfigure(InputStream inputStream, LoggerRepository repository) { >+ throw new IllegalStateException("This class should NOT be instantiated!"); >+ } >+ >+ /** >+ * This is a dummy method that will throw an >+ * <code>IllegalStateException</code> if used. >+ */ > public void doConfigure(URL configURL, LoggerRepository repository) { >- throw new IllegalStateException("This class should NOT be" + >- " instantiated!"); >+ throw new IllegalStateException("This class should NOT be instantiated!"); > } > > //-------------------------------------------------------------------------- >Index: src/main/java/org/apache/log4j/spi/Configurator.java >=================================================================== >--- src/main/java/org/apache/log4j/spi/Configurator.java (revision 1312006) >+++ src/main/java/org/apache/log4j/spi/Configurator.java (working copy) >@@ -17,6 +17,7 @@ > > package org.apache.log4j.spi; > >+import java.io.InputStream; > import java.net.URL; > > /** >@@ -42,6 +43,19 @@ > > > /** >+ Interpret a resource pointed by a InputStream and set up log4j accordingly. >+ >+ The configuration is done relative to the <code>hierarchy</code> >+ parameter. >+ >+ @param inputStream The InputStream to parse >+ @param repository The hierarchy to operation upon. >+ >+ @since 1.2.17 >+ */ >+ void doConfigure(InputStream inputStream, LoggerRepository repository); >+ >+ /** > Interpret a resource pointed by a URL and set up log4j accordingly. > > The configuration is done relative to the <code>hierarchy</code> >Index: src/main/java/org/apache/log4j/varia/ReloadingPropertyConfigurator.java >=================================================================== >--- src/main/java/org/apache/log4j/varia/ReloadingPropertyConfigurator.java (revision 1312006) >+++ src/main/java/org/apache/log4j/varia/ReloadingPropertyConfigurator.java (working copy) >@@ -17,22 +17,27 @@ > > package org.apache.log4j.varia; > >+import java.io.InputStream; >+import java.net.URL; >+ > import org.apache.log4j.PropertyConfigurator; > import org.apache.log4j.spi.Configurator; >-import java.net.URL; >-import org.apache.log4j.spi.LoggerRepository; >+import org.apache.log4j.spi.LoggerRepository; > > public class ReloadingPropertyConfigurator implements Configurator { > >+ PropertyConfigurator delegate = new PropertyConfigurator(); > >- PropertyConfigurator delegate = new PropertyConfigurator(); >+ public ReloadingPropertyConfigurator() { >+ } > >- >- public ReloadingPropertyConfigurator() { >- } >+ /** >+ * @since 1.2.17 >+ */ >+ public void doConfigure(InputStream inputStream, LoggerRepository repository) { >+ } > >- public >- void doConfigure(URL url, LoggerRepository repository) { >- } >+ public void doConfigure(URL url, LoggerRepository repository) { >+ } > > } >Index: src/main/java/org/apache/log4j/PropertyConfigurator.java >=================================================================== >--- src/main/java/org/apache/log4j/PropertyConfigurator.java (revision 1312006) >+++ src/main/java/org/apache/log4j/PropertyConfigurator.java (working copy) >@@ -405,18 +405,30 @@ > } > > /** >- Read configuration options from url <code>configURL</code>. >+ Read configuration options from url <code>configURL</code>. > >- @since 0.8.2 >- */ >- public >- static >- void configure(java.net.URL configURL) { >- new PropertyConfigurator().doConfigure(configURL, >- LogManager.getLoggerRepository()); >- } >+ @since 0.8.2 >+*/ >+public >+static >+void configure(java.net.URL configURL) { >+ new PropertyConfigurator().doConfigure(configURL, >+ LogManager.getLoggerRepository()); >+} > >+/** >+Reads configuration options from an InputStream. > >+@since 1.2.17 >+*/ >+public >+static >+void configure(InputStream inputStream) { >+new PropertyConfigurator().doConfigure(inputStream, >+ LogManager.getLoggerRepository()); >+} >+ >+ > /** > Read configuration options from <code>properties</code>. > >@@ -509,6 +521,27 @@ > registry.clear(); > } > >+ /** >+ * Read configuration options from url <code>configURL</code>. >+ * >+ * @since 1.2.17 >+ */ >+ public void doConfigure(InputStream inputStream, LoggerRepository hierarchy) { >+ Properties props = new Properties(); >+ try { >+ props.load(inputStream); >+ } catch (IOException e) { >+ if (e instanceof InterruptedIOException) { >+ Thread.currentThread().interrupt(); >+ } >+ LogLog.error("Could not read configuration file from InputStream [" + inputStream >+ + "].", e); >+ LogLog.error("Ignoring configuration InputStream [" + inputStream +"]."); >+ return; >+ } >+ this.doConfigure(props, hierarchy); >+ } >+ > /** > Read configuration options from url <code>configURL</code>. > */ >Index: tests/src/java/org/apache/log4j/helpers/OptionConverterTestCase.java >=================================================================== >--- tests/src/java/org/apache/log4j/helpers/OptionConverterTestCase.java (revision 1312006) >+++ tests/src/java/org/apache/log4j/helpers/OptionConverterTestCase.java (working copy) >@@ -20,14 +20,20 @@ > > package org.apache.log4j.helpers; > >-import org.apache.log4j.Level; >-import org.apache.log4j.xml.XLevel; >+import java.io.File; >+import java.io.FileInputStream; >+import java.io.IOException; >+import java.util.Properties; > >+import junit.framework.Test; > import junit.framework.TestCase; > import junit.framework.TestSuite; >-import junit.framework.Test; >-import java.util.Properties; > >+import org.apache.log4j.Level; >+import org.apache.log4j.LogManager; >+import org.apache.log4j.PropertyConfiguratorTest; >+import org.apache.log4j.xml.XLevel; >+ > /** > Test variable substitution code. > @author Ceki Gülcü >@@ -48,6 +54,10 @@ > props.put("TOTO", "wonderful"); > props.put("key1", "value1"); > props.put("key2", "value2"); >+ // Log4J will NPE without this: >+ props.put("line.separator", System.getProperty("line.separator")); >+ // Log4J will throw an Error without this: >+ props.put("java.home", System.getProperty("java.home")); > System.setProperties(props); > > >@@ -56,6 +66,7 @@ > public > void tearDown() { > props = null; >+ LogManager.resetConfiguration(); > } > > public >@@ -113,6 +124,23 @@ > assertEquals(res, "x1"); > } > >+ /** >+ * Tests configuring Log4J from an InputStream. >+ * >+ * @since 1.2.17 >+ */ >+ public void testInputStream() throws IOException { >+ File file = new File("input/filter1.properties"); >+ assertTrue(file.exists()); >+ FileInputStream inputStream = new FileInputStream(file); >+ try { >+ OptionConverter.selectAndConfigure(inputStream, null, LogManager.getLoggerRepository()); >+ } finally { >+ inputStream.close(); >+ } >+ new PropertyConfiguratorTest(this.getClass().getName()).validateNested(); >+ } >+ > public > void toLevelTest1() { > String val = "INFO"; >@@ -148,7 +176,6 @@ > assertEquals(p, null); > } > >- > public > static > Test suite() { >@@ -159,6 +186,7 @@ > suite.addTest(new OptionConverterTestCase("varSubstTest3")); > suite.addTest(new OptionConverterTestCase("varSubstTest4")); > >+ suite.addTest(new OptionConverterTestCase("testInputStream")); > > suite.addTest(new OptionConverterTestCase("toLevelTest1")); > suite.addTest(new OptionConverterTestCase("toLevelTest2")); >Index: tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java >=================================================================== >--- tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java (revision 1312006) >+++ tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java (working copy) >@@ -16,9 +16,8 @@ > */ > package org.apache.log4j; > >-import junit.framework.TestCase; >- > import java.io.File; >+import java.io.FileInputStream; > import java.io.FileOutputStream; > import java.io.FileWriter; > import java.io.IOException; >@@ -27,9 +26,11 @@ > import java.util.zip.ZipEntry; > import java.util.zip.ZipOutputStream; > >-import org.apache.log4j.spi.OptionHandler; >+import junit.framework.TestCase; >+ > import org.apache.log4j.spi.Filter; > import org.apache.log4j.spi.LoggingEvent; >+import org.apache.log4j.spi.OptionHandler; > import org.apache.log4j.spi.ThrowableRenderer; > import org.apache.log4j.spi.ThrowableRendererSupport; > import org.apache.log4j.varia.LevelRangeFilter; >@@ -94,6 +95,39 @@ > } > > /** >+ * Tests configuring Log4J from an InputStream. >+ * >+ * @since 1.2.17 >+ */ >+ public void testInputStream() throws IOException { >+ File file = new File("input/filter1.properties"); >+ assertTrue(file.exists()); >+ FileInputStream inputStream = new FileInputStream(file); >+ try { >+ PropertyConfigurator.configure(inputStream); >+ } finally { >+ inputStream.close(); >+ } >+ this.validateNested(); >+ LogManager.resetConfiguration(); >+ } >+ >+ public void validateNested() { >+ RollingFileAppender rfa = (RollingFileAppender) >+ Logger.getLogger("org.apache.log4j.PropertyConfiguratorTest") >+ .getAppender("ROLLING"); >+ FixedWindowRollingPolicy rollingPolicy = (FixedWindowRollingPolicy) rfa.getRollingPolicy(); >+ assertEquals("filterBase-test1.log", rollingPolicy.getActiveFileName()); >+ assertEquals("filterBased-test1.%i", rollingPolicy.getFileNamePattern()); >+ assertEquals(0, rollingPolicy.getMinIndex()); >+ assertTrue(rollingPolicy.isActivated()); >+ FilterBasedTriggeringPolicy triggeringPolicy = >+ (FilterBasedTriggeringPolicy) rfa.getTriggeringPolicy(); >+ LevelRangeFilter filter = (LevelRangeFilter) triggeringPolicy.getFilter(); >+ assertTrue(Level.INFO.equals(filter.getLevelMin())); >+ } >+ >+ /** > * Test for bug 47465. > * configure(URL) did not close opened JarURLConnection. > * @throws IOException if IOException creating properties jar. >@@ -274,24 +308,15 @@ > } > > /** >- * Test processing of nested objects, see bug 36384. >- * >+ * Tests processing of nested objects, see bug 36384. > */ > public void testNested() { >- PropertyConfigurator.configure("input/filter1.properties"); >- RollingFileAppender rfa = (RollingFileAppender) >- Logger.getLogger("org.apache.log4j.PropertyConfiguratorTest") >- .getAppender("ROLLING"); >- FixedWindowRollingPolicy rollingPolicy = (FixedWindowRollingPolicy) rfa.getRollingPolicy(); >- assertEquals("filterBase-test1.log", rollingPolicy.getActiveFileName()); >- assertEquals("filterBased-test1.%i", rollingPolicy.getFileNamePattern()); >- assertEquals(0, rollingPolicy.getMinIndex()); >- assertTrue(rollingPolicy.isActivated()); >- FilterBasedTriggeringPolicy triggeringPolicy = >- (FilterBasedTriggeringPolicy) rfa.getTriggeringPolicy(); >- LevelRangeFilter filter = (LevelRangeFilter) triggeringPolicy.getFilter(); >- assertTrue(Level.INFO.equals(filter.getLevelMin())); >- LogManager.resetConfiguration(); >+ try { >+ PropertyConfigurator.configure("input/filter1.properties"); >+ this.validateNested(); >+ } finally { >+ LogManager.resetConfiguration(); >+ } > } > > >Index: tests/temp >=================================================================== >--- tests/temp (revision 0) >+++ tests/temp (working copy) >@@ -0,0 +1,2 @@ >+INFO org.apache.log4j.rewrite.RewriteAppenderTest - p1:Hello p2:World Message 0 >+INFO org.apache.log4j.rewrite.RewriteAppenderTest - p1:Hola p2:World Message 1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 52913
:
28469
| 28578