Package org.apache.fulcrum.factory
Class DefaultFactoryService
- java.lang.Object
-
- org.apache.avalon.framework.logger.AbstractLogEnabled
-
- org.apache.fulcrum.factory.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
-
-
Field Summary
Fields Modifier and Type Field Description protected static String
DEFAULT_FACTORY
The name of the default factory.-
Fields inherited from interface org.apache.fulcrum.factory.FactoryService
ROLE
-
-
Constructor Summary
Constructors Constructor Description DefaultFactoryService()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
configure(org.apache.avalon.framework.configuration.Configuration conf)
void
dispose()
Avalon component lifecycle method Clear lists and mapsprotected <T> Factory<T>
getFactory(String className)
Gets a customized factory for a named class.<T> T
getInstance(Class<T> clazz)
Gets an instance of a specified class.protected <T> T
getInstance(Class<T> clazz, Object[] params, String[] signature)
Gets an instance of a specified class.<T> T
getInstance(String className)
Gets an instance of a named class.<T> T
getInstance(String className, ClassLoader loader)
Gets an instance of a named class using a specified class loader.<T> T
getInstance(String className, ClassLoader loader, Object[] params, String[] signature)
Gets an instance of a named class using a specified class loader.<T> T
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 <T> Class<T>
loadClass(String className)
Loads the named class using the default class loader.protected <T> Class<T>
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.
-
-
-
Field Detail
-
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 <T> T getInstance(String className) throws FactoryException
Gets an instance of a named class.- Specified by:
getInstance
in interfaceFactoryService
- Type Parameters:
T
- Type of the class- Parameters:
className
- the name of the class.- Returns:
- the instance.
- Throws:
FactoryException
- if instantiation fails.
-
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 interfaceFactoryService
- 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 interfaceFactoryService
- 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 interfaceFactoryService
- 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.
-
isLoaderSupported
public boolean isLoaderSupported(String className) throws FactoryException
Tests if specified class loaders are supported for a named class.- Specified by:
isLoaderSupported
in interfaceFactoryService
- Parameters:
className
- the name of the class.- Returns:
- true if class loaders are supported, false otherwise.
- Throws:
FactoryException
- if test fails.
-
getInstance
public <T> T getInstance(Class<T> clazz) throws FactoryException
Gets an instance of a specified class.- Specified by:
getInstance
in interfaceFactoryService
- Type Parameters:
T
- Type of the class- Parameters:
clazz
- the class.- 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 classparams
- an array containing the parameters of the constructorsignature
- 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 interfaceFactoryService
- 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 useloader
- the loader of the new context.- Returns:
- the object
-
loadClass
protected <T> Class<T> 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 <T> Class<T> 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 <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 interfaceorg.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 interfaceorg.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 interfaceorg.apache.avalon.framework.activity.Disposable
-
-