Class LifeCycleBeanProvider

  • All Implemented Interfaces:
    BeanInitializer, BeanProvider
    Direct Known Subclasses:
    FactoryBeanProvider, SingletonBeanProvider

    public abstract class LifeCycleBeanProvider
    extends Object
    implements BeanProvider, BeanInitializer

    An abstract base class for BeanProvider implementations with life-cycle support.

    A LifeCycleBeanProvider has the following properties:

    • A (usually simple) BeanProvider for actually creating an instance of the managed bean.
    • An optional Invokable object for initializing the newly created bean instance.

    This base class provides basic functionality for the creation and initialization of beans. It also implements the methods related to life-cycle support of the BeanProvider interface in a meaningful way. There are two methods that are intended to be called by concrete sub classes: createBean() and fetchBean().

    createBean(), as its name implies, creates a new instance of the managed bean class. This is done by invoking the BeanProvider for creating new beans. After that the Invokable object is called on the newly created bean. The creation of a bean through the bean provider may cause an endless loop if there are cyclic dependencies (e.g. bean A needs bean B as a constructor argument and vice verse). Such cycles are detected and lead to a InjectionException exception being thrown.

    The fetchBean() method checks whether a bean instance has already been created. If this is the case, it is directly returned. Otherwise createBean() is called for creating a new instance. Depending on their semantics derived classes decide, which of these methods to call. This decision must be implemented in the getBean() method; all other methods defined by the BeanProvider interface are already implemented by this base class.

    Implementation note: This class is intended to be used together with a correct implementation of the BeanContext interface. It is not thread-safe by itself, but if the bean context handles transactions properly, it can be used in an environment with multiple threads accessing the bean context concurrently.

    Version:
    $Id: LifeCycleBeanProvider.java 208 2012-02-11 20:57:33Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • LifeCycleBeanProvider

        protected LifeCycleBeanProvider​(BeanProvider createProvider,
                                        Invokable initinv)
        Creates a new instance of LifeCycleBeanProvider and initializes it with the BeanProvider for creating the bean instance and an Invokable for initializing it.
        Parameters:
        createProvider - the bean provider for creating a bean instance (must not be null)
        initinv - an optional invocation object with initialization code for the newly created bean
        Throws:
        IllegalArgumentException - if the bean provider is null
      • LifeCycleBeanProvider

        protected LifeCycleBeanProvider​(BeanProvider createProvider)
        Creates a new instance of LifeCycleBeanProvider and initializes it with the BeanProvider for creating the bean instance.
        Parameters:
        createProvider - the bean provider for creating a bean instance (must not be null)
        Throws:
        IllegalArgumentException - if the bean provider is null
    • Method Detail

      • getBeanCreator

        public BeanProvider getBeanCreator()
        Returns the BeanProvider that is responsible for creating a new bean instance.
        Returns:
        the bean provider for creating new bean instances
      • getBeanInitializer

        public Invokable getBeanInitializer()
        Returns the Invokable object responsible for initializing the newly created bean. This method never returns null. If no initializer was set, a default initializer object (that does not have any effect) is returned.
        Returns:
        the invocation object used for initialization
      • getBeanClass

        public Class<?> getBeanClass​(DependencyProvider dependencyProvider)
        Returns the class of the bean managed by this provider. This class is determined by the BeanProvider for creating new bean instances.
        Specified by:
        getBeanClass in interface BeanProvider
        Parameters:
        dependencyProvider - the dependency provider
        Returns:
        the class of the managed bean
        Throws:
        InjectionException - if an error occurs determining the class
      • getDependencies

        public Set<Dependency> getDependencies()
        Returns the dependencies of this bean provider. This implementation obtains the dependencies of the creation bean provider and the invocation object for initialization and returns a union.
        Specified by:
        getDependencies in interface BeanProvider
        Returns:
        the dependencies of this bean provider
        See Also:
        Dependency
      • getLockID

        public Long getLockID()
        Returns the ID of the locking transaction. If this method returns a non null value, this bean provider must not be used by a concurrent transaction.
        Specified by:
        getLockID in interface BeanProvider
        Returns:
        the ID of the locking transaction
      • setLockID

        public void setLockID​(Long lid)
        Sets the ID of the locking transaction. This indicates that this bean provider is blocked by the specified transaction.
        Specified by:
        setLockID in interface BeanProvider
        Parameters:
        lid - the ID of the locking transaction
        See Also:
        BeanProvider.getLockID()
      • isBeanAvailable

        public boolean isBeanAvailable()
        Returns a flag whether the bean managed by this provider is available. This implementation checks whether the bean is currently created. If this is the case, it is not available.
        Specified by:
        isBeanAvailable in interface BeanProvider
        Returns:
        a flag whether the managed bean is available
      • shutdown

        public void shutdown​(DependencyProvider depProvider)
        Notifies this BeanProvider that it is no longer needed. This is just an empty dummy implementation. Derived classes that support shutdown handling have to override it.
        Specified by:
        shutdown in interface BeanProvider
        Parameters:
        depProvider - the DependencyProvider
      • initialize

        public void initialize​(DependencyProvider dependencyProvider)
        Performs initialization. This method is called by the dependency provider if initialization has to be delayed because of cyclic dependencies. It invokes the initializer.
        Specified by:
        initialize in interface BeanInitializer
        Parameters:
        dependencyProvider - the dependency provider
      • toString

        public String toString()
        Returns a string representation of this object. This string also contains information about the bean provider used for creating the bean and the invocation object for the initialization.
        Overrides:
        toString in class Object
        Returns:
        a string for this object
      • createBean

        protected Object createBean​(DependencyProvider dependencyProvider)
        Creates and initializes a new bean instance. This method is reentrant, which is necessary if there are cycles in the dependencies. In this case a bean that is only partly initialized may be returned. It is also possible that the cycles cannot be resolved, then an exception is thrown.
        Parameters:
        dependencyProvider - the dependency provider
        Returns:
        the newly created bean instance
        Throws:
        InjectionException - if an error occurs
      • fetchBean

        protected Object fetchBean​(DependencyProvider dependencyProvider)
        Returns the bean instance created by this provider. If no instance has been created yet, this is done now by invoking createBean(). Otherwise the bean instance is directly returned. If the dependencies contain cyclic references, it is possible that a bean instance is returned, which has not yet been fully initialized. Cycles that cannot be resolved cause an exception.
        Parameters:
        dependencyProvider - the dependency provider
        Returns:
        the bean instance managed by this provider
        Throws:
        InjectionException - if an error occurs
      • canInitialize

        protected boolean canInitialize​(DependencyProvider dependencyProvider)
        Checks whether initialization of the bean is now possible. This method tests whether all dependencies required for the bean's initialization are currently available.
        Parameters:
        dependencyProvider - the dependency provider
        Returns:
        a flag whether initialization can now be performed
      • hasBean

        protected boolean hasBean()
        Returns a flag whether a bean instance has already been created. This can be used by derived classes for adapting their behavior. This implementation returns true if and only if a bean instance has been created and fully initialized.
        Returns:
        a flag whether a bean instance has been created
      • resetBean

        protected void resetBean()
        Resets any so far created bean. This method can be called by derived classes to reset this bean provider. In a following call to fetchBean() a completely new bean will be created.
      • doCreateBean

        protected Object doCreateBean​(DependencyProvider dependencyProvider)
        Creates a new bean instance. This method is called by createBean() for actually creating the bean.
        Parameters:
        dependencyProvider - the dependency provider
        Returns:
        the new bean instance
        Throws:
        InjectionException - if an error occurs
      • initBean

        @Deprecated
        protected void initBean​(Object bean,
                                DependencyProvider dependencyProvider)
        Deprecated.
        This method is not called any more during bean creation; instead fetchInitializedBeanInstance(Object, DependencyProvider) is invoked
        Initializes a newly created bean instance. This method is called by createBean() for each new bean instance. This implementation invokes the initializer and notifies the DependencyProvider about the creation of a new bean.
        Parameters:
        bean - the bean to initialize
        dependencyProvider - the dependency provider
        Throws:
        InjectionException - if an error occurs
      • fetchInitializedBeanInstance

        protected Object fetchInitializedBeanInstance​(Object bean,
                                                      DependencyProvider dependencyProvider)
        Returns the initialized bean instance. This method is called by createBean() for each new bean instance. Its purpose is to execute the initializer script on the bean. This implementation invokes the initializer and notifies the DependencyProvider about the creation of a new bean. Note that the bean returned by the initializer script is the result of this method. Thus it is possible that a different bean than passed to the method becomes the managed bean of this provider. However, if the initializer returns null, the passed in bean is returned.
        Parameters:
        bean - the bean to initialize
        dependencyProvider - the dependency provider
        Returns:
        the bean instance
        Throws:
        InjectionException - if an error occurs
        Since:
        1.1