Class JellyBeanBuilder

  • All Implemented Interfaces:
    BeanBuilder
    Direct Known Subclasses:
    JellyBuilder

    public class JellyBeanBuilder
    extends Object
    implements BeanBuilder

    An implementation of the BeanBuilder interface that is able to process bean definitions defined in a Apache Commons Jelly script.

    This class prepares a JellyContext object and registers the dependency injection tag library (DITagLibrary). Then it invokes Jelly for evaluating the specified script.

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

      • JellyBeanBuilder

        protected JellyBeanBuilder()
        Creates a new instance of JellyBeanBuilder. Instances should only be created using the factory.
    • Method Detail

      • getDiBuilderNameSpaceURI

        public String getDiBuilderNameSpaceURI()
        Returns the name space URI, under which the DI tag library must be registered.
        Returns:
        the name space URI for the DI tag library
      • setDiBuilderNameSpaceURI

        public void setDiBuilderNameSpaceURI​(String diBuilderNameSpaceURI)
        Sets the name space URI for the DI tag library.
        Parameters:
        diBuilderNameSpaceURI - the new name space URI
      • build

        public BeanBuilderResult build​(Locator script,
                                       MutableBeanStore rootStore,
                                       ClassLoaderProvider loaderProvider)
                                throws BuilderException
        Executes a script with bean definitions. This implementation just calls the other build() method passing in a null InvocationHelper.
        Specified by:
        build in interface BeanBuilder
        Parameters:
        script - points to the script to be executed (must not be null)
        rootStore - the root BeanStore object; if defined, the processed bean definitions will be added to this store unless otherwise specified; if null, a new bean store will be created
        loaderProvider - an object with information about registered class loaders; can be null, then a default class loader provider will be used
        Returns:
        an object providing access to the BeanStore instances created or populated during the builder operation
        Throws:
        BuilderException - if an error occurs
      • build

        public BeanBuilderResult build​(Locator script,
                                       MutableBeanStore rootStore,
                                       ClassLoaderProvider loaderProvider,
                                       InvocationHelper invHlp)
                                throws BuilderException
        Executes a script with bean definitions and the specified helper objects. With this method all helper objects taking part in the builder operation can be specified. Passing the InvocationHelper can be useful if the script defines special beans which require custom data type converters. These converters can be configured in the ConversionHelper instance contained in the InvocationHelper. It is possible to pass null references for the helper objects. In this case default objects are created. Delegates to #executeScript() which does the real work.
        Specified by:
        build in interface BeanBuilder
        Parameters:
        script - points to the script to be executed (must not be null)
        rootStore - the root BeanStore object; if defined, the processed bean definitions will be added to this store unless otherwise specified; if null, a new bean store will be created
        loaderProvider - an object with information about registered class loaders; can be null, then a default class loader provider will be used
        invHlp - a helper object for reflection operations; can be null, then a default helper object will be used
        Returns:
        an object providing access to the BeanStore instances created or populated during the builder operation
        Throws:
        BuilderException - if an error occurs
      • release

        public void release​(BeanBuilderResult builderResult)
        Releases the specified BeanBuilderResult object. This will especially invoke the shutdown() method on all bean providers stored in one of the BeanContext objects contained in the result object.
        Specified by:
        release in interface BeanBuilder
        Parameters:
        builderResult - the BeanBuilderResult object to be released
        Throws:
        IllegalArgumentException - if the passed in result object is null or invalid
      • executeScript

        protected BeanBuilderResult executeScript​(Locator script,
                                                  org.apache.commons.jelly.JellyContext context,
                                                  MutableBeanStore rootStore,
                                                  ClassLoaderProvider loaderProvider,
                                                  InvocationHelper invHlp)
                                           throws BuilderException
        Executes the specified script on the given Jelly context. Occurring Jelly exceptions are caught and re-thrown as builder exceptions.
        Parameters:
        script - the script to be executed (must not be null)
        context - the Jelly context
        rootStore - the root bean store (can be null)
        loaderProvider - a data object with the registered class loaders (can be null)
        invHlp - a helper object for reflection operations (can be null)
        Returns:
        an object with the results of the builder operation
        Throws:
        BuilderException - if an error occurs while executing the script
        IllegalArgumentException - if the script is null
      • createBuilderData

        protected DIBuilderData createBuilderData​(org.apache.commons.jelly.JellyContext context,
                                                  MutableBeanStore rootStore,
                                                  ClassLoaderProvider loaderProvider,
                                                  InvocationHelper invHlp)
        Creates the DIBuilderData object used during the builder operation. This object holds central data required by multiple components involved in the builder operation.
        Parameters:
        context - the Jelly context
        rootStore - the root bean store
        loaderProvider - the class loader provider
        invHlp - the invocation helper
        Returns:
        the newly created DIBuilderData object
      • prepareInputSource

        protected InputSource prepareInputSource​(Locator script)
                                          throws IOException
        Prepares an InputSource object for the specified Locator. This method is called by executeScript(). The resulting InputSource is then passed to Jelly for processing the represented script. Note that the way Jelly deals with URLs is not compatible with Locator implementations derived from ByteArrayLocator (in-memory locators). This is due to the fact that the URL is first transformed into a string and later back into a URL. This implementation tries to work around this problem by creating the InputSource from the stream the Locator provides. If the URL can be transformed to a string and back to a URL, it is also set as the system ID of the input source (this makes it possible to resolve relative files).
        Parameters:
        script - the Locator pointing the the Jelly script
        Returns:
        an InputSource for this script
        Throws:
        IOException - if an IO error occurs
      • setUpJellyContext

        protected org.apache.commons.jelly.JellyContext setUpJellyContext()
        Creates and initializes the Jelly context to be used for executing the builder script. This method will also register the required tag libraries and perform other mandatory initialization steps.
        Returns:
        the fully initialized context
      • createJellyContext

        protected org.apache.commons.jelly.JellyContext createJellyContext()
        Creates the Jelly context for executing the builder script. This method is called by setUpJellyContext(). Its task is only the creation of the context, not its initialization.
        Returns:
        the newly created context
      • registerTagLibraries

        protected void registerTagLibraries​(org.apache.commons.jelly.JellyContext context)
        Registers the required builder tag libraries at the given context. This method is called by setUpJellyContext() before the builder script will be executed.
        Parameters:
        context - the context
      • fetchClassLoaderProvider

        protected ClassLoaderProvider fetchClassLoaderProvider​(ClassLoaderProvider clp)
        Returns a ClassLoaderProvider. Some of the builder methods need a ClassLoaderProvider, but the corresponding parameter is optional. This method is called to obtain a valid ClassLoaderProvider reference. If the passed in ClassLoaderProvider object is defined, it is directly returned. Otherwise a default ClassLoaderProvider is created, which does not has any registered class loaders.
        Parameters:
        clp - the input ClassLoaderProvider
        Returns:
        the ClassLoaderProvider to be used
      • createReleaseDependencyProvider

        protected DependencyProvider createReleaseDependencyProvider​(BeanBuilderResult result)
        Creates a DependencyProvider object that can be used during a release() operation. This method creates a restricted dependency provider that can be used for executing simple shutdown scripts, but does not support access to external beans.
        Parameters:
        result - the BeanBuilderResult object that is to be released
        Returns:
        the DependencyProvider for release operations
        Throws:
        IllegalArgumentException - if helper objects required for the dependency provider are undefined