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.configuration2.beanutils;
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
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.configuration2.Configuration;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /**
32   * The {@code ConfigurationDynaClass} dynamically determines properties for a {@code ConfigurationDynaBean} from a
33   * wrapped configuration-collection {@link org.apache.commons.configuration2.Configuration} instance.
34   *
35   * @since 1.0-rc1
36   */
37  public class ConfigurationDynaClass implements DynaClass {
38  
39      /** The logger. */
40      private static final Log LOG = LogFactory.getLog(ConfigurationDynaClass.class);
41  
42      /** Stores the associated configuration. */
43      private final Configuration configuration;
44  
45      /**
46       * Constructs an instance of a {@code ConfigurationDynaClass} wrapping the specified {@code Configuration} instance.
47       *
48       * @param configuration {@code Configuration} instance.
49       */
50      public ConfigurationDynaClass(final Configuration configuration) {
51          if (LOG.isTraceEnabled()) {
52              LOG.trace("ConfigurationDynaClass(" + configuration + ")");
53          }
54          this.configuration = configuration;
55      }
56  
57      @Override
58      public DynaProperty getDynaProperty(final String name) {
59          if (LOG.isTraceEnabled()) {
60              LOG.trace("getDynaProperty(" + name + ")");
61          }
62  
63          if (name == null) {
64              throw new IllegalArgumentException("Property name must not be null!");
65          }
66  
67          final Object value = configuration.getProperty(name);
68          if (value == null) {
69              return null;
70          }
71          Class<?> type = value.getClass();
72  
73          if (type == Byte.class) {
74              type = Byte.TYPE;
75          }
76          if (type == Character.class) {
77              type = Character.TYPE;
78          } else if (type == Boolean.class) {
79              type = Boolean.TYPE;
80          } else if (type == Double.class) {
81              type = Double.TYPE;
82          } else if (type == Float.class) {
83              type = Float.TYPE;
84          } else if (type == Integer.class) {
85              type = Integer.TYPE;
86          } else if (type == Long.class) {
87              type = Long.TYPE;
88          } else if (type == Short.class) {
89              type = Short.TYPE;
90          }
91  
92          return new DynaProperty(name, type);
93      }
94  
95      @Override
96      public DynaProperty[] getDynaProperties() {
97          if (LOG.isTraceEnabled()) {
98              LOG.trace("getDynaProperties()");
99          }
100 
101         final Iterator<String> keys = configuration.getKeys();
102         final List<DynaProperty> properties = new ArrayList<>();
103         while (keys.hasNext()) {
104             final String key = keys.next();
105             final DynaProperty property = getDynaProperty(key);
106             properties.add(property);
107         }
108 
109         final DynaProperty[] propertyArray = new DynaProperty[properties.size()];
110         properties.toArray(propertyArray);
111         if (LOG.isDebugEnabled()) {
112             LOG.debug("Found " + properties.size() + " properties.");
113         }
114 
115         return propertyArray;
116     }
117 
118     @Override
119     public String getName() {
120         return ConfigurationDynaBean.class.getName();
121     }
122 
123     @Override
124     public DynaBean newInstance() throws IllegalAccessException, InstantiationException {
125         return new ConfigurationDynaBean(configuration);
126     }
127 }