org.apache.turbine.services.factory
Class TurbineFactoryService

java.lang.Object
  extended byorg.apache.turbine.services.BaseInitable
      extended byorg.apache.turbine.services.BaseService
          extended byorg.apache.turbine.services.TurbineBaseService
              extended byorg.apache.turbine.services.factory.TurbineFactoryService
All Implemented Interfaces:
FactoryService, Initable, Service

public class TurbineFactoryService
extends TurbineBaseService
implements FactoryService

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

Version:
$Id: TurbineFactoryService.java 534527 2007-05-02 16:10:59Z tv $
Author:
Ilkka Priha

Field Summary
static java.lang.String CLASS_LOADERS
          The property specifying a set of additional class loaders.
static java.lang.String OBJECT_FACTORY
          The property prefix specifying additional object factories.
 
Fields inherited from class org.apache.turbine.services.BaseService
configuration, name, serviceBroker
 
Fields inherited from class org.apache.turbine.services.BaseInitable
initableBroker, isInitialized
 
Fields inherited from interface org.apache.turbine.services.factory.FactoryService
SERVICE_NAME
 
Constructor Summary
TurbineFactoryService()
          Constructs a Factory Service.
 
Method Summary
protected  Factory getFactory(java.lang.String className)
          Gets a customized factory for a named class.
protected  java.lang.Object getInstance(java.lang.Class clazz)
          Gets an instance of a specified class.
protected  java.lang.Object getInstance(java.lang.Class clazz, java.lang.Object[] params, java.lang.String[] signature)
          Gets an instance of a specified class.
 java.lang.Object getInstance(java.lang.String className)
          Gets an instance of a named class.
 java.lang.Object getInstance(java.lang.String className, java.lang.ClassLoader loader)
          Gets an instance of a named class using a specified class loader.
 java.lang.Object getInstance(java.lang.String className, java.lang.ClassLoader loader, java.lang.Object[] params, java.lang.String[] signature)
          Gets an instance of a named class using a specified class loader.
 java.lang.Object getInstance(java.lang.String className, java.lang.Object[] params, java.lang.String[] signature)
          Gets an instance of a named class.
protected static java.lang.Class getPrimitiveClass(java.lang.String type)
          Gets the class of a primitive type.
 java.lang.Class[] getSignature(java.lang.Class clazz, java.lang.Object[] params, java.lang.String[] signature)
          Gets the signature classes for parameters of a method of a class.
 void init()
          Initializes the service by loading default class loaders and customized object factories.
 boolean isLoaderSupported(java.lang.String className)
          Tests if specified class loaders are supported for a named class.
protected  java.lang.Class loadClass(java.lang.String className)
          Loads the named class using the default class loader.
protected  java.lang.Class loadClass(java.lang.String className, java.lang.ClassLoader loader)
          Loads the named class using a specified class loader.
protected  java.lang.Object switchObjectContext(java.lang.Object object, java.lang.ClassLoader loader)
          Switches an object into the context of a different class loader.
 
Methods inherited from class org.apache.turbine.services.TurbineBaseService
init, init, init, shutdown
 
Methods inherited from class org.apache.turbine.services.BaseService
getConfiguration, getName, getProperties, getServiceBroker, setName, setServiceBroker
 
Methods inherited from class org.apache.turbine.services.BaseInitable
getInit, getInitableBroker, setInit, setInitableBroker
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.turbine.services.Service
getConfiguration, getName, getProperties, setName, setServiceBroker
 
Methods inherited from interface org.apache.turbine.services.Initable
getInit, init, setInitableBroker, shutdown
 

Field Detail

CLASS_LOADERS

public static final java.lang.String CLASS_LOADERS
The property specifying a set of additional class loaders.

See Also:
Constant Field Values

OBJECT_FACTORY

public static final java.lang.String OBJECT_FACTORY
The property prefix specifying additional object factories.

See Also:
Constant Field Values
Constructor Detail

TurbineFactoryService

public TurbineFactoryService()
Constructs a Factory Service.

Method Detail

getPrimitiveClass

protected static java.lang.Class getPrimitiveClass(java.lang.String type)
Gets the class of a primitive type.

Parameters:
type - a primitive type.
Returns:
the corresponding class, or null.

init

public void init()
          throws InitializationException
Initializes the service by loading default class loaders and customized object factories.

Specified by:
init in interface Initable
Overrides:
init in class TurbineBaseService
Throws:
InitializationException - if initialization fails.

getInstance

public java.lang.Object getInstance(java.lang.String className)
                             throws TurbineException
Gets an instance of a named class.

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

getInstance

public java.lang.Object getInstance(java.lang.String className,
                                    java.lang.ClassLoader loader)
                             throws TurbineException
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:
TurbineException - if instantiation fails.

getInstance

public java.lang.Object getInstance(java.lang.String className,
                                    java.lang.Object[] params,
                                    java.lang.String[] signature)
                             throws TurbineException
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:
TurbineException - if instantiation fails.

getInstance

public java.lang.Object getInstance(java.lang.String className,
                                    java.lang.ClassLoader loader,
                                    java.lang.Object[] params,
                                    java.lang.String[] signature)
                             throws TurbineException
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:
TurbineException - if instantiation fails.

isLoaderSupported

public boolean isLoaderSupported(java.lang.String className)
                          throws TurbineException
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:
TurbineException - if test fails.

getInstance

protected java.lang.Object getInstance(java.lang.Class clazz)
                                throws TurbineException
Gets an instance of a specified class.

Parameters:
clazz - the class.
Returns:
the instance.
Throws:
TurbineException - if instantiation fails.

getInstance

protected java.lang.Object getInstance(java.lang.Class clazz,
                                       java.lang.Object[] params,
                                       java.lang.String[] signature)
                                throws TurbineException
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:
TurbineException - if instantiation fails.

getSignature

public java.lang.Class[] getSignature(java.lang.Class clazz,
                                      java.lang.Object[] params,
                                      java.lang.String[] signature)
                               throws java.lang.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:
java.lang.ClassNotFoundException - if any of the classes is not found.

switchObjectContext

protected java.lang.Object switchObjectContext(java.lang.Object object,
                                               java.lang.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 java.lang.Class loadClass(java.lang.String className)
                             throws java.lang.ClassNotFoundException
Loads the named class using the default class loader.

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

loadClass

protected java.lang.Class loadClass(java.lang.String className,
                                    java.lang.ClassLoader loader)
                             throws java.lang.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:
java.lang.ClassNotFoundException - if the class was not found.

getFactory

protected Factory getFactory(java.lang.String className)
                      throws TurbineException
Gets a customized factory for a named class.

Parameters:
className - the name of the class to load.
Returns:
the factory or null if not specified.
Throws:
TurbineException - if instantiation of the factory fails.


Copyright © 2000-2008 Apache Software Foundation. All Rights Reserved.