net.sf.taverna.raven.spi
Class InstanceRegistry<IType>

java.lang.Object
  extended by net.sf.taverna.raven.spi.InstanceRegistry<IType>
Type Parameters:
IType - The interface type implemented by all instances of the SPI to be tracked by this InstanceRegistry. This must be the type corresponding to the interface name specified in the SpiRegistry to which this binds.
All Implemented Interfaces:
java.lang.Iterable<IType>, RegistryListener

public class InstanceRegistry<IType>
extends java.lang.Object
implements java.lang.Iterable<IType>, RegistryListener

Instance registry to sit on top of the SpiRegistry object. When changes occur within the list of available SPI classes this object is responsible for creating new instances of those classes and notifying any interested listeners that this has changed.

Instantiation is lazy (as is the underlying scan for Classes in the SpiRegistry) so may incur a delay the first time the getInstances() method is called. Once this method has been called any subsequent events from the SpiRegistry will cause a regeneration of the instance list although this is conservative and will keep any instances of existing unchanged Class objects rather than creating new ones.

Author:
Tom Oinn

Constructor Summary
InstanceRegistry(SpiRegistry registry, java.lang.Object[] cArgs)
          Build a new InstanceRegistry which will listen to events from an underlying SpiRegistry and automatically construct a single instance of each class found by that registry using the specified arguments to the constructor.
 
Method Summary
 void addRegistryListener(InstanceRegistryListener l)
          Add a new registry listener to be notified of any updates to this SpiRegistry
 void finalize()
           
 java.util.List<IType> getInstances()
          Return a copy of the List of instances of classes within the underlying SpiRegistry
 java.util.Iterator<IType> iterator()
          Returns an iterator over a copy of the instance list to avoid potential concurrent modification exceptions when update events occur in the underlying registry.
 void removeRegistryListener(InstanceRegistryListener l)
          Remove a listener from this SpiRegistry
 void spiRegistryUpdated(SpiRegistry registry)
          If the instance list exists then update it, if set to null then we don't need to as it will be updated automatically when the list is first accessed through the getInstances method
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InstanceRegistry

public InstanceRegistry(SpiRegistry registry,
                        java.lang.Object[] cArgs)
Build a new InstanceRegistry which will listen to events from an underlying SpiRegistry and automatically construct a single instance of each class found by that registry using the specified arguments to the constructor.

Parameters:
registry - The SpiRegistry which provides Class objects implementing the generic type defined here.
cArgs - Arguments for the constructors of the new objects
Throws:
java.lang.ClassCastException - if the underlying classname for the SpiRegistry and the declared generic type for this object don't match.
Method Detail

addRegistryListener

public void addRegistryListener(InstanceRegistryListener l)
Add a new registry listener to be notified of any updates to this SpiRegistry

Parameters:
l -

finalize

public void finalize()
Overrides:
finalize in class java.lang.Object

getInstances

public java.util.List<IType> getInstances()
Return a copy of the List of instances of classes within the underlying SpiRegistry

Returns:
instance List

iterator

public java.util.Iterator<IType> iterator()
Returns an iterator over a copy of the instance list to avoid potential concurrent modification exceptions when update events occur in the underlying registry.

Specified by:
iterator in interface java.lang.Iterable<IType>

removeRegistryListener

public void removeRegistryListener(InstanceRegistryListener l)
Remove a listener from this SpiRegistry

Parameters:
l -

spiRegistryUpdated

public void spiRegistryUpdated(SpiRegistry registry)
If the instance list exists then update it, if set to null then we don't need to as it will be updated automatically when the list is first accessed through the getInstances method

Specified by:
spiRegistryUpdated in interface RegistryListener