KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > util > ReflectiveConfigurator


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

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 /**
31  * A creator that configures a component based on reflection.
32  *
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  * @version CVS $Revision: 1.1 $ $Date: 2004/03/08 18:50:25 $
35  */

36 public final class ReflectiveConfigurator
37     extends AbstractCreator
38 {
39
40     /* (non-Javadoc)
41      * @see org.apache.avalon.lifecycle.Creator#create(java.lang.Object, org.apache.avalon.framework.context.Context)
42      */

43     public void create(Object JavaDoc component, Context context)
44     throws Exception JavaDoc
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 JavaDoc[] 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 JavaDoc 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