KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > beanutils > ConfigurationDynaClass


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

16
17 package org.apache.commons.configuration.beanutils;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.commons.beanutils.DynaBean;
24 import org.apache.commons.beanutils.DynaClass;
25 import org.apache.commons.beanutils.DynaProperty;
26 import org.apache.commons.configuration.Configuration;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /**
31  * The <tt>ConfigurationDynaClass</tt> dynamically determines properties for
32  * a <code>ConfigurationDynaBean</code> from a wrapped configuration-collection
33  * {@link org.apache.commons.configuration.Configuration} instance.
34  *
35  * @author <a HREF="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
36  * @version $Revision: 155408 $, $Date: 2005-02-26 13:56:39 +0100 (Sa, 26 Feb 2005) $
37  * @since 1.0-rc1
38  */

39 public class ConfigurationDynaClass implements DynaClass
40 {
41     private final static Log log = LogFactory.getLog(ConfigurationDynaClass.class);
42
43     Configuration configuration;
44
45     /**
46      * Construct an instance of a <code>ConfigurationDynaClass</code>
47      * wrapping the specified <code>Configuration</code> instance.
48      * @param configuration <code>Configuration</code> instance.
49      */

50     public ConfigurationDynaClass(Configuration configuration)
51     {
52         super();
53         if (log.isTraceEnabled()) log.trace("ConfigurationDynaClass(" + configuration + ")");
54         this.configuration = configuration;
55     }
56
57     /**
58      * @see org.apache.commons.beanutils.DynaClass#getDynaProperty(java.lang.String)
59      */

60     public DynaProperty getDynaProperty(String JavaDoc name)
61     {
62         if (log.isTraceEnabled())
63         {
64             log.trace("getDynaProperty(" + name + ")");
65         }
66
67         if (name == null)
68         {
69             throw new IllegalArgumentException JavaDoc("No such property name=[" + name + "]");
70         }
71
72         Object JavaDoc value = configuration.getProperty(name);
73         if (value == null)
74         {
75             return null;
76         }
77         else
78         {
79             Class JavaDoc type = value.getClass();
80
81             if (type == Byte JavaDoc.class)
82             {
83                 type = Byte.TYPE;
84             }
85             if (type == Character JavaDoc.class)
86             {
87                 type = Character.TYPE;
88             }
89             else if (type == Boolean JavaDoc.class)
90             {
91                 type = Boolean.TYPE;
92             }
93             else if (type == Double JavaDoc.class)
94             {
95                 type = Double.TYPE;
96             }
97             else if (type == Float JavaDoc.class)
98             {
99                 type = Float.TYPE;
100             }
101             else if (type == Integer JavaDoc.class)
102             {
103                 type = Integer.TYPE;
104             }
105             else if (type == Long JavaDoc.class)
106             {
107                 type = Long.TYPE;
108             }
109             else if (type == Short JavaDoc.class)
110             {
111                 type = Short.TYPE;
112             }
113
114             return new DynaProperty(name, type);
115         }
116     }
117
118     /**
119      * @see org.apache.commons.beanutils.DynaClass#getDynaProperties()
120      */

121     public DynaProperty[] getDynaProperties()
122     {
123         if (log.isTraceEnabled())
124         {
125             log.trace("getDynaProperties()");
126         }
127
128         Iterator JavaDoc keys = configuration.getKeys();
129         List JavaDoc properties = new ArrayList JavaDoc();
130         while (keys.hasNext())
131         {
132             String JavaDoc key = (String JavaDoc) keys.next();
133             DynaProperty property = getDynaProperty(key);
134             properties.add(property);
135         }
136
137         DynaProperty[] propertyArray = new DynaProperty[properties.size()];
138         properties.toArray(propertyArray);
139         if (log.isDebugEnabled())
140         {
141             log.debug("Found " + properties.size() + " properties.");
142         }
143
144         return propertyArray;
145     }
146
147     /**
148      * @see org.apache.commons.beanutils.DynaClass#getName()
149      */

150     public String JavaDoc getName()
151     {
152         return ConfigurationDynaBean.class.getName();
153     }
154
155     /**
156      * @see org.apache.commons.beanutils.DynaClass#newInstance()
157      */

158     public DynaBean newInstance() throws IllegalAccessException JavaDoc, InstantiationException JavaDoc
159     {
160         return new ConfigurationDynaBean(configuration);
161     }
162
163 }
164
Popular Tags