ConfigurationDynaClass.java

  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. package org.apache.commons.configuration2.beanutils;

  18. import java.util.ArrayList;
  19. import java.util.Iterator;
  20. import java.util.List;

  21. import org.apache.commons.beanutils.DynaBean;
  22. import org.apache.commons.beanutils.DynaClass;
  23. import org.apache.commons.beanutils.DynaProperty;
  24. import org.apache.commons.configuration2.Configuration;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;

  27. /**
  28.  * The {@code ConfigurationDynaClass} dynamically determines properties for a {@code ConfigurationDynaBean} from a
  29.  * wrapped configuration-collection {@link org.apache.commons.configuration2.Configuration} instance.
  30.  *
  31.  * @since 1.0-rc1
  32.  */
  33. public class ConfigurationDynaClass implements DynaClass {

  34.     /** The logger. */
  35.     private static final Log LOG = LogFactory.getLog(ConfigurationDynaClass.class);

  36.     /** Stores the associated configuration. */
  37.     private final Configuration configuration;

  38.     /**
  39.      * Constructs an instance of a {@code ConfigurationDynaClass} wrapping the specified {@code Configuration} instance.
  40.      *
  41.      * @param configuration {@code Configuration} instance.
  42.      */
  43.     public ConfigurationDynaClass(final Configuration configuration) {
  44.         if (LOG.isTraceEnabled()) {
  45.             LOG.trace("ConfigurationDynaClass(" + configuration + ")");
  46.         }
  47.         this.configuration = configuration;
  48.     }

  49.     @Override
  50.     public DynaProperty[] getDynaProperties() {
  51.         if (LOG.isTraceEnabled()) {
  52.             LOG.trace("getDynaProperties()");
  53.         }

  54.         final Iterator<String> keys = configuration.getKeys();
  55.         final List<DynaProperty> properties = new ArrayList<>();
  56.         while (keys.hasNext()) {
  57.             final String key = keys.next();
  58.             final DynaProperty property = getDynaProperty(key);
  59.             properties.add(property);
  60.         }

  61.         final DynaProperty[] propertyArray = new DynaProperty[properties.size()];
  62.         properties.toArray(propertyArray);
  63.         if (LOG.isDebugEnabled()) {
  64.             LOG.debug("Found " + properties.size() + " properties.");
  65.         }

  66.         return propertyArray;
  67.     }

  68.     @Override
  69.     public DynaProperty getDynaProperty(final String name) {
  70.         if (LOG.isTraceEnabled()) {
  71.             LOG.trace("getDynaProperty(" + name + ")");
  72.         }

  73.         if (name == null) {
  74.             throw new IllegalArgumentException("Property name must not be null!");
  75.         }

  76.         final Object value = configuration.getProperty(name);
  77.         if (value == null) {
  78.             return null;
  79.         }
  80.         Class<?> type = value.getClass();

  81.         if (type == Byte.class) {
  82.             type = Byte.TYPE;
  83.         }
  84.         if (type == Character.class) {
  85.             type = Character.TYPE;
  86.         } else if (type == Boolean.class) {
  87.             type = Boolean.TYPE;
  88.         } else if (type == Double.class) {
  89.             type = Double.TYPE;
  90.         } else if (type == Float.class) {
  91.             type = Float.TYPE;
  92.         } else if (type == Integer.class) {
  93.             type = Integer.TYPE;
  94.         } else if (type == Long.class) {
  95.             type = Long.TYPE;
  96.         } else if (type == Short.class) {
  97.             type = Short.TYPE;
  98.         }

  99.         return new DynaProperty(name, type);
  100.     }

  101.     @Override
  102.     public String getName() {
  103.         return ConfigurationDynaBean.class.getName();
  104.     }

  105.     @Override
  106.     public DynaBean newInstance() throws IllegalAccessException, InstantiationException {
  107.         return new ConfigurationDynaBean(configuration);
  108.     }
  109. }