1 19 20 package org.apache.avalon.fortress.util; 21 22 import org.apache.avalon.framework.configuration.Configurable; 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.context.Context; 25 import org.apache.avalon.framework.parameters.Parameterizable; 26 import org.apache.avalon.framework.parameters.Parameters; 27 import org.apache.avalon.lifecycle.AbstractCreator; 28 import org.apache.commons.beanutils.BeanUtils; 29 30 36 public final class ReflectiveConfigurator 37 extends AbstractCreator 38 { 39 40 43 public void create(Object component, Context context) 44 throws Exception 45 { 46 super.create( component, context ); 47 48 if ( !(component instanceof Parameterizable) 49 && !(component instanceof Configurable) ) 50 { 51 final Configuration conf = (Configuration) context.get("component.configuration"); 52 if ( conf != null && conf.getChildren().length > 0) 53 { 54 final Parameters p = Parameters.fromConfiguration( conf ); 55 String [] names = p.getNames(); 56 for( int i = 0; i < names.length; i++ ) 57 { 58 try 59 { 60 BeanUtils.setProperty( component, names[i], p.getParameter(names[i])); 61 } 62 catch (Exception ignore) 63 { 64 if ( this.getLogger() != null && this.getLogger().isWarnEnabled() ) 65 { 66 this.getLogger().warn("Error while trying to configure " + component 67 + " with parameter: " + names[i], ignore); 68 } 69 } 70 } 71 } 72 } 73 } 74 75 } 76 | Popular Tags |