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    *     https://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[] getDynaProperties() {
59          if (LOG.isTraceEnabled()) {
60              LOG.trace("getDynaProperties()");
61          }
62  
63          final Iterator<String> keys = configuration.getKeys();
64          final List<DynaProperty> properties = new ArrayList<>();
65          while (keys.hasNext()) {
66              final String key = keys.next();
67              final DynaProperty property = getDynaProperty(key);
68              properties.add(property);
69          }
70  
71          final DynaProperty[] propertyArray = new DynaProperty[properties.size()];
72          properties.toArray(propertyArray);
73          if (LOG.isDebugEnabled()) {
74              LOG.debug("Found " + properties.size() + " properties.");
75          }
76  
77          return propertyArray;
78      }
79  
80      @Override
81      public DynaProperty getDynaProperty(final String name) {
82          if (LOG.isTraceEnabled()) {
83              LOG.trace("getDynaProperty(" + name + ")");
84          }
85  
86          if (name == null) {
87              throw new IllegalArgumentException("Property name must not be null!");
88          }
89  
90          final Object value = configuration.getProperty(name);
91          if (value == null) {
92              return null;
93          }
94          Class<?> type = value.getClass();
95  
96          if (type == Byte.class) {
97              type = Byte.TYPE;
98          }
99          if (type == Character.class) {
100             type = Character.TYPE;
101         } else if (type == Boolean.class) {
102             type = Boolean.TYPE;
103         } else if (type == Double.class) {
104             type = Double.TYPE;
105         } else if (type == Float.class) {
106             type = Float.TYPE;
107         } else if (type == Integer.class) {
108             type = Integer.TYPE;
109         } else if (type == Long.class) {
110             type = Long.TYPE;
111         } else if (type == Short.class) {
112             type = Short.TYPE;
113         }
114 
115         return new DynaProperty(name, type);
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 }