Class DIBuilderData


  • public class DIBuilderData
    extends Object

    A data class for maintaining all information required for a DI builder operation.

    An instance of this class will be present in the Jelly context when a Jelly script for populating a BeanContext is running. The specific tag handler classes can access the information they need for adding newly created beans to the correct BeanStore or for accessing required helper objects.

    One important task of this class is dealing with BeanStore objects: One root bean store must be provided, to which bean definitions are added per default. In a builder script, it is possible to create an arbitrary number of further bean stores below this root store. In a bean definition the target bean store (where the bean should be stored) can be specified.

    This class also maintains the current InvocationHelper instance which is responsible for reflection operations and (indirectly) for data type conversions required by the current builder script. Either an instance of InvocationHelper can be set explicitly using the setInvocationHelper(InvocationHelper) method or a default instance will be created. If the root BeanStore is created by this object, the ConversionHelper will be set automatically. If the root BeanStore is set manually using the initRootBeanStore(MutableBeanStore) method, the caller is responsible for initializing the root store with the correct ConversionHelper instance.

    Implementation note: This class is not thread-safe. It is intended to be used inside a Jelly script, which runs in a single thread.

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

      • DIBuilderData

        public DIBuilderData()
        Creates a new instance of DIBuilderData.
    • Method Detail

      • getClassLoaderProvider

        public ClassLoaderProvider getClassLoaderProvider()
        Returns the ClassLoaderProvider to be used. This method never returns null. If no specific ClassLoaderProvider has been set so far (by using the setClassLoaderProvider(ClassLoaderProvider) method), a new default provider is created and returned, which does not contain any specific registered class loaders.
        Returns:
        the ClassLoaderProvider to be used
      • setClassLoaderProvider

        public void setClassLoaderProvider​(ClassLoaderProvider classLoaderProvider)
        Sets the ClassLoaderProvider to be used. This object is especially required for resolving ClassDescription objects.
        Parameters:
        classLoaderProvider - the ClassLoaderProvider to be used
      • getInvocationHelper

        public InvocationHelper getInvocationHelper()
        Returns the current InvocationHelper. This object can be used by all components involved in the builder process if reflection operations or data type conversions are needed. If custom type converters are needed, they have to be registered at conversion helper managed by this instance.
        Returns:
        the InvocationHelper
        See Also:
        setInvocationHelper(InvocationHelper)
      • getRootBeanStore

        public BeanStore getRootBeanStore()
        Returns the root bean store. This method returns never null; if no root bean store has been set so far, a default one is created now.
        Returns:
        the root bean store
      • initRootBeanStore

        public void initRootBeanStore​(MutableBeanStore rootBeanStore)
        Sets the root bean store. This is the default bean store, to which all beans are added unless a specific bean store is specified. Using the addBeanStore() method it is possible to create further bean stores below this root store.
        Parameters:
        rootBeanStore - the root bean store
      • getBeanStore

        public BeanStore getBeanStore​(String name)
        Returns the BeanStore with the specified name. The root store can be obtained by passing in null for the name. If a name cannot be resolved, an exception is thrown.
        Parameters:
        name - the name of the desired bean store
        Returns:
        the bean store with this name
        Throws:
        NoSuchElementException - if the name cannot be resolved
      • hasBeanStore

        public boolean hasBeanStore​(String name)
        Returns a flag whether the bean store with the given name is present.
        Parameters:
        name - the name of the bean store in question
        Returns:
        a flag whether this bean store is present
      • getBeanStoreNames

        public Set<String> getBeanStoreNames()
        Returns a set with the names of the existing bean stores. These are the bean stores that have been added using the addBeanStore() method. Note that the root bean store (which is identified by the name null) is not contained in this set.
        Returns:
        a set with the names of the existing bean stores
      • addBeanStore

        public void addBeanStore​(String name,
                                 String parentName)
        Creates a BeanStore with the specified name and adds it to the internal list of existing bean stores. The new store is added as a child of the specified parent bean store. If null is passed in for the parent, the new store will become a child of the root bean store. Note that the names of bean stores must be unique, even if they are at different levels of the hierarchy.
        Parameters:
        name - the name of the bean store to be added (must not be null)
        parentName - the name of the parent bean store
        Throws:
        NoSuchElementException - if the parent name cannot be resolved
        IllegalArgumentException - if the name is null or is already used by an existing bean store
      • addBeanProvider

        public void addBeanProvider​(String storeName,
                                    String beanName,
                                    BeanProvider bean)
        Adds a BeanProvider to a BeanStore. The bean store with the specified name is resolved (null selects the root bean store), and the provided BeanProvider is added to it under the given name.
        Parameters:
        storeName - the name of the bean store
        beanName - the name of the bean provider (must not be null)
        bean - the bean provider to be added (must not be null)
        Throws:
        NoSuchElementException - if the bean store cannot be resolved
        IllegalArgumentException - if the bean name or the bean provider is null
      • addAnonymousBeanProvider

        public String addAnonymousBeanProvider​(String storeName,
                                               BeanProvider bean)
        Adds an "anonymous" BeanProvider to a BeanStore. This method can be used for beans that are visible in a local context only. It works similar to addBeanProvider(), but the bean store's addAnonymousBeanProvider() method will be called. This class will keep a counter for generating indices for anonymous bean providers. This ensures that the generated names used internally for these beans are unique.
        Parameters:
        storeName - the name of the bean store
        bean - the bean provider to be added (must not be null)
        Returns:
        the name generated for the bean provider added
        Throws:
        IllegalArgumentException - if no provider is specified
      • put

        public void put​(org.apache.commons.jelly.JellyContext context)
        Stores this instance in the specified context. It is stored there under a default key.
        Parameters:
        context - the Jelly context
      • get

        public static DIBuilderData get​(org.apache.commons.jelly.JellyContext context)
        Obtains an instance of this class from the specified Jelly context. The instance is looked up under a default key, the same that is also used by the put() method. If no instance can be found in this context, null is returned.
        Parameters:
        context - the Jelly context
        Returns:
        the instance found in this context
      • internalGetBeanStore

        protected MutableBeanStore internalGetBeanStore​(String name)
        Returns the internal bean store with the given name. Internally this class operates on mutable DefaultBeanStore objects. To its clients only the immutable BeanStore interface is exposed.
        Parameters:
        name - the name of the bean store to be retrieved (null means the root bean store)
        Returns:
        the bean store with this name
        Throws:
        NoSuchElementException - if the bean store cannot be resolved
      • createRootStore

        protected MutableBeanStore createRootStore()
        Creates the root bean store. This method is called by internalGetBeanStore() when the root bean store is accessed for the first time and has not been explicitly initialized.
        Returns:
        the new root bean store