    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 a {@code ConfigurationDynaBean} from a
    033 * wrapped configuration-collection {@link org.apache.commons.configuration2.Configuration} instance.
    034 *
    035 * @since 1.0-rc1
    036 */
    037public class ConfigurationDynaClass implements DynaClass {
    038
    039    /** The logger. */
    040    private static final Log LOG = LogFactory.getLog(ConfigurationDynaClass.class);
    041
    042    /** Stores the associated configuration. */
    043    private final Configuration configuration;
    044
    045    /**
    046     * Constructs an instance of a {@code ConfigurationDynaClass} wrapping the specified {@code Configuration} instance.
    047     *
    048     * @param configuration {@code Configuration} instance.
    049     */
    050    public ConfigurationDynaClass(final Configuration configuration) {
    051        if (LOG.isTraceEnabled()) {
    052            LOG.trace("ConfigurationDynaClass(" + configuration + ")");
    053        }
    054        this.configuration = configuration;
    055    }
    056
    057    @Override
    058    public DynaProperty getDynaProperty(final String name) {
    059        if (LOG.isTraceEnabled()) {
    060            LOG.trace("getDynaProperty(" + name + ")");
    061        }
    062
    063        if (name == null) {
    064            throw new IllegalArgumentException("Property name must not be null!");
    065        }
    066
    067        final Object value = configuration.getProperty(name);
    068        if (value == null) {
    069            return null;
    070        }
    071        Class<?> type = value.getClass();
    072
    073        if (type == Byte.class) {
    074            type = Byte.TYPE;
    075        }
    076        if (type == Character.class) {
    077            type = Character.TYPE;
    078        } else if (type == Boolean.class) {
    079            type = Boolean.TYPE;
    080        } else if (type == Double.class) {
    081            type = Double.TYPE;
    082        } else if (type == Float.class) {
    083            type = Float.TYPE;
    084        } else if (type == Integer.class) {
    085            type = Integer.TYPE;
    086        } else if (type == Long.class) {
    087            type = Long.TYPE;
    088        } else if (type == Short.class) {
    089            type = Short.TYPE;
    090        }
    091
    092        return new DynaProperty(name, type);
    093    }
    094
    095    @Override
    096    public DynaProperty[] getDynaProperties() {
    097        if (LOG.isTraceEnabled()) {
    098            LOG.trace("getDynaProperties()");
    099        }
    100
    101        final Iterator<String> keys = configuration.getKeys();
    102        final List<DynaProperty> properties = new ArrayList<>();
    103        while (keys.hasNext()) {
    104            final String key = keys.next();
    105            final DynaProperty property = getDynaProperty(key);
    106            properties.add(property);
    107        }
    108
    109        final DynaProperty[] propertyArray = new DynaProperty[properties.size()];
    110        properties.toArray(propertyArray);
    111        if (LOG.isDebugEnabled()) {
    112            LOG.debug("Found " + properties.size() + " properties.");
    113        }
    114
    115        return propertyArray;
    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}