org.softamis.net.registry.impl
Class DefaultConsumingRegistry<K extends java.io.Serializable,V extends java.io.Serializable>

java.lang.Object
  extended by org.softamis.net.registry.impl.AbstractConsumingRegistry<K,V>
      extended by org.softamis.net.registry.impl.DefaultConsumingRegistry<K,V>
Type Parameters:
K - type of keys used to identify registry items
V - type of values stored in the registry
All Implemented Interfaces:
RegistryEventProcessor<K,V>, ConsumingRegistry<K,V>
Direct Known Subclasses:
DefaultConsumingRegistry

public class DefaultConsumingRegistry<K extends java.io.Serializable,V extends java.io.Serializable>
extends AbstractConsumingRegistry<K,V>
implements RegistryEventProcessor<K,V>

Default implementation of ConsumingRegistry. It issues network notifications when item for given key should be requested or given item should be invalidated. To isolate details of network communication, it delegates the actual details of network communication to corresponding CommunicationHelper. In addition, it implements methods from RegistryEventProcessor interface which allows this class to handle notifications about item registrations/undregistration that are issued by provider registry and also notifications about items invalidation which could be issued by another ConsumerRegistries.

The CommunicationHelper used by instance of this class is source for events processed by RegistryEventProcessor.

Author:
Andrew Sazonov
See Also:
CommunicationHelper

Field Summary
static long DEFAULT_DISCOVERING_TIMEOUT
          Default discovering timeout used if timeout is not specified explicitely.
protected  boolean fClosed
          Method that indicates whether consumer registry was closed
protected  CommunicationHelper<K,V> fCommunicationHelper
          CommunicationHelper responsible for performing network communications
protected  java.lang.String fDefaultMessageSignature
           
protected  long fDiscoveringTimeout
          Timeout (in ms) used during discovering services.
protected  boolean fIssueRequestForInvalidatedService
          Option which defines policy how "COMMAND_ITEM_INVALIDATED" notification should be processed by ConsumingRegistry.
protected  java.util.Map<K,java.util.Set<V>> fItems
          Items stored in distributed registry.
protected static org.apache.commons.logging.Log fLog
           
protected  RegistryEventsMulticaster<K,V> fRegistryEventsMulticaster
          Processors that are responsible for processing of obtaining external notifications
 
Fields inherited from class org.softamis.net.registry.impl.AbstractConsumingRegistry
fDirtyKeys, fRequestItemsOnInit
 
Constructor Summary
DefaultConsumingRegistry()
           
 
Method Summary
 void addRegistryEventProcessor(RegistryEventProcessor<K,V> aListener)
          Adds listener to list of registered RegistryEventProcessors which performs further processing of obtained external notifications
protected  boolean checkExternalItemUnregistration(ItemEntry<K,V> aEntry)
          Tries to perform unregistration of the item issued by ProvidingRegistry.
 void close()
          Closed consumer registry.
protected  void discoverItems(K aItemKey)
          Performs discovering of items that are registered for given key.
protected  java.util.Set<V> doGetItems(K aItemKey)
          Performs acutual obtaining of items for given key from distributed registry.
protected  void doInit()
           
protected  void doMarkItemInvalid(K aItemKey, V aItem)
          Performs marking item invalid.
protected  void doRequestItems(K aItemKey)
          Perform actual request for items for particular key
 CommunicationHelper<K,V> getCommunicationHelper()
          Returns communicationHelper responsible for performing network communications
 java.lang.String getDefaultMessageSignature()
           
 long getDiscoveringTimeout()
          Returns timeout (in ms) used during discovering services.
 java.util.Set<K> getKeys(boolean aForceRefresh)
          Returns set of keys which are available in registry
 void init()
          Method that performs initialization of the consumer registry by additing it as listener to CommunicationHelper used to perform network communication.
protected  void insureCommunicationHelperSet()
           
 boolean isClosed()
          Indicated whether registry it in closed state
 boolean isIssueRequestForInvalidatedService()
          Returns option which defines policy how "COMMAND_ITEM_INVALIDATED" notification should be processed by ConsumingRegistry.
 void processItemInvalid(ItemEntry<K,V> aEntry)
          Handles notification about item which was invalidated by another ConsumingRegistry.
 void processItemRegistered(ItemEntry<K,V> aEntry)
          Processes notification about item registration issues by ProvidingRegistry.
 void processItemsRequest(ItemEntry<K,V> aEntry)
          Callback for processing CommunicationHelper.COMMAND_ITEM_REQUEST notification - typically will be processed by ProvidingRegistry
 void processItemUnregistered(ItemEntry<K,V> aEntry)
          Processes notification about item unregistration issued by ProvidingRegistry
 void removeRegistryEventProcessor(RegistryEventProcessor<K,V> aListener)
          Removes listener from list of registered RegistryEventProcessors which performs further processing of obtained external notifications
 void setCommunicationHelper(CommunicationHelper<K,V> aCommunicationHelper)
          Sets communicationHelper responsible for performing network communications
 void setDefaultMessageSignature(java.lang.String aDefaultMessageSignature)
           
 void setDiscoveringTimeout(long aDiscoveringTimeout)
          Sets timeout (in ms) used during discovering services.
 void setIssueRequestForInvalidatedService(boolean aIssueRequest)
          Sets option which defines policy how "COMMAND_ITEM_INVALIDATED" notification should be processed by ConsumingRegistry.
 
