| Method from org.apache.log4j.config.PropertySetter Detail: |
public void activate() {
if (obj instanceof OptionHandler) {
((OptionHandler) obj).activateOptions();
}
}
|
protected 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(v);
} else if (Long.TYPE.isAssignableFrom(type)) {
return new Long(v);
} else if (Boolean.TYPE.isAssignableFrom(type)) {
if ("true".equalsIgnoreCase(v)) {
return Boolean.TRUE;
} else if ("false".equalsIgnoreCase(v)) {
return Boolean.FALSE;
}
} else if (Priority.class.isAssignableFrom(type)) {
return OptionConverter.toLevel(v, (Level) Level.DEBUG);
}
return null;
}
Convert val a String parameter to an object of a
given type. |
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];
}
}
return null;
}
|
protected void introspect() {
try {
BeanInfo bi = Introspector.getBeanInfo(obj.getClass());
props = bi.getPropertyDescriptors();
} catch (IntrospectionException ex) {
LogLog.error("Failed to introspect "+obj+": " + ex.getMessage());
props = new PropertyDescriptor[0];
}
}
Uses JavaBeans Introspector to computer setters of object to be
configured. |
public void setProperties(Properties properties,
String prefix) {
int len = prefix.length();
for (Enumeration e = properties.propertyNames(); e.hasMoreElements(); ) {
String key = (String) e.nextElement();
// handle only properties that start with the desired frefix.
if (key.startsWith(prefix)) {
// ignore key if it contains dots after the prefix
if (key.indexOf('.", len + 1) > 0) {
//System.err.println("----------Ignoring---["+key
// +"], prefix=["+prefix+"].");
continue;
}
String value = OptionConverter.findAndSubst(key, properties);
key = key.substring(len);
if ("layout".equals(key) && obj instanceof Appender) {
continue;
}
setProperty(key, value);
}
}
activate();
}
Set the properites for the object that match the
prefix passed as parameter. |
public static void setProperties(Object obj,
Properties properties,
String prefix) {
new PropertySetter(obj).setProperties(properties, prefix);
}
Set the properties of an object passed as a parameter in one
go. The properties are parsed relative to a
prefix. |
public void setProperty(String name,
String value) {
if (value == null) return;
name = Introspector.decapitalize(name);
PropertyDescriptor prop = getPropertyDescriptor(name);
//LogLog.debug("---------Key: "+name+", type="+prop.getPropertyType());
if (prop == null) {
LogLog.warn("No such property [" + name + "] in "+
obj.getClass().getName()+"." );
} else {
try {
setProperty(prop, name, value);
} catch (PropertySetterException ex) {
LogLog.warn("Failed to set property [" + name +
"] to value \"" + value + "\". ", ex.rootCause);
}
}
}
Set a property on this PropertySetter's Object. If successful, this
method will invoke a setter method on the underlying Object. The
setter is the one for the specified property name and the value is
determined partly from the setter argument type and partly from the
value specified in the call to this method.
If the setter expects a String no conversion is necessary.
If it expects an int, then an attempt is made to convert 'value'
to an int using new Integer(value). If the setter expects a boolean,
the conversion is by new Boolean(value). |
public void setProperty(PropertyDescriptor prop,
String name,
String value) throws PropertySetterException {
Method setter = prop.getWriteMethod();
if (setter == null) {
throw new PropertySetterException("No setter for property ["+name+"].");
}
Class[] paramTypes = setter.getParameterTypes();
if (paramTypes.length != 1) {
throw new PropertySetterException("#params for setter != 1");
}
Object arg;
try {
arg = convertArg(value, paramTypes[0]);
} catch (Throwable t) {
throw new PropertySetterException("Conversion to type ["+paramTypes[0]+
"] failed. Reason: "+t);
}
if (arg == null) {
throw new PropertySetterException(
"Conversion to type ["+paramTypes[0]+"] failed.");
}
LogLog.debug("Setting property [" + name + "] to [" +arg+"].");
try {
setter.invoke(obj, new Object[] { arg });
} catch (Exception ex) {
throw new PropertySetterException(ex);
}
}
|