    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    package org.apache.commons.configuration.beanutils;
    018
    019    /**
    020     * <p>
    021     * The default implementation of the <code>BeanFactory</code> interface.
    022     * </p>
    023     * <p>
    024     * This class creates beans of arbitrary types using reflection. Each time the
    025     * <code>createBean()</code> method is invoked, a new bean instance is
    026     * created. A default bean class is not supported.
    027     * </p>
    028     * <p>
    029     * An instance of this factory class will be set as the default bean factory for
    030     * the <code>{@link BeanHelper}</code> class. This means that if not bean
    031     * factory is specified in a <code>{@link BeanDeclaration}</code>, this
    032     * default instance will be used.
    033     * </p>
    034     *
    035     * @since 1.3
    036     * @author Oliver Heger
    037     * @version $Id: DefaultBeanFactory.java 439648 2006-09-02 20:42:10Z oheger $
    038     */
    039    public class DefaultBeanFactory implements BeanFactory
    040    {
    041        /** Stores the default instance of this class. */
    042        public static final DefaultBeanFactory INSTANCE = new DefaultBeanFactory();
    043
    044        /**
    045         * Creates a new bean instance. This implementation delegates to the
    046         * protected methods <code>createBeanInstance()</code> and
    047         * <code>initBeanInstance()</code> for creating and initializing the bean.
    048         * This makes it easier for derived classes that need to change specific
    049         * functionality of the base class.
    050         *
    051         * @param beanClass the class of the bean, from which an instance is to be
    052         * created
    053         * @param data the bean declaration object
    054         * @param parameter an additional parameter (ignored by this implementation)
    055         * @return the new bean instance
    056         * @throws Exception if an error occurs
    057         */
    058        public Object createBean(Class beanClass, BeanDeclaration data,
    059                Object parameter) throws Exception
    060        {
    061            Object result = createBeanInstance(beanClass, data);
    062            initBeanInstance(result, data);
    063            return result;
    064        }
    065
    066        /**
    067         * Returns the default bean class used by this factory. This is always
    068         * <b>null</b> for this implementation.
    069         *
    070         * @return the default bean class
    071         */
    072        public Class getDefaultBeanClass()
    073        {
    074            return null;
    075        }
    076
    077        /**
    078         * Creates the bean instance. This method is called by
    079         * <code>createBean()</code>. It uses reflection to create a new instance
    080         * of the specified class.
    081         *
    082         * @param beanClass the class of the bean to be created
    083         * @param data the bean declaration
    084         * @return the new bean instance
    085         * @throws Exception if an error occurs
    086         */
    087        protected Object createBeanInstance(Class beanClass, BeanDeclaration data)
    088                throws Exception
    089        {
    090            return beanClass.newInstance();
    091        }
    092
    093        /**
    094         * Initializes the newly created bean instance. This method is called by
    095         * <code>createBean()</code>. It calls the
    096         * <code>{@link BeanHelper#initBean(Object, BeanDeclaration) initBean()}</code>
    097         * of <code>{@link BeanHelper}</code> for performing the initialization.
    098         *
    099         * @param bean the newly created bean instance
    100         * @param data the bean declaration object
    101         * @throws Exception if an error occurs
    102         */
    103        protected void initBeanInstance(Object bean, BeanDeclaration data)
    104                throws Exception
    105        {
    106            BeanHelper.initBean(bean, data);
    107        }
    108    }