Methods inherited from class org.softamis.net.registry.impl.AbstractConsumingRegistry
getItems, isDirty, isRequestItemsOnInit, markDirty, markItemInvalid, requestItems, setRequestItemsOnInit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fLog

protected static final org.apache.commons.logging.Log fLog

fItems

protected final java.util.Map<K extends java.io.Serializable,java.util.Set<V extends java.io.Serializable>> fItems
Items stored in distributed registry. Since it's possible that there are several providers will publish items under the same key, the Set of items is stored under one key.


fCommunicationHelper

protected CommunicationHelper<K extends java.io.Serializable,V extends java.io.Serializable> fCommunicationHelper
CommunicationHelper responsible for performing network communications


fIssueRequestForInvalidatedService

protected boolean fIssueRequestForInvalidatedService
Option which defines policy how "COMMAND_ITEM_INVALIDATED" notification should be processed by ConsumingRegistry. If this option is set to true, the ConsumingRegistry will invalidate item and then issue "COMMAND_ITEM_REQUEST" for invalidated item key. If this option is set to false, the ConsumingRegistry will simply invalidate item.

See Also:
CommunicationHelper

fDiscoveringTimeout

protected long fDiscoveringTimeout
Timeout (in ms) used during discovering services. When ConsumingRegistry issues request for items, it will wait for responses from ProviderRegistries during specified period of time.


DEFAULT_DISCOVERING_TIMEOUT

public static final long DEFAULT_DISCOVERING_TIMEOUT
Default discovering timeout used if timeout is not specified explicitely.

See Also:
Constant Field Values

fClosed

protected boolean fClosed
Method that indicates whether consumer registry was closed


fRegistryEventsMulticaster

protected RegistryEventsMulticaster<K extends java.io.Serializable,V extends java.io.Serializable> fRegistryEventsMulticaster
Processors that are responsible for processing of obtaining external notifications


fDefaultMessageSignature

protected java.lang.String fDefaultMessageSignature
Constructor Detail

DefaultConsumingRegistry

public DefaultConsumingRegistry()
Method Detail

insureCommunicationHelperSet

protected void insureCommunicationHelperSet()

init

public void init()
          throws java.io.IOException
Method that performs initialization of the consumer registry by additing it as listener to CommunicationHelper used to perform network communication.

NOTE: This method SHOULD be called before working with consumer registry to let it handle requests for items registered, unregistered and invalidated. Actually, if this method is not called before usage, the registry will not contain information from provider registries

Throws:
java.io.IOException

doInit

protected void doInit()

close

public void close()
Closed consumer registry. Removes registry from the list of CommunicationHelper's listeners.

Specified by:
close in interface ConsumingRegistry<K extends java.io.Serializable,V extends java.io.Serializable>

isClosed

public boolean isClosed()
Description copied from interface: ConsumingRegistry
Indicated whether registry it in closed state

Specified by:
isClosed in interface ConsumingRegistry<K extends java.io.Serializable,V extends java.io.Serializable>
Returns:
true if registry instance is closed, false otherwise
See Also:
ConsumingRegistry.close()

getKeys

public java.util.Set<K> getKeys(boolean aForceRefresh)
Returns set of keys which are available in registry

Specified by:
getKeys in interface ConsumingRegistry<K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aForceRefresh -
Returns:

doGetItems

protected java.util.Set<V> doGetItems(K aItemKey)
Performs acutual obtaining of items for given key from distributed registry. If this is is the first attempt to obtain items for this key, method tries to discover items by issuing notification with request for items.

Specified by:
doGetItems in class AbstractConsumingRegistry<K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aItemKey - key of items
Returns:
set of items registered for given key

doRequestItems

protected void doRequestItems(K aItemKey)
Description copied from class: AbstractConsumingRegistry
Perform actual request for items for particular key

Specified by:
doRequestItems in class AbstractConsumingRegistry<K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aItemKey - key for items

doMarkItemInvalid

protected void doMarkItemInvalid(K aItemKey,
                                 V aItem)
Performs marking item invalid. First, method checks whether given item was actually reqistered for given key. If item was registered, it removed it from the storage of items and issues notification about item invalidation.

Specified by:
doMarkItemInvalid in class AbstractConsumingRegistry<K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aItemKey - key of the item
aItem - the item itself

discoverItems

protected void discoverItems(K aItemKey)
Performs discovering of items that are registered for given key. Discovering is performed during discovering timeout specified for registry.

Parameters:
aItemKey - key of the item.

processItemsRequest

public void processItemsRequest(ItemEntry<K,V> aEntry)
Description copied from interface: RegistryEventProcessor
Callback for processing CommunicationHelper.COMMAND_ITEM_REQUEST notification - typically will be processed by ProvidingRegistry

Specified by:
processItemsRequest in interface RegistryEventProcessor<K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aEntry - recevied item entry

processItemInvalid

public void processItemInvalid(ItemEntry<K,V> aEntry)
Handles notification about item which was invalidated by another ConsumingRegistry. First, the methods performs unregistration of given entry. Later, if the fIssueRequestForInvalidatedService option is set to true, the method tries to discover items for key stored in the entry.

Specified by:
processItemInvalid in interface RegistryEventProcessor<K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aEntry - entry marked as invalid

processItemUnregistered

public void processItemUnregistered(ItemEntry<K,V> aEntry)
Processes notification about item unregistration issued by ProvidingRegistry

Specified by:
processItemUnregistered in interface RegistryEventProcessor<K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aEntry - entry to register

checkExternalItemUnregistration

protected boolean checkExternalItemUnregistration(ItemEntry<K,V> aEntry)
Tries to perform unregistration of the item issued by ProvidingRegistry. First, method checks whether item was previously registered and if so, removes it from the local items storage.

Parameters:
aEntry - entry to be unregistered
Returns:
true if item was previouly registered was was actually unregistered

processItemRegistered

public void processItemRegistered(ItemEntry<K,V> aEntry)
Processes notification about item registration issues by ProvidingRegistry. First, the method checks whether item was not previously registered and if this is correct, it adds obtained item into the local storage and marks storage "dirty" for given key.

Specified by:
processItemRegistered in interface RegistryEventProcessor<K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aEntry - entry to be registered.

removeRegistryEventProcessor

public void removeRegistryEventProcessor(RegistryEventProcessor<K,V> aListener)
Removes listener from list of registered RegistryEventProcessors which performs further processing of obtained external notifications

Specified by:
removeRegistryEventProcessor in interface ConsumingRegistry<K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aListener - processor to be removed

addRegistryEventProcessor

public void addRegistryEventProcessor(RegistryEventProcessor<K,V> aListener)
Adds listener to list of registered RegistryEventProcessors which performs further processing of obtained external notifications

Specified by:
addRegistryEventProcessor in interface ConsumingRegistry<K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aListener - processor to be added

getCommunicationHelper

public CommunicationHelper<K,V> getCommunicationHelper()
Returns communicationHelper responsible for performing network communications

Returns:
communication helper

setCommunicationHelper

public void setCommunicationHelper(CommunicationHelper<K,V> aCommunicationHelper)
Sets communicationHelper responsible for performing network communications

Parameters:
aCommunicationHelper - communication helper

getDiscoveringTimeout

public long getDiscoveringTimeout()
Returns timeout (in ms) used during discovering services. When ConsumingRegistry issues request for items, it will wait for responses from ProviderRegistries during specified period of time.

Returns:
timeout (in ms) used during discovering services.

setDiscoveringTimeout

public void setDiscoveringTimeout(long aDiscoveringTimeout)
Sets timeout (in ms) used during discovering services. When ConsumingRegistry issues request for items, it will wait for responses from ProviderRegistries during specified period of time.

Parameters:
aDiscoveringTimeout - timeout (in ms) used during discovering services.

isIssueRequestForInvalidatedService

public boolean isIssueRequestForInvalidatedService()
Returns option which defines policy how "COMMAND_ITEM_INVALIDATED" notification should be processed by ConsumingRegistry. If this option is set to true, the ConsumingRegistry will invalidate item and then issue "COMMAND_ITEM_REQUEST" for invalidated item key. If this option is set to false, the ConsumingRegistry will simply invalidate item.

Returns:
policy used to process invalidated services

setIssueRequestForInvalidatedService

public void setIssueRequestForInvalidatedService(boolean aIssueRequest)
Sets option which defines policy how "COMMAND_ITEM_INVALIDATED" notification should be processed by ConsumingRegistry. If this option is set to true, the ConsumingRegistry will invalidate item and then issue "COMMAND_ITEM_REQUEST" for invalidated item key. If this option is set to false, the ConsumingRegistry will simply invalidate item.

Parameters:
aIssueRequest - policy for processing item invalidation notifications

getDefaultMessageSignature

public java.lang.String getDefaultMessageSignature()

setDefaultMessageSignature

public void setDefaultMessageSignature(java.lang.String aDefaultMessageSignature)