    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 * @since 1.0-rc1
    037 */
    038public class ConfigurationDynaClass implements DynaClass
    039{
    040    /** The logger.*/
    041    private static final Log LOG = LogFactory.getLog(ConfigurationDynaClass.class);
    042
    043    /** Stores the associated configuration.*/
    044    private final Configuration configuration;
    045
    046    /**
    047     * Construct an instance of a {@code ConfigurationDynaClass}
    048     * wrapping the specified {@code Configuration} instance.
    049     * @param configuration {@code Configuration} instance.
    050     */
    051    public ConfigurationDynaClass(final Configuration configuration)
    052    {
    053        super();
    054        if (LOG.isTraceEnabled())
    055        {
    056            LOG.trace("ConfigurationDynaClass(" + configuration + ")");
    057        }
    058        this.configuration = configuration;
    059    }
    060
    061    @Override
    062    public DynaProperty getDynaProperty(final String name)
    063    {
    064        if (LOG.isTraceEnabled())
    065        {
    066            LOG.trace("getDynaProperty(" + name + ")");
    067        }
    068
    069        if (name == null)
    070        {
    071            throw new IllegalArgumentException("Property name must not be null!");
    072        }
    073
    074        final Object value = configuration.getProperty(name);
    075        if (value == null)
    076        {
    077            return null;
    078        }
    079        Class<?> type = value.getClass();
    080
    081        if (type == Byte.class)
    082        {
    083            type = Byte.TYPE;
    084        }
    085        if (type == Character.class)
    086        {
    087            type = Character.TYPE;
    088        }
    089        else if (type == Boolean.class)
    090        {
    091            type = Boolean.TYPE;
    092        }
    093        else if (type == Double.class)
    094        {
    095            type = Double.TYPE;
    096        }
    097        else if (type == Float.class)
    098        {
    099            type = Float.TYPE;
    100        }
    101        else if (type == Integer.class)
    102        {
    103            type = Integer.TYPE;
    104        }
    105        else if (type == Long.class)
    106        {
    107            type = Long.TYPE;
    108        }
    109        else if (type == Short.class)
    110        {
    111            type = Short.TYPE;
    112        }
    113
    114        return new DynaProperty(name, type);
    115    }
    116
    117    @Override
    118    public DynaProperty[] getDynaProperties()
    119    {
    120        if (LOG.isTraceEnabled())
    121        {
    122            LOG.trace("getDynaProperties()");
    123        }
    124
    125        final Iterator<String> keys = configuration.getKeys();
    126        final List<DynaProperty> properties = new ArrayList<>();
    127        while (keys.hasNext())
    128        {
    129            final String key = keys.next();
    130            final DynaProperty property = getDynaProperty(key);
    131            properties.add(property);
    132        }
    133
    134        final DynaProperty[] propertyArray = new DynaProperty[properties.size()];
    135        properties.toArray(propertyArray);
    136        if (LOG.isDebugEnabled())
    137        {
    138            LOG.debug("Found " + properties.size() + " properties.");
    139        }
    140
    141        return propertyArray;
    142    }
    143
    144    @Override
    145    public String getName()
    146    {
    147        return ConfigurationDynaBean.class.getName();
    148    }
    149
    150    @Override
    151    public DynaBean newInstance() throws IllegalAccessException, InstantiationException
    152    {
    153        return new ConfigurationDynaBean(configuration);
    154    }
    155}