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    
018    package org.apache.commons.configuration.beanutils;
019    
020    import java.util.ArrayList;
021    import java.util.Iterator;
022    import java.util.List;
023    
024    import org.apache.commons.beanutils.DynaBean;
025    import org.apache.commons.beanutils.DynaClass;
026    import org.apache.commons.beanutils.DynaProperty;
027    import org.apache.commons.configuration.Configuration;
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    
031    /**
032     * The <tt>ConfigurationDynaClass</tt> dynamically determines properties for
033     * a <code>ConfigurationDynaBean</code> from a wrapped configuration-collection
034     * {@link org.apache.commons.configuration.Configuration} instance.
035     *
036     * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
037     * @version $Revision: 727664 $, $Date: 2008-12-18 08:16:09 +0100 (Do, 18 Dez 2008) $
038     * @since 1.0-rc1
039     */
040    public class ConfigurationDynaClass implements DynaClass
041    {
042        /** The logger.*/
043        private static Log log = LogFactory.getLog(ConfigurationDynaClass.class);
044    
045        /** Stores the associated configuration.*/
046        private Configuration configuration;
047    
048        /**
049         * Construct an instance of a <code>ConfigurationDynaClass</code>
050         * wrapping the specified <code>Configuration</code> instance.
051         * @param configuration <code>Configuration</code> instance.
052         */
053        public ConfigurationDynaClass(Configuration configuration)
054        {
055            super();
056            if (log.isTraceEnabled())
057            {
058                log.trace("ConfigurationDynaClass(" + configuration + ")");
059            }
060            this.configuration = configuration;
061        }
062    
063        public DynaProperty getDynaProperty(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            Object value = configuration.getProperty(name);
076            if (value == null)
077            {
078                return null;
079            }
080            else
081            {
082                Class type = value.getClass();
083    
084                if (type == Byte.class)
085                {
086                    type = Byte.TYPE;
087                }
088                if (type == Character.class)
089                {
090                    type = Character.TYPE;
091                }
092                else if (type == Boolean.class)
093                {
094                    type = Boolean.TYPE;
095                }
096                else if (type == Double.class)
097                {
098                    type = Double.TYPE;
099                }
100                else if (type == Float.class)
101                {
102                    type = Float.TYPE;
103                }
104                else if (type == Integer.class)
105                {
106                    type = Integer.TYPE;
107                }
108                else if (type == Long.class)
109                {
110                    type = Long.TYPE;
111                }
112                else if (type == Short.class)
113                {
114                    type = Short.TYPE;
115                }
116    
117                return new DynaProperty(name, type);
118            }
119        }
120    
121        public DynaProperty[] getDynaProperties()
122        {
123            if (log.isTraceEnabled())
124            {
125                log.trace("getDynaProperties()");
126            }
127    
128            Iterator keys = configuration.getKeys();
129            List properties = new ArrayList();
130            while (keys.hasNext())
131            {
132                String key = (String) keys.next();
133                DynaProperty property = getDynaProperty(key);
134                properties.add(property);
135            }
136    
137            DynaProperty[] propertyArray = new DynaProperty[properties.size()];
138            properties.toArray(propertyArray);
139            if (log.isDebugEnabled())
140            {
141                log.debug("Found " + properties.size() + " properties.");
142            }
143    
144            return propertyArray;
145        }
146    
147        public String getName()
148        {
149            return ConfigurationDynaBean.class.getName();
150        }
151    
152        public DynaBean newInstance() throws IllegalAccessException, InstantiationException
153        {
154            return new ConfigurationDynaBean(configuration);
155        }
156    }