View Javadoc

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 implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.configuration.beanutils;
19  
20  import java.util.Iterator;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.commons.beanutils.DynaBean;
25  import org.apache.commons.beanutils.DynaClass;
26  import org.apache.commons.beanutils.DynaProperty;
27  import org.apache.commons.configuration.Configuration;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /***
32   * The <tt>ConfigurationDynaClass</tt> dynamically determines properties for
33   * a <code>ConfigurationDynaBean</code> from a wrapped configuration-collection
34   * {@link org.apache.commons.configuration.Configuration} instance.
35   *
36   * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
37   * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
38   * @since 1.0-rc1
39   */
40  public class ConfigurationDynaClass implements DynaClass
41  {
42      /*** The logger.*/
43      private static Log log = LogFactory.getLog(ConfigurationDynaClass.class);
44  
45      /*** Stores the associated configuration.*/
46      private Configuration configuration;
47  
48      /***
49       * Construct an instance of a <code>ConfigurationDynaClass</code>
50       * wrapping the specified <code>Configuration</code> instance.
51       * @param configuration <code>Configuration</code> instance.
52       */
53      public ConfigurationDynaClass(Configuration configuration)
54      {
55          super();
56          if (log.isTraceEnabled())
57          {
58              log.trace("ConfigurationDynaClass(" + configuration + ")");
59          }
60          this.configuration = configuration;
61      }
62  
63      /***
64       * @see org.apache.commons.beanutils.DynaClass#getDynaProperty(java.lang.String)
65       */
66      public DynaProperty getDynaProperty(String name)
67      {
68          if (log.isTraceEnabled())
69          {
70              log.trace("getDynaProperty(" + name + ")");
71          }
72  
73          if (name == null)
74          {
75              throw new IllegalArgumentException("No such property name=[" + name + "]");
76          }
77  
78          Object value = configuration.getProperty(name);
79          if (value == null)
80          {
81              return null;
82          }
83          else
84          {
85              Class type = value.getClass();
86  
87              if (type == Byte.class)
88              {
89                  type = Byte.TYPE;
90              }
91              if (type == Character.class)
92              {
93                  type = Character.TYPE;
94              }
95              else if (type == Boolean.class)
96              {
97                  type = Boolean.TYPE;
98              }
99              else if (type == Double.class)
100             {
101                 type = Double.TYPE;
102             }
103             else if (type == Float.class)
104             {
105                 type = Float.TYPE;
106             }
107             else if (type == Integer.class)
108             {
109                 type = Integer.TYPE;
110             }
111             else if (type == Long.class)
112             {
113                 type = Long.TYPE;
114             }
115             else if (type == Short.class)
116             {
117                 type = Short.TYPE;
118             }
119 
120             return new DynaProperty(name, type);
121         }
122     }
123 
124     /***
125      * @see org.apache.commons.beanutils.DynaClass#getDynaProperties()
126      */
127     public DynaProperty[] getDynaProperties()
128     {
129         if (log.isTraceEnabled())
130         {
131             log.trace("getDynaProperties()");
132         }
133 
134         Iterator keys = configuration.getKeys();
135         List properties = new ArrayList();
136         while (keys.hasNext())
137         {
138             String key = (String) keys.next();
139             DynaProperty property = getDynaProperty(key);
140             properties.add(property);
141         }
142 
143         DynaProperty[] propertyArray = new DynaProperty[properties.size()];
144         properties.toArray(propertyArray);
145         if (log.isDebugEnabled())
146         {
147             log.debug("Found " + properties.size() + " properties.");
148         }
149 
150         return propertyArray;
151     }
152 
153     /***
154      * @see org.apache.commons.beanutils.DynaClass#getName()
155      */
156     public String getName()
157     {
158         return ConfigurationDynaBean.class.getName();
159     }
160 
161     /***
162      * @see org.apache.commons.beanutils.DynaClass#newInstance()
163      */
164     public DynaBean newInstance() throws IllegalAccessException, InstantiationException
165     {
166         return new ConfigurationDynaBean(configuration);
167     }
168 
169 }