    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.configuration2.beanutils;
    019
    020import java.util.ArrayList;
    021import java.util.Iterator;
    022import java.util.List;
    023
    024import org.apache.commons.beanutils.DynaBean;
    025import org.apache.commons.beanutils.DynaClass;
    026import org.apache.commons.beanutils.DynaProperty;
    027import org.apache.commons.configuration2.Configuration;
    028import org.apache.commons.logging.Log;
    029import org.apache.commons.logging.LogFactory;
    030
    031/**
    032 * The {@code ConfigurationDynaClass} dynamically determines properties for
    033 * a {@code ConfigurationDynaBean} from a wrapped configuration-collection
    034 * {@link org.apache.commons.configuration2.Configuration} instance.
    035 *
    036 * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
    037 * @version $Id: ConfigurationDynaClass.java 1842194 2018-09-27 22:24:23Z ggregory $
    038 * @since 1.0-rc1
    039 */
    040public class ConfigurationDynaClass implements DynaClass
    041{
    042    /** The logger.*/
    043    private static final Log LOG = LogFactory.getLog(ConfigurationDynaClass.class);
    044
    045    /** Stores the associated configuration.*/
    046    private final Configuration configuration;
    047
    048    /**
    049     * Construct an instance of a {@code ConfigurationDynaClass}
    050     * wrapping the specified {@code Configuration} instance.
    051     * @param configuration {@code Configuration} instance.
    052     */
    053    public ConfigurationDynaClass(final Configuration configuration)
    054    {
    055        super();
    056        if (LOG.isTraceEnabled())
    057        {
    058            LOG.trace("ConfigurationDynaClass(" + configuration + ")");
    059        }
    060        this.configuration = configuration;
    061    }
    062
    063    @Override
    064    public DynaProperty getDynaProperty(final String name)
    065    {
    066        if (LOG.isTraceEnabled())
    067        {
    068            LOG.trace("getDynaProperty(" + name + ")");
    069        }
    070
    071        if (name == null)
    072        {
    073            throw new IllegalArgumentException("Property name must not be null!");
    074        }
    075
    076        final Object value = configuration.getProperty(name);
    077        if (value == null)
    078        {
    079            return null;
    080        }
    081        Class<?> type = value.getClass();
    082
    083        if (type == Byte.class)
    084        {
    085            type = Byte.TYPE;
    086        }
    087        if (type == Character.class)
    088        {
    089            type = Character.TYPE;
    090        }
    091        else if (type == Boolean.class)
    092        {
    093            type = Boolean.TYPE;
    094        }
    095        else if (type == Double.class)
    096        {
    097            type = Double.TYPE;
    098        }
    099        else if (type == Float.class)
    100        {
    101            type = Float.TYPE;
    102        }
    103        else if (type == Integer.class)
    104        {
    105            type = Integer.TYPE;
    106        }
    107        else if (type == Long.class)
    108        {
    109            type = Long.TYPE;
    110        }
    111        else if (type == Short.class)
    112        {
    113            type = Short.TYPE;
    114        }
    115
    116        return new DynaProperty(name, type);
    117    }
    118
    119    @Override
    120    public DynaProperty[] getDynaProperties()
    121    {
    122        if (LOG.isTraceEnabled())
    123        {
    124            LOG.trace("getDynaProperties()");
    125        }
    126
    127        final Iterator<String> keys = configuration.getKeys();
    128        final List<DynaProperty> properties = new ArrayList<>();
    129        while (keys.hasNext())
    130        {
    131            final String key = keys.next();
    132            final DynaProperty property = getDynaProperty(key);
    133            properties.add(property);
    134        }
    135
    136        final DynaProperty[] propertyArray = new DynaProperty[properties.size()];
    137        properties.toArray(propertyArray);
    138        if (LOG.isDebugEnabled())
    139        {
    140            LOG.debug("Found " + properties.size() + " properties.");
    141        }
    142
    143        return propertyArray;
    144    }
    145
    146    @Override
    147    public String getName()
    148    {
    149        return ConfigurationDynaBean.class.getName();
    150    }
    151
    152    @Override
    153    public DynaBean newInstance() throws IllegalAccessException, InstantiationException
    154    {
    155        return new ConfigurationDynaBean(configuration);
    156    }
    157}