Class DefaultFactoryService

  • All Implemented Interfaces:
    org.apache.avalon.framework.activity.Disposable, org.apache.avalon.framework.activity.Initializable, org.apache.avalon.framework.configuration.Configurable, org.apache.avalon.framework.logger.LogEnabled, FactoryService

    public class DefaultFactoryService
    extends org.apache.avalon.framework.logger.AbstractLogEnabled
    implements FactoryService, org.apache.avalon.framework.configuration.Configurable, org.apache.avalon.framework.activity.Initializable, org.apache.avalon.framework.activity.Disposable
    The Factory Service instantiates objects using specified class loaders. If none is specified, the default one will be used. avalon.component name="factory" lifestyle="singleton" avalon.service type="org.apache.fulcrum.factory.FactoryService"
    Version:
    $Id$
    Author:
    Eric Pugh, Ilkka Priha, Stephen McConnell
    • Method Detail

      • getPrimitiveClass

        protected static Class<?> getPrimitiveClass​(String type)
        Gets the class of a primitive type.
        Parameters:
        type - a primitive type.
        Returns:
        the corresponding class, or null.
      • getInstance

        public <T> T getInstance​(String className,
                                 ClassLoader loader)
                          throws FactoryException
        Gets an instance of a named class using a specified class loader.

        Class loaders are supported only if the isLoaderSupported method returns true. Otherwise the loader parameter is ignored.

        Specified by:
        getInstance in interface FactoryService
        Type Parameters:
        T - Type of the class
        Parameters:
        className - the name of the class.
        loader - the class loader.
        Returns:
        the instance.
        Throws:
        FactoryException - if instantiation fails.
      • getInstance

        public <T> T getInstance​(String className,
                                 Object[] params,
                                 String[] signature)
                          throws FactoryException
        Gets an instance of a named class. Parameters for its constructor are given as an array of objects, primitive types must be wrapped with a corresponding class.
        Specified by:
        getInstance in interface FactoryService
        Type Parameters:
        T - Type of the class
        Parameters:
        className - the name of the class.
        params - an array containing the parameters of the constructor.
        signature - an array containing the signature of the constructor.
        Returns:
        the instance.
        Throws:
        FactoryException - if instantiation fails.
      • getInstance

        public <T> T getInstance​(String className,
                                 ClassLoader loader,
                                 Object[] params,
                                 String[] signature)
                          throws FactoryException
        Gets an instance of a named class using a specified class loader. Parameters for its constructor are given as an array of objects, primitive types must be wrapped with a corresponding class.

        Class loaders are supported only if the isLoaderSupported method returns true. Otherwise the loader parameter is ignored.

        Specified by:
        getInstance in interface FactoryService
        Type Parameters:
        T - Type of the class
        Parameters:
        className - the name of the class.
        loader - the class loader.
        params - an array containing the parameters of the constructor.
        signature - an array containing the signature of the constructor.
        Returns:
        the instance.
        Throws:
        FactoryException - if instantiation fails.
      • getInstance

        protected <T> T getInstance​(Class<T> clazz,
                                    Object[] params,
                                    String[] signature)
                             throws FactoryException
        Gets an instance of a specified class. Parameters for its constructor are given as an array of objects, primitive types must be wrapped with a corresponding class.
        Type Parameters:
        T - Type of the class
        Parameters:
        clazz - the class
        params - an array containing the parameters of the constructor
        signature - an array containing the signature of the constructor
        Returns:
        the instance
        Throws:
        FactoryException - if instantiation fails.
      • getSignature

        public Class<?>[] getSignature​(Class<?> clazz,
                                       Object[] params,
                                       String[] signature)
                                throws ClassNotFoundException
        Gets the signature classes for parameters of a method of a class.
        Specified by:
        getSignature in interface FactoryService
        Parameters:
        clazz - the class.
        params - an array containing the parameters of the method.
        signature - an array containing the signature of the method.
        Returns:
        an array of signature classes. Note that in some cases objects in the parameter array can be switched to the context of a different class loader.
        Throws:
        ClassNotFoundException - if any of the classes is not found.
      • switchObjectContext

        protected Object switchObjectContext​(Object object,
                                             ClassLoader loader)
        Switches an object into the context of a different class loader.
        Parameters:
        object - an object to switch.
        loader - the ClassLoader to use
        loader - the loader of the new context.
        Returns:
        the object
      • getFactory

        protected <T> Factory<T> getFactory​(String className)
                                     throws FactoryException
        Gets a customized factory for a named class. If no class-specific factory is specified but a default factory is, will use the default factory.
        Parameters:
        className - the name of the class to load.
        Returns:
        the factory, or null if not specified and no default.
        Throws:
        FactoryException - if instantiation of the factory fails.
      • configure

        public void configure​(org.apache.avalon.framework.configuration.Configuration conf)
                       throws org.apache.avalon.framework.configuration.ConfigurationException
        Specified by:
        configure in interface org.apache.avalon.framework.configuration.Configurable
        Throws:
        org.apache.avalon.framework.configuration.ConfigurationException
      • initialize

        public void initialize()
                        throws Exception
        Avalon component lifecycle method Initializes the service by loading default class loaders and customized object factories.
        Specified by:
        initialize in interface org.apache.avalon.framework.activity.Initializable
        Throws:
        Exception - if initialization fails.
      • dispose

        public void dispose()
        Avalon component lifecycle method Clear lists and maps
        Specified by:
        dispose in interface org.apache.avalon.framework.activity.Disposable