1 16 17 19 package org.apache.log4j.config; 20 21 import java.beans.Introspector ; 22 import java.beans.PropertyDescriptor ; 23 import java.beans.BeanInfo ; 24 import java.beans.IntrospectionException ; 25 import java.lang.reflect.*; 26 import java.util.*; 27 import org.apache.log4j.*; 28 import org.apache.log4j.helpers.LogLog; 29 import org.apache.log4j.helpers.OptionConverter; 30 import org.apache.log4j.spi.OptionHandler; 31 32 53 public class PropertySetter { 54 protected Object obj; 55 protected PropertyDescriptor [] props; 56 57 63 public 64 PropertySetter(Object obj) { 65 this.obj = obj; 66 } 67 68 72 protected 73 void introspect() { 74 try { 75 BeanInfo bi = Introspector.getBeanInfo(obj.getClass()); 76 props = bi.getPropertyDescriptors(); 77 } catch (IntrospectionException ex) { 78 LogLog.error("Failed to introspect "+obj+": " + ex.getMessage()); 79 props = new PropertyDescriptor [0]; 80 } 81 } 82 83 84 93 public 94 static 95 void setProperties(Object obj, Properties properties, String prefix) { 96 new PropertySetter(obj).setProperties(properties, prefix); 97 } 98 99 100 106 public 107 void setProperties(Properties properties, String prefix) { 108 int len = prefix.length(); 109 110 for (Enumeration e = properties.propertyNames(); e.hasMoreElements(); ) { 111 String key = (String ) e.nextElement(); 112 113 if (key.startsWith(prefix)) { 115 116 117 if (key.indexOf('.', len + 1) > 0) { 119 continue; 122 } 123 124 String value = OptionConverter.findAndSubst(key, properties); 125 key = key.substring(len); 126 if ("layout".equals(key) && obj instanceof Appender) { 127 continue; 128 } 129 setProperty(key, value); 130 } 131 } 132 activate(); 133 } 134 135 150 public 151 void setProperty(String name, String value) { 152 if (value == null) return; 153 154 name = Introspector.decapitalize(name); 155 PropertyDescriptor prop = getPropertyDescriptor(name); 156 157 159 if (prop == null) { 160 LogLog.warn("No such property [" + name + "] in "+ 161 obj.getClass().getName()+"." ); 162 } else { 163 try { 164 setProperty(prop, name, value); 165 } catch (PropertySetterException ex) { 166 LogLog.warn("Failed to set property [" + name + 167 "] to value \"" + value + "\". ", ex.rootCause); 168 } 169 } 170 } 171 172 180 public 181 void setProperty(PropertyDescriptor prop, String name, String value) 182 throws PropertySetterException { 183 Method setter = prop.getWriteMethod(); 184 if (setter == null) { 185 throw new PropertySetterException("No setter for property ["+name+"]."); 186 } 187 Class [] paramTypes = setter.getParameterTypes(); 188 if (paramTypes.length != 1) { 189 throw new PropertySetterException("#params for setter != 1"); 190 } 191 192 Object arg; 193 try { 194 arg = convertArg(value, paramTypes[0]); 195 } catch (Throwable t) { 196 throw new PropertySetterException("Conversion to type ["+paramTypes[0]+ 197 "] failed. Reason: "+t); 198 } 199 if (arg == null) { 200 throw new PropertySetterException( 201 "Conversion to type ["+paramTypes[0]+"] failed."); 202 } 203 LogLog.debug("Setting property [" + name + "] to [" +arg+"]."); 204 try { 205 setter.invoke(obj, new Object [] { arg }); 206 } catch (Exception ex) { 207 throw new PropertySetterException(ex); 208 } 209 } 210 211 212 216 protected 217 Object convertArg(String val, Class type) { 218 if(val == null) 219 return null; 220 221 String v = val.trim(); 222 if (String .class.isAssignableFrom(type)) { 223 return val; 224 } else if (Integer.TYPE.isAssignableFrom(type)) { 225 return new Integer (v); 226 } else if (Long.TYPE.isAssignableFrom(type)) { 227 return new Long (v); 228 } else if (Boolean.TYPE.isAssignableFrom(type)) { 229 if ("true".equalsIgnoreCase(v)) { 230 return Boolean.TRUE; 231 } else if ("false".equalsIgnoreCase(v)) { 232 return Boolean.FALSE; 233 } 234 } else if (Priority.class.isAssignableFrom(type)) { 235 return OptionConverter.toLevel(v, (Level) Level.DEBUG); 236 } 237 return null; 238 } 239 240 241 protected 242 PropertyDescriptor getPropertyDescriptor(String name) { 243 if (props == null) introspect(); 244 245 for (int i = 0; i < props.length; i++) { 246 if (name.equals(props[i].getName())) { 247 return props[i]; 248 } 249 } 250 return null; 251 } 252 253 public 254 void activate() { 255 if (obj instanceof OptionHandler) { 256 ((OptionHandler) obj).activateOptions(); 257 } 258 } 259 } 260 | Popular Tags |