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