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