org.apache.fulcrum.factory
Class DefaultFactoryService

java.lang.Object
  extended by org.apache.avalon.framework.logger.AbstractLogEnabled
      extended by org.apache.fulcrum.factory.DefaultFactoryService
All Implemented Interfaces:
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

The Factory Service instantiates objects using specified class loaders. If none is specified, the default one will be used.

Version:
$Id: DefaultFactoryService.java 670327 2008-06-22 09:32:41Z tv $
Author:
Eric Pugh, Ilkka Priha, Stephen McConnell

Field Summary
protected static String DEFAULT_FACTORY
          The name of the default factory.
protected  boolean initialized
           
 
Fields inherited from interface org.apache.fulcrum.factory.FactoryService
ROLE
 
Constructor Summary
DefaultFactoryService()
           
 
Method Summary
 void configure(org.apache.avalon.framework.configuration.Configuration conf)
          Avalon component lifecycle method
protected  Factory getFactory(String className)
          Gets a customized factory for a named class.
 Object getInstance(Class clazz)
          Gets an instance of a specified class.
protected  Object getInstance(Class clazz, Object[] params, String[] signature)
          Gets an instance of a specified class.
 Object getInstance(String className)
          Gets an instance of a named class.
 Object getInstance(String className, ClassLoader loader)
          Gets an instance of a named class using a specified class loader.
 Object getInstance(String className, ClassLoader loader, Object[] params, String[] signature)
          Gets an instance of a named class using a specified class loader.
 Object getInstance(String className, Object[] params, String[] signature)
          Gets an instance of a named class.
protected static Class getPrimitiveClass(String type)
          Gets the class of a primitive type.
 Class[] getSignature(Class clazz, Object[] params, String[] signature)
          Gets the signature classes for parameters of a method of a class.
 void initialize()
          Avalon component lifecycle method Initializes the service by loading default class loaders and customized object factories.
 boolean isLoaderSupported(String className)
          Tests if specified class loaders are supported for a named class.
protected  Class loadClass(String className)
          Loads the named class using the default class loader.
protected  Class loadClass(String className, ClassLoader loader)
          Loads the named class using a specified class loader.
protected  Object switchObjectContext(Object object, ClassLoader loader)
          Switches an object into the context of a different class loader.
 
Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

initialized

protected boolean initialized

DEFAULT_FACTORY

protected static final String DEFAULT_FACTORY
The name of the default factory.

See Also:
Constant Field Values
Constructor Detail

DefaultFactoryService

public DefaultFactoryService()
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 Object getInstance(String className)
                   throws FactoryException
Gets an instance of a named class.

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

getInstance

public Object 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
Parameters:
className - the name of the class.
loader - the class loader.
Returns:
the instance.
Throws:
FactoryException - if instantiation fails.

getInstance

public Object 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
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 Object 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
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.

isLoaderSupported

public boolean isLoaderSupported(String className)
                          throws FactoryException
Tests if specified class loaders are supported for a named class.

Specified by:
isLoaderSupported in interface FactoryService
Parameters:
className - the name of the class.
Returns:
true if class loaders are supported, false otherwise.
Throws:
FactoryException - if test fails.

getInstance

public Object getInstance(Class clazz)
                   throws FactoryException
Gets an instance of a specified class.

Specified by:
getInstance in interface FactoryService
Parameters:
clazz - the class.
Returns:
the instance.
Throws:
FactoryException - if instantiation fails.

getInstance

protected Object getInstance(Class 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.

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 loader of the new context.

loadClass

protected Class loadClass(String className)
                   throws ClassNotFoundException
Loads the named class using the default class loader.

Parameters:
className - the name of the class to load.
Returns:
the loaded class.
Throws:
ClassNotFoundException - if the class was not found.

loadClass

protected Class loadClass(String className,
                          ClassLoader loader)
                   throws ClassNotFoundException
Loads the named class using a specified class loader.

Parameters:
className - the name of the class to load.
loader - the loader to use.
Returns:
the loaded class.
Throws:
ClassNotFoundException - if the class was not found.

getFactory

protected Factory 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
Avalon component lifecycle method

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:
InitializationException - if initialization fails.
Exception


Copyright © 2005-2009 The Apache Software Foundation. All Rights Reserved.