gate.util.spring
Class DuplicateResourceFactoryBean

java.lang.Object
  extended by gate.util.spring.GateAwareObject
      extended by gate.util.spring.DuplicateResourceFactoryBean
All Implemented Interfaces:
org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.FactoryBean

public class DuplicateResourceFactoryBean
extends GateAwareObject
implements org.springframework.beans.factory.FactoryBean, org.springframework.beans.factory.DisposableBean

Spring factory bean to create duplicate copies of a GATE resource. This bean would typically be declared with singleton scope, but the factory produces a new duplicate of its template resource each time getBean is called (or each time it is injected as a dependency).


Field Summary
private  List<ResourceCustomiser> customisers
          Customisers that are applied to the duplicated resource before it is returned.
private  List<WeakReference<Resource>> resourcesReturned
          A list of weak references to the duplicates that have been returned, so they can be freed when this factory bean is disposed.
private  boolean returnTemplate
          Should we return the template itself the first time getObject() is called, or should we keep it for use only as a template and just return duplicates?
private  Resource templateResource
          The template resource which we will duplicate.
private  Class<?> typeClass
           
 
Constructor Summary
DuplicateResourceFactoryBean()
           
 
Method Summary
 void destroy()
          Delete the duplicates we have returned, unless they have already been freed.
 Object getObject()
           
 Class<?> getObjectType()
          Returns a proxy class that implements the same set of GATE interfaces as the template resource.
 boolean isSingleton()
          This factory is not a singleton - it produces a new object each time getObject() is called.
 void setCustomisers(List<ResourceCustomiser> customisers)
          Optional customisers that will be applied to the duplicate resource before it is returned.
 void setReturnTemplate(boolean returnTemplate)
          Should this factory bean return the template resource itself the first time getObject() is called, or should it always return a duplicate, keeping the template in pristine condition.
 void setTemplate(Resource template)
          Set the template resource that this factory bean will duplicate.
 
Methods inherited from class gate.util.spring.GateAwareObject
ensureGateInit, setBeanFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

templateResource

private Resource templateResource
The template resource which we will duplicate.


returnTemplate

private boolean returnTemplate
Should we return the template itself the first time getObject() is called, or should we keep it for use only as a template and just return duplicates?


customisers

private List<ResourceCustomiser> customisers
Customisers that are applied to the duplicated resource before it is returned.


resourcesReturned

private List<WeakReference<Resource>> resourcesReturned
A list of weak references to the duplicates that have been returned, so they can be freed when this factory bean is disposed.


typeClass

private Class<?> typeClass
Constructor Detail

DuplicateResourceFactoryBean

public DuplicateResourceFactoryBean()
Method Detail

setTemplate

public void setTemplate(Resource template)
Set the template resource that this factory bean will duplicate.


setReturnTemplate

public void setReturnTemplate(boolean returnTemplate)
Should this factory bean return the template resource itself the first time getObject() is called, or should it always return a duplicate, keeping the template in pristine condition. Generally, if the duplicates will all be created up-front, and there are no configured customisers, then it is safe to set this option to true. In cases where the duplicates may be created asynchronously (possibly creating one duplicate while another one is in use in a different thread) or may be customised after creation it is safer to set this option to false (the default) to keep the template pristine and always return duplicates.


setCustomisers

public void setCustomisers(List<ResourceCustomiser> customisers)
Optional customisers that will be applied to the duplicate resource before it is returned. If customisers are specified then it is strongly recommended to leave the returnTemplate option set to false.


getObject

public Object getObject()
                 throws Exception
Specified by:
getObject in interface org.springframework.beans.factory.FactoryBean
Throws:
Exception

getObjectType

public Class<?> getObjectType()
Returns a proxy class that implements the same set of GATE interfaces as the template resource. See Factory.duplicate(Resource) for the list of interfaces that will be considered.

Specified by:
getObjectType in interface org.springframework.beans.factory.FactoryBean

isSingleton

public boolean isSingleton()
This factory is not a singleton - it produces a new object each time getObject() is called.

Specified by:
isSingleton in interface org.springframework.beans.factory.FactoryBean

destroy

public void destroy()
Delete the duplicates we have returned, unless they have already been freed.

Specified by:
destroy in interface org.springframework.beans.factory.DisposableBean