ceki 01/04/17 10:14:33 Modified: src/java/org/apache/log4j/config PropertyGetter.java PropertySetter.java src/java/org/apache/log4j/helpers OptionConverter.java src/java/org/apache/log4j/test Makefile shallow src/java/org/apache/log4j/xml DOMConfigurator.java src/java/org/apache/log4j/xml/examples XPriority.java Added: src/java/org/apache/log4j/config package.html src/java/org/apache/log4j/test UnitTestOptionConverter.java Removed: src/java/org/apache/log4j/test UnitTestVarSubst.java Log: Added custom Priority paramater parsing capability to config/PropertySetter.java. Revision Changes Path 1.2 +3 -4 jakarta-log4j/src/java/org/apache/log4j/config/PropertyGetter.java Index: PropertyGetter.java =================================================================== RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/config/PropertyGetter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- PropertyGetter.java 2001/03/19 12:31:41 1.1 +++ PropertyGetter.java 2001/04/17 17:13:52 1.2 @@ -32,10 +32,9 @@ /** Create a new PropertySetter for the specified Object. This is done - in prepartion for invoking {@link #setProperty} one or more times. + in prepartion for invoking {@link #getProperties(PropertyCallback,String)} one or more times. - @param obj the object for which to set properties - */ + @param obj the object for which to set properties */ public PropertyGetter(Object obj) throws IntrospectionException { BeanInfo bi = Introspector.getBeanInfo(obj.getClass()); @@ -83,4 +82,4 @@ Boolean.TYPE.isAssignableFrom(type) || Priority.class.isAssignableFrom(type); } -} \ No newline at end of file +} 1.3 +43 -31 jakarta-log4j/src/java/org/apache/log4j/config/PropertySetter.java Index: PropertySetter.java =================================================================== RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/config/PropertySetter.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- PropertySetter.java 2001/03/21 21:33:55 1.2 +++ PropertySetter.java 2001/04/17 17:13:54 1.3 @@ -68,6 +68,16 @@ } } + + /** + Set the properties of an object passed as a parameter in one + go. The <code>properties</code> are parsed relative to a + <code>prefix</code>. + + @param obj The object to configure. + @param properties A java.util.Properties containing keys and values. + @param prefix Only keys having the specified prefix will be set. + */ public static void setProperties(Object obj, Properties properties, String prefix) { @@ -103,8 +113,7 @@ key = key.substring(len); if ("layout".equals(key) && obj instanceof Appender) { continue; - } - + } setProperty(key, value); } } @@ -139,7 +148,8 @@ PropertyDescriptor prop = getPropertyDescriptor(name); if (prop == null) { - LogLog.warn("No such property: " + name); + LogLog.warn("No such property [" + name + "] in "+ + obj.getClass().getName()+"." ); } else { try { setProperty(prop, name, value); @@ -149,27 +159,18 @@ } } } - - /* - public - void setProperty(String name, String value) throws PropertySetterException { - if (props == null) introspect(); - name = Introspector.decapitalize(name); - PropertyDescriptor prop = getPropertyDescriptor(name); - - if (prop != null) { - setProperty(prop, name, value); - } else { - throw new PropertySetterException( - "No such property", obj, name, value); - } - } - */ + /** + Set the named property given a {@link PropertyDescriptor}. + + @param prop A PropertyDescriptor describing the characteristics + of the property to set. + @param name The named of the property to set. + @param value The value of the property. + */ public void setProperty(PropertyDescriptor prop, String name, String value) - throws PropertySetterException - { + throws PropertySetterException { Method setter = prop.getWriteMethod(); if (setter == null) { throw new PropertySetterException("No setter for property"); @@ -181,7 +182,7 @@ Object arg; try { - arg = getArg(value, paramTypes[0]); + arg = convertArg(value, paramTypes[0]); } catch (Throwable t) { throw new PropertySetterException(t); } @@ -197,33 +198,44 @@ } } + + /** + Convert <code>val</code> a String parameter to an object of a + given type. + */ protected - Object getArg(String val, Class type) { + Object convertArg(String val, Class type) { + if(val == null) + return null; + + String v = val.trim(); if (String.class.isAssignableFrom(type)) { return val; } else if (Integer.TYPE.isAssignableFrom(type)) { - return new Integer(val.trim()); + return new Integer(v); } else if (Long.TYPE.isAssignableFrom(type)) { - return new Long(val.trim()); + return new Long(v); } else if (Boolean.TYPE.isAssignableFrom(type)) { - val = val.trim(); - if ("true".equalsIgnoreCase(val)) { + if ("true".equalsIgnoreCase(v)) { return Boolean.TRUE; - } else if ("false".equalsIgnoreCase(val)) { + } else if ("false".equalsIgnoreCase(v)) { return Boolean.FALSE; } } else if (Priority.class.isAssignableFrom(type)) { - return Priority.toPriority(val.trim()); + return OptionConverter.toPriority(v, null); } return null; } + protected PropertyDescriptor getPropertyDescriptor(String name) { if (props == null) introspect(); for (int i = 0; i < props.length; i++) { - if (name.equals(props[i].getName())) return props[i]; + if (name.equals(props[i].getName())) { + return props[i]; + } } return null; } @@ -231,7 +243,7 @@ public void activate() { if (obj instanceof OptionHandler) { - ((OptionHandler) obj).activateOptions(); + ((OptionHandler) obj).activateOptions(); } } } 1.1 jakarta-log4j/src/java/org/apache/log4j/config/package.html Index: package.html =================================================================== <html> <body> Package used in getting/setting component properties. </body> </html> 1.14 +76 -0 jakarta-log4j/src/java/org/apache/log4j/helpers/OptionConverter.java Index: OptionConverter.java =================================================================== RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/helpers/OptionConverter.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- OptionConverter.java 2001/03/22 18:37:49 1.13 +++ OptionConverter.java 2001/04/17 17:14:02 1.14 @@ -12,6 +12,7 @@ import java.net.URL; import org.apache.log4j.Category; import org.apache.log4j.Hierarchy; +import org.apache.log4j.Priority; import org.apache.log4j.spi.Configurator; import org.apache.log4j.xml.DOMConfigurator; import org.apache.log4j.PropertyConfigurator; @@ -25,6 +26,7 @@ A convenience class to convert property values to specific types. @author Ceki Gülcü + @author Simon Kitching; */ public class OptionConverter { @@ -151,6 +153,80 @@ return dEfault; } + /** + Converts a standard or custom priority level to a Priority + object. <p> If <code>value</code> is of form + "priority#classname", then the specified class' toPriority method + is called to process the specified priority string; if no '#' + character is present, then the default {@link org.log4j.Priority} + class is used to process the priority value. + + <p> If any error occurs while converting the value to a priority, + the dflt value (which may be null) is returned. + + <p> Case of + value is unimportant for the priority level, but is significant + for any class name part present. + + @since 1.1 + */ + public + static + Priority toPriority(String value, Priority defaultValue) { + if(value == null) + return defaultValue; + + int hashIndex = value.indexOf('#'); + if (hashIndex == -1) { + // no class name specified : use standard Priority class + return Priority.toPriority(value, defaultValue); + } + + Priority result = defaultValue; + + String clazz = value.substring(hashIndex+1); + String priorityName = value.substring(0, hashIndex); + + LogLog.debug("toPriority" + ":class=[" + clazz + "]" + + ":pri=[" + priorityName + "]"); + + try { + Class customPriority = Class.forName(clazz); + + // get a ref to the specified class' static method + // toPriority(String, org.apache.log4j.Priority) + Class[] paramTypes = new Class[] { String.class, + org.apache.log4j.Priority.class + }; + java.lang.reflect.Method toPriorityMethod = + customPriority.getMethod("toPriority", paramTypes); + + // now call the toPriority method, passing priority string + default + Object[] params = new Object[] {priorityName, defaultValue}; + Object o = toPriorityMethod.invoke(null, params); + + result = (Priority) o; + } catch(ClassNotFoundException e) { + LogLog.error("custom priority class [" + clazz + "] could not be added."); + } catch(NoSuchMethodException e) { + LogLog.error("custom priority class [" + clazz + "]" + + " does not have a constructor which takes one string parameter", e); + } catch(java.lang.reflect.InvocationTargetException e) { + LogLog.error("custom priority class [" + clazz + "]" + + " could not be instantiated", e); + } catch(ClassCastException e) { + LogLog.error("class [" + clazz + + "] is not a subclass of org.apache.log4j.Priority", e); + } catch(IllegalAccessException e) { + LogLog.error("class ["+clazz+ + "] cannot be instantiated due to access restrictions", e); + } catch(Exception e) { + LogLog.error("class ["+clazz+"], priority ["+priorityName+ + "] conversion failed.", e); + } + return result; + } + public static long toFileSize(String value, long dEfault) { 1.10 +1 -1 jakarta-log4j/src/java/org/apache/log4j/test/Makefile Index: Makefile =================================================================== RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/test/Makefile,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- Makefile 2001/03/21 20:52:12 1.9 +++ Makefile 2001/04/17 17:14:08 1.10 @@ -22,7 +22,7 @@ UnitTestCategory.java\ UnitTestCyclicBuffer.java\ UnitTestBoundedFIFO.java\ - UnitTestVarSubst.java\ + UnitTestOptionConverter.java\ SocketAppenderTest.java\ PrintProperties.java\ UnitTestOR.java\ 1.6 +2 -0 jakarta-log4j/src/java/org/apache/log4j/test/shallow Index: shallow =================================================================== RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/test/shallow,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- shallow 2001/01/20 16:02:22 1.5 +++ shallow 2001/04/17 17:14:12 1.6 @@ -85,9 +85,11 @@ TEST=1 if [ $TEST -ge $start ]; then + makeShallowConfigFile $TEMP DEBUG " " $SIMPLE testShallow LINE_NUMBER fi + TEST=2 if [ $TEST -ge $start ]; then 1.1 jakarta-log4j/src/java/org/apache/log4j/test/UnitTestOptionConverter.java Index: UnitTestOptionConverter.java =================================================================== // Log4j uses the JUnit framework for internal unit testing. JUnit // is available from "http://www.junit.org". package org.apache.log4j.test; import org.apache.log4j.helpers.OptionConverter; import org.apache.log4j.Priority; import org.apache.log4j.xml.examples.XPriority; import junit.framework.TestCase; import junit.framework.TestSuite; import junit.framework.Test; import java.util.Properties; /** Test variable substitution code. @author Ceki Gülcü @since 1.0 */ public class UnitTestOptionConverter extends TestCase { Properties props; public UnitTestOptionConverter(String name) { super(name); } public void setUp() { props = new Properties(); props.put("TOTO", "wonderful"); props.put("key1", "value1"); props.put("key2", "value2"); System.setProperties(props); } public void tearDown() { props = null; } public void varSubstTest1() { String r; r = OptionConverter.substVars("hello world.", null); assertEquals("hello world.", r); r = OptionConverter.substVars("hello ${TOTO} world.", null); assertEquals("hello wonderful world.", r); } public void varSubstTest2() { String r; r = OptionConverter.substVars("Test2 ${key1} mid ${key2} end.", null); assertEquals("Test2 value1 mid value2 end.", r); } public void varSubstTest3() { String r; r = OptionConverter.substVars( "Test3 ${unset} mid ${key1} end.", null); assertEquals("Test3 mid value1 end.", r); } public void varSubstTest4() { String res; String val = "Test4 ${incomplete "; try { res = OptionConverter.substVars(val, null); } catch(IllegalArgumentException e) { String errorMsg = e.getMessage(); //System.out.println('['+errorMsg+']'); assertEquals('"'+val+ "\" has no closing brace. Opening brace at position 6.", errorMsg); } } public void toPriorityTest1() { String val = "INFO"; Priority p = OptionConverter.toPriority(val, null); assertEquals(p, Priority.INFO); } public void toPriorityTest2() { String val = "INFO#org.apache.log4j.xml.examples.XPriority"; Priority p = OptionConverter.toPriority(val, null); assertEquals(p, Priority.INFO); } public void toPriorityTest3() { String val = "TRACE#org.apache.log4j.xml.examples.XPriority"; Priority p = OptionConverter.toPriority(val, null); assertEquals(p, XPriority.TRACE); } public void toPriorityTest4() { String val = "TR#org.apache.log4j.xml.examples.XPriority"; Priority p = OptionConverter.toPriority(val, null); assertEquals(p, null); } public static Test suite() { TestSuite suite = new TestSuite(); suite.addTest(new UnitTestOptionConverter("varSubstTest1")); suite.addTest(new UnitTestOptionConverter("varSubstTest2")); suite.addTest(new UnitTestOptionConverter("varSubstTest3")); suite.addTest(new UnitTestOptionConverter("varSubstTest4")); suite.addTest(new UnitTestOptionConverter("toPriorityTest1")); suite.addTest(new UnitTestOptionConverter("toPriorityTest2")); suite.addTest(new UnitTestOptionConverter("toPriorityTest3")); suite.addTest(new UnitTestOptionConverter("toPriorityTest4")); return suite; } } 1.14 +2 -2 jakarta-log4j/src/java/org/apache/log4j/xml/DOMConfigurator.java Index: DOMConfigurator.java =================================================================== RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/xml/DOMConfigurator.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- DOMConfigurator.java 2001/04/13 00:01:18 1.13 +++ DOMConfigurator.java 2001/04/17 17:14:20 1.14 @@ -50,7 +50,7 @@ <p>There are sample XML files included in the package. - @author <a href=mailto:[EMAIL PROTECTED]>Christopher Taylor</a> + @author Christopher Taylor @author Ceki Gülcü @author Anders Kristensen @@ -112,7 +112,7 @@ } else { Document doc = appenderRef.getOwnerDocument(); - // DOESN'T WORK!! : + // Doesn't work on DOM Level 1 : // Element element = doc.getElementById(appenderName); // Endre's hack: 1.5 +6 -3 jakarta-log4j/src/java/org/apache/log4j/xml/examples/XPriority.java Index: XPriority.java =================================================================== RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/xml/examples/XPriority.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- XPriority.java 2001/02/15 16:22:16 1.4 +++ XPriority.java 2001/04/17 17:14:28 1.5 @@ -29,11 +29,13 @@ super(level, strLevel, syslogEquiv); } + public static - Priority toPriority(String sArg) { + Priority toPriority(String sArg, Priority defaultValue) { + if(sArg == null) { - return XPriority.TRACE; + return defaultValue; } String stringVal = sArg.toUpperCase(); @@ -43,8 +45,9 @@ return XPriority.LETHAL; } - return Priority.toPriority(sArg); + return Priority.toPriority(sArg, defaultValue); } + public static --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]