View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.configuration.beanutils;
    18
    19  /***
    20   * <p>
    21   * The default implementation of the <code>BeanFactory</code> interface.
    22   * </p>
    23   * <p>
    24   * This class creates beans of arbitrary types using reflection. Each time the
    25   * <code>createBean()</code> method is invoked, a new bean instance is
    26   * created. A default bean class is not supported.
    27   * </p>
    28   * <p>
    29   * An instance of this factory class will be set as the default bean factory for
    30   * the <code>{@link BeanHelper}</code> class. This means that if not bean
    31   * factory is specified in a <code>{@link BeanDeclaration}</code>, this
    32   * default instance will be used.
    33   * </p>
    34   *
    35   * @since 1.3
    36   * @author Oliver Heger
    37   * @version $Id: DefaultBeanFactory.java 439648 2006-09-02 20:42:10Z oheger $
    38   */
    39  public class DefaultBeanFactory implements BeanFactory
    40  {
    41      /*** Stores the default instance of this class. */
    42      public static final DefaultBeanFactory INSTANCE = new DefaultBeanFactory();
    43
    44      /***
    45       * Creates a new bean instance. This implementation delegates to the
    46       * protected methods <code>createBeanInstance()</code> and
    47       * <code>initBeanInstance()</code> for creating and initializing the bean.
    48       * This makes it easier for derived classes that need to change specific
    49       * functionality of the base class.
    50       *
    51       * @param beanClass the class of the bean, from which an instance is to be
    52       * created
    53       * @param data the bean declaration object
    54       * @param parameter an additional parameter (ignored by this implementation)
    55       * @return the new bean instance
    56       * @throws Exception if an error occurs
    57       */
    58      public Object createBean(Class beanClass, BeanDeclaration data,
    59              Object parameter) throws Exception
    60      {
    61          Object result = createBeanInstance(beanClass, data);
    62          initBeanInstance(result, data);
    63          return result;
    64      }
    65
    66      /***
    67       * Returns the default bean class used by this factory. This is always
    68       * <b>null</b> for this implementation.
    69       *
    70       * @return the default bean class
    71       */
    72      public Class getDefaultBeanClass()
    73      {
    74          return null;
    75      }
    76
    77      /***
    78       * Creates the bean instance. This method is called by
    79       * <code>createBean()</code>. It uses reflection to create a new instance
    80       * of the specified class.
    81       *
    82       * @param beanClass the class of the bean to be created
    83       * @param data the bean declaration
    84       * @return the new bean instance
    85       * @throws Exception if an error occurs
    86       */
    87      protected Object createBeanInstance(Class beanClass, BeanDeclaration data)
    88              throws Exception
    89      {
    90          return beanClass.newInstance();
    91      }
    92
    93      /***
    94       * Initializes the newly created bean instance. This method is called by
    95       * <code>createBean()</code>. It calls the
    96       * <code>{@link BeanHelper#initBean(Object, BeanDeclaration) initBean()}</code>
    97       * of <code>{@link BeanHelper}</code> for performing the initialization.
    98       *
    99       * @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 }