Class DefaultBeanContext

  • All Implemented Interfaces:
    BeanContext

    public class DefaultBeanContext
    extends Object
    implements BeanContext

    A default implementation of the BeanContext interface.

    This class allows full access to all beans defined in a hierarchy of BeanStores. Dependencies are dynamically resolved and injected.

    The class is thread-safe. When operating on a bean store hierarchy it implements transactional behavior as described in the documentation to the BeanStore interface, i.e. if two threads try to access a BeanProvider concurrently, one of the will be suspended until the other one has resolved all of its dependencies. This enables concurrent, synchronized access to bean stores in a read-only manner. However the bean stores should not be written at the same time.

    Version:
    $Id: DefaultBeanContext.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • DefaultBeanContext

        public DefaultBeanContext()
        Creates a new instance of DefaultBeanContext.
      • DefaultBeanContext

        public DefaultBeanContext​(BeanStore defStore)
        Creates a new instance of DefaultBeanContext and sets the default bean store. This parameter is optional. If no BeanStore is provided, a default store must be set later or only methods can be used that expect a store as argument.
        Parameters:
        defStore - the default bean store
    • Method Detail

      • setClassLoaderProvider

        public void setClassLoaderProvider​(ClassLoaderProvider classLoaderProvider)
        Sets the ClassLoaderProvider to be used by this bean context. The ClassLoaderProvider is needed when dependencies to beans are to be resolved that specified by class names. When a new DefaultBeanContext instance is created, a default ClassLoaderProvider is set. It is then possible to change this object using this method.
        Specified by:
        setClassLoaderProvider in interface BeanContext
        Parameters:
        classLoaderProvider - the new ClassLoaderProvider (must not be null)
        Throws:
        IllegalArgumentException - if the parameter is null
      • beanClasses

        public Set<Class<?>> beanClasses()
        Obtains a list of the classes of the beans defined in the default bean store.
        Specified by:
        beanClasses in interface BeanContext
        Returns:
        the classes of the defined beans
      • beanClasses

        public Set<Class<?>> beanClasses​(BeanStore store)
        Obtains a list of the classes of the beans defined in the given store and its parents.
        Specified by:
        beanClasses in interface BeanContext
        Parameters:
        store - the store to start from
        Returns:
        the classes of the defined beans
      • beanNames

        public Set<String> beanNames()
        Returns a set with the names of the beans defined in the default bean store.
        Specified by:
        beanNames in interface BeanContext
        Returns:
        the names of the defined beans
      • beanNames

        public Set<String> beanNames​(BeanStore store)
        Returns a set with the names of the beans defined in the given bean store (or its parent).
        Specified by:
        beanNames in interface BeanContext
        Parameters:
        store - the bean store
        Returns:
        a set with the names of the defined beans
      • containsBean

        public boolean containsBean​(String name)
        Tests whether a bean with the given name can be found in the default store.
        Specified by:
        containsBean in interface BeanContext
        Parameters:
        name - the name of the bean
        Returns:
        a flag whether this bean can be found
      • containsBean

        public boolean containsBean​(String name,
                                    BeanStore store)
        Tests whether a bean with the given name can be found in the specified bean store. null can be specified for both the name and the bean store; result will then be false.
        Specified by:
        containsBean in interface BeanContext
        Parameters:
        name - the name of the bean
        store - the bean store
        Returns:
        a flag whether this bean can be found in this store
      • containsBean

        public boolean containsBean​(Class<?> beanClass)
        Tests whether a bean with the given bean class can be found in the default bean store.
        Specified by:
        containsBean in interface BeanContext
        Parameters:
        beanClass - the class of the bean
        Returns:
        a flag whether this bean can be found
      • containsBean

        public boolean containsBean​(Class<?> beanClass,
                                    BeanStore store)
        Tests whether a bean with the given class can be found in the specified bean store. null can be specified for both the name and the bean store; result will then be false.
        Specified by:
        containsBean in interface BeanContext
        Parameters:
        beanClass - the class of the bean
        store - the bean store
        Returns:
        a flag whether this bean can be found in this store
      • getBean

        public Object getBean​(String name)
        Returns the bean with the specified name from the default store.
        Specified by:
        getBean in interface BeanContext
        Parameters:
        name - the name of the bean to retrieve
        Returns:
        the bean with this name
        Throws:
        InjectionException - if an error occurs
      • getBean

        public Object getBean​(String name,
                              BeanStore store)
        Returns the bean with the specified name from the given bean store.
        Specified by:
        getBean in interface BeanContext
        Parameters:
        name - the name of the bean to retrieve
        store - the bean store
        Returns:
        the bean with this name
        Throws:
        InjectionException - if an error occurs
      • getBean

        public <T> T getBean​(Class<T> beanCls)
        Returns the bean with the specified class from the default bean store.
        Specified by:
        getBean in interface BeanContext
        Type Parameters:
        T - the type of the bean to be retrieved
        Parameters:
        beanCls - the class of the bean to be retrieved
        Returns:
        the bean with this class
        Throws:
        InjectionException - if an error occurs
      • getBean

        public <T> T getBean​(Class<T> beanCls,
                             BeanStore store)
        Returns the bean with the specified class from the given bean store.
        Specified by:
        getBean in interface BeanContext
        Type Parameters:
        T - the type of the bean to be retrieved
        Parameters:
        beanCls - the class of the bean to be retrieved
        store - the bean store
        Returns:
        the bean with this class
        Throws:
        InjectionException - if an error occurs
      • beanNameFor

        public String beanNameFor​(BeanProvider beanProvider)
        Searches for the specified BeanProvider in the accessible bean stores (starting with the default bean store) and the returns the name, under which it is registered.
        Specified by:
        beanNameFor in interface BeanContext
        Parameters:
        beanProvider - the BeanProvider in question
        Returns:
        the corresponding bean name or null if it cannot be resolved
        See Also:
        BeanContext.beanNameFor(BeanProvider, BeanStore)
      • beanNameFor

        public String beanNameFor​(BeanProvider beanProvider,
                                  BeanStore store)
        Searches for the specified BeanProvider in the accessible bean stores (starting with the specified bean store) and the returns the name, under which it is registered.
        Specified by:
        beanNameFor in interface BeanContext
        Parameters:
        beanProvider - the BeanProvider in question
        store - the BeanStore
        Returns:
        the corresponding bean name or null if it cannot be resolved
      • close

        public void close()
        Closes this BeanContext. This is just an empty dummy implementation. There are no resources to be freed.
        Specified by:
        close in interface BeanContext
      • setDefaultBeanStore

        public void setDefaultBeanStore​(BeanStore store)
        Sets the default bean store.
        Specified by:
        setDefaultBeanStore in interface BeanContext
        Parameters:
        store - the new default bean store
      • getBean

        protected Object getBean​(Dependency dependency,
                                 BeanStore store)
        Obtains the bean from the BeanProvider specified by the given Dependency. This method is called by the other getBean() methods. It does the real work.
        Parameters:
        dependency - the dependency to be resolved
        store - the current store
        Returns:
        the bean managed by the specified provider
        Throws:
        InjectionException - if an error occurs while resolving the dependency
      • waitForTx

        protected void waitForTx​(BeanStore root)
                          throws InterruptedException
        Waits at the specified bean store until the current transaction finishes. This method is called when a transaction affects a bean provider that is already locked by another transaction. In this case this other transaction has to finish first. This implementation calls wait() on the given bean store.
        Parameters:
        root - the root bean store
        Throws:
        InterruptedException - if the thread is interrupted