public class DefaultFactoryProducer extends Object implements FactoryProducer
Factory classes will be looked up in a configuration file called CONFIG_FILE
.
The format of the file is as follows: blank lines are allowed (and ignored); comments are
allowed (and ignored). The comment indicator is (#).
Each functional line of the configuration file has two parts, the factory class name, and the
scope class name. For example:
scope.class.name : factory.class.name
Each factory class name must have a two-argument constructor. The first argument is the scope class,
the very same class that is passed into the getFactory(Object)
method. The second
argument is the optional delegate factory that the specified factory will defer to if it is not
able to locate the specified object. (If no parent is specified, no delegation will occur).
Only one factory class per scope may be defined. If more than one is found, a configuration error will be thrown.
By default, DefaultFactoryProducer knows how to get access to the ServletContext from within the ICS object, and so a factory corresponding to the ServletContext scope will be used as the delegate for the ICS scoped factory. Users may override either of these two and the relationship will be preserved.
Conversely, this factory producer does not know the relationship between any custom scope and the two it knows about, and so it is unable to automatically wire delegation between factories involving custom scopes. To do this, it is necessary to extend this class (or replace it).
Also, this factory producer does not know how to cache factories with custom scopes, so new versions will be created whenever one is requested. For the known scopes, caching is built-in.
Modifier and Type | Field and Description |
---|---|
protected static String |
CONFIG_FILE
The configuration file(s) that this class will read while looking for
information about which factories to use
|
protected static String |
ICS_CONTEXT_BACKED_FACTORY
The ics object pool key for the ics-backed factory.
|
protected static String |
SERVLET_CONTEXT_BACKED_FACTORY
The servlet context attribute key for the servletContext-backed factory.
|
Constructor and Description |
---|
DefaultFactoryProducer()
Creates a new factory producer.
|
Modifier and Type | Method and Description |
---|---|
protected Factory |
createFactory(COM.FutureTense.Interfaces.ICS ics)
Create a new instance of the ics-backed factory.
|
protected Factory |
createFactory(Object customScope)
Create a custom-scoped factory.
|
protected Factory |
createFactory(javax.servlet.ServletContext servletContext)
Create a new instance of the servletContext-backed factory.
|
protected static Map<Class,Constructor<Factory>> |
getConstructorMap(Map<Class,Class> config)
Converts the map of classes to a map containing factory constructors as the value.
|
protected Factory |
getFactory(COM.FutureTense.Interfaces.ICS ics)
Get the ics-backed factory.
|
Factory |
getFactory(Object scope)
Get the factory configured for the scope specified.
|
protected Factory |
getFactory(javax.servlet.ServletContext servletContext)
Get the servletContext-backed factory.
|
protected static Map<Class,Class> |
lookupConfiguration(ClassLoader classLoader)
Look up the configuration for the FactoryProducer by reading the
CONFIG_FILE s found
in the specified ClassLoader's classpath. |
protected static final String CONFIG_FILE
protected static final String ICS_CONTEXT_BACKED_FACTORY
protected static final String SERVLET_CONTEXT_BACKED_FACTORY
public DefaultFactoryProducer()
If no factory is configured for ICS or ServletContext, a default is provided.
public Factory getFactory(Object scope)
FactoryProducer
getFactory
in interface FactoryProducer
scope
- the scopeprotected Factory createFactory(Object customScope)
customScope
- custom scopeprotected Factory getFactory(COM.FutureTense.Interfaces.ICS ics)
ICS_CONTEXT_BACKED_FACTORY
as the key.ics
- ics contextprotected Factory createFactory(COM.FutureTense.Interfaces.ICS ics)
This implementation uses the constructor configured in the CONFIG_FILE
. It also
uses ICS to locate the ServletContext, and then gets the servletContext-backed factory
which it will use as a delegate for the ics-backed factory.
ics
- the ics contextprotected Factory getFactory(javax.servlet.ServletContext servletContext)
SERVLET_CONTEXT_BACKED_FACTORY
as the key.servletContext
- the servlet contextprotected Factory createFactory(javax.servlet.ServletContext servletContext)
This implementation uses the constructor configured in the CONFIG_FILE
.
servletContext
- the servlet contextprotected static Map<Class,Class> lookupConfiguration(ClassLoader classLoader)
CONFIG_FILE
s found
in the specified ClassLoader's classpath. The configuration file format is described above.classLoader
- the classloader to do the lookingIllegalStateException
- if the config file format is invalid, if more than one
configuration setting is found for a given scope, or if the config
file points to classes that are not found.protected static Map<Class,Constructor<Factory>> getConstructorMap(Map<Class,Class> config)
This class will look for a constructor with the signature described above, and will fail if one is not found.
config
- the mapping between scope and factory constructors to be used to create
the factoriesIllegalStateException
- if no constructor can be found with the appropriate signature,
or if the class specified does not implement the Factory interface.Copyright © 2010–2017. All rights reserved.