The Factory Service instantiates objects from the given class name using either the given class loader or an applicable one found from the class loader repository. If neither one is specified, the default class loader will be used.
The service provides the following benefits compared to Class.forName():
First, here is the role configuration.
<role
name="org.apache.fulcrum.factory.FactoryService"
shorthand="factory"
default-class="org.apache.fulcrum.factory.DefaultFactoryService"/>
There is configuration values for classloaders, still need to add.
<factory/>
In Turbine, the Factory Service is currently only used internally by the Pool Service. Applications can also use the service instead of Class.forName() and for unifying initialization, configuration and access to vendor specific object factories. The following is a simplified example of a customized DOM parser factory:
package org.foo.xml;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.fulcrum.ServiceException;
import org.apache.fulcrum.factory.Factory;
/**
* A factory for instantiating DOM parsers.
*/
public class DomBuilderFactory implements Factory
{
/**
* The implementation of the factory.
*/
private DocumentBuilderFactory factory;
/**
* Initializes the factory.
*/
public void init(String className)
throws ServiceException
{
factory = DocumentBuilderFactory.newInstance();
}
/**
* Gets a DocumentBuilder instance.
*/
public Object getInstance()
throws ServiceException
{
try
{
return factory.newDocumentBuilder();
}
catch (ParserConfigurationException x)
{
throw new TurbineException(x);
}
}
/**
* Gets a DocumentBuilder instance.
* The given loader is ignored.
*/
public Object getInstance(ClassLoader loader)
throws ServiceException
{
return getInstance();
}
/**
* Gets a DocumentBuilder instance.
* Constructor parameters are ignored.
*/
public Object getInstance(Object[] params,
String[] signature)
throws ServiceException
{
return getInstance();
}
/**
* Gets a DocumentBuilder instance.
* The given loader and constructor parameters are ignored.
*/
public Object getInstance(ClassLoader loader,
Object[] params,
String[] signature)
throws ServiceException
{
return getInstance();
}
/**
* Returns false as given class loaders are not supported.
*/
public boolean isLoaderSupported()
{
return false;
}
}
The customized DOM parser factory must be specified in Turbine Resources before it can be used:
services.FactoryService.factory.javax.xml.parsers.DocumentBuilder=org.foo.xml.DomBuilderFactory
A DOM parser can now be instantiated with the following code fragment:
// Access the service singleton.
FactoryService service = (FactoryService)
TurbineServices.getInstance().getService(FactoryService.SERVICE_NAME);
// Create a new DOM parser.
DocumentBuilder parser =
service.getInstance("javax.xml.parsers.DocumentBuilder");