org.softamis.cluster4spring.support.provider
Class AbstractDiscoveringEndpointProvider<E extends Endpoint<SI>,SI extends ServiceMoniker>

java.lang.Object
  extended by org.softamis.cluster4spring.support.provider.BaseEndpointProvider<E,SI>
      extended by org.softamis.cluster4spring.support.provider.MultiURLEndpointProvider<E,SI>
          extended by org.softamis.cluster4spring.support.provider.AbstractUrlListEndpointProvider<E,SI>
              extended by org.softamis.cluster4spring.support.provider.AbstractDiscoveringEndpointProvider<E,SI>
Type Parameters:
SI - type of data used to invoke remote service (such as remote service URL)
E - type of endpoints that could be created by this factory
All Implemented Interfaces:
java.util.EventListener, EndpointProvider<E,SI>, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationListener
Direct Known Subclasses:
DiscoveringEndpointProvider

public abstract class AbstractDiscoveringEndpointProvider<E extends Endpoint<SI>,SI extends ServiceMoniker>
extends AbstractUrlListEndpointProvider<E,SI>
implements org.springframework.context.ApplicationListener

Base abstract class for implementation of EndpointProvider that automatically discovers services based on service name and service group. Such auto-discoverable services are published via appropriate ServicePublisher. Class contains all basic logic related to discovering, but does not include any references to underlying services storage.

Author:
Andrew Sazonov

Field Summary
protected  java.lang.String fProtocolName
          Name of protocol
protected  java.lang.String fServiceGroup
          Group to which service belong
protected  java.lang.String fServiceKey
           
protected  java.lang.String fServiceName
          Name of service
 
Fields inherited from class org.softamis.cluster4spring.support.provider.AbstractUrlListEndpointProvider
fCacheLock, fEndpointsCache
 
Fields inherited from class org.softamis.cluster4spring.support.provider.MultiURLEndpointProvider
fEndpointSelectionPolicy
 
Fields inherited from class org.softamis.cluster4spring.support.provider.BaseEndpointProvider
fCacheEndpoints
 
Constructor Summary
protected AbstractDiscoveringEndpointProvider()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by Spring as part of bean lifecycle.
protected  void checkServiceName()
           
protected  java.lang.String createServiceKey(java.lang.String aBeanName)
          Represensts implementation of actual algorythm of service key calculation.
protected  java.util.List<E> doRefreshServiceEndpointsList(org.springframework.remoting.support.RemoteInvocationFactory aRemoteInvocationFactory, EndpointFactory<E,SI> aEndpointFactory, java.lang.String aBeanName)
          Creates list of endpoints available for service.
protected  java.util.List<E> getCachedEndpoints(org.springframework.remoting.support.RemoteInvocationFactory aRemoteInvocationFactory, EndpointFactory<E,SI> aEndpointFactory, java.lang.String aBeanName)
          Obtains endpoints from endpoints cache.
 java.lang.String getProtocolName()
          Returns name of protocol
 java.lang.String getServiceGroup()
          Returns group to which service belong
 java.lang.String getServiceName()
          Returns name of service
protected abstract  void invalidateServiceInRegistry(java.lang.String aServiceKey, SI aServiceInfo)
          Methods used to notify underlying services registry that particular service location should be invalidated.
protected abstract  boolean isRefreshRequiredForCachedEndpoints(java.lang.String aServiceKey)
          Determines whether refresh for cached endpoints is required.
 void markInvalid(java.lang.String aBeanName, E aEndpoint)
          Marks given endpoint invalid.
protected  void markServiceInvalidInternal(java.lang.String aServiceKey, java.util.Set<SI> aServiceInfos, SI aServiceInfo)
          Notifies underlying registry that given service url under given service key is invalid if service url is contained in provided set of service urls
protected  java.lang.String obtainServiceKey(java.lang.String aBeanName)
          Returns key of service in consuming registry.
protected abstract  java.util.Set<SI> obtainServiceUrlsFromRegistry(java.lang.String aServiceKey)
          Returns set of service url's from service locations registry
 void onApplicationEvent(org.springframework.context.ApplicationEvent aEvent)
          Handles application event
protected  void onContextClosed()
          Handles ContextClosed application event.
protected  void onContextRefreshed()
          Handles ContextRefreshed application event.
 void setProtocolName(java.lang.String aProtocolName)
          Sets name of protocol.
 void setServiceGroup(java.lang.String aServiceGroup)
          Sets group to which service belong.
 void setServiceName(java.lang.String aServiceName)
          Sets name of service
 
Methods inherited from class org.softamis.cluster4spring.support.provider.AbstractUrlListEndpointProvider
doGetCachedEndpoints, getServiceEndpointsList, refresh, saveFreshEndpointsToCache
 
Methods inherited from class org.softamis.cluster4spring.support.provider.MultiURLEndpointProvider
createDefaultEndpointSelectionPolicy, getEndpoint, setEndpointSelectionPolicy
 
Methods inherited from class org.softamis.cluster4spring.support.provider.BaseEndpointProvider
doCreateServiceEndpoint, isCacheEndpoints, setCacheEndpoints
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fServiceName

protected java.lang.String fServiceName
Name of service


fServiceKey

protected java.lang.String fServiceKey

fProtocolName

protected java.lang.String fProtocolName
Name of protocol


fServiceGroup

protected java.lang.String fServiceGroup
Group to which service belong

Constructor Detail

AbstractDiscoveringEndpointProvider

protected AbstractDiscoveringEndpointProvider()
Method Detail

isRefreshRequiredForCachedEndpoints

protected abstract boolean isRefreshRequiredForCachedEndpoints(java.lang.String aServiceKey)

Determines whether refresh for cached endpoints is required. Implementation will rely on underlying services registry.

Parameters:
aServiceKey - key used to check whether cahe should be refreshed
Returns:
true if endpoints cache should be refreshed.
See Also:
ConsumingRegistry.isDirty(java.io.Serializable)

obtainServiceUrlsFromRegistry

protected abstract java.util.Set<SI> obtainServiceUrlsFromRegistry(java.lang.String aServiceKey)
                                                                                   throws java.lang.Exception
Returns set of service url's from service locations registry

Parameters:
aServiceKey - key used to obtain service urls
Returns:
found service urls
Throws:
java.lang.Exception

invalidateServiceInRegistry

protected abstract void invalidateServiceInRegistry(java.lang.String aServiceKey,
                                                    SI aServiceInfo)
Methods used to notify underlying services registry that particular service location should be invalidated.

Parameters:
aServiceKey - key used to obtain service urls
aServiceInfo - information about particular service location

afterPropertiesSet

public void afterPropertiesSet()
                        throws java.lang.Exception
Invoked by Spring as part of bean lifecycle. Adds checking for protocol name, consuming registry, etc.

Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Overrides:
afterPropertiesSet in class AbstractUrlListEndpointProvider<E extends Endpoint<SI>,SI extends ServiceMoniker>
Throws:
java.lang.Exception
See Also:
MultiURLEndpointProvider.createDefaultEndpointSelectionPolicy()

getCachedEndpoints

protected java.util.List<E> getCachedEndpoints(org.springframework.remoting.support.RemoteInvocationFactory aRemoteInvocationFactory,
                                               EndpointFactory<E,SI> aEndpointFactory,
                                               java.lang.String aBeanName)
Obtains endpoints from endpoints cache. First, tries to determine whether cache should be refreshed. If one should, refreshes cache. After this returns cached endpoints.

Overrides:
getCachedEndpoints in class AbstractUrlListEndpointProvider<E extends Endpoint<SI>,SI extends ServiceMoniker>
Parameters:
aRemoteInvocationFactory - factory to create remote invocations
aEndpointFactory - factory used to create endpoints
aBeanName -
Returns:
list of endpoints
See Also:
isRefreshRequiredForCachedEndpoints(String)

doRefreshServiceEndpointsList

protected java.util.List<E> doRefreshServiceEndpointsList(org.springframework.remoting.support.RemoteInvocationFactory aRemoteInvocationFactory,
                                                          EndpointFactory<E,SI> aEndpointFactory,
                                                          java.lang.String aBeanName)
                                                                        throws org.springframework.remoting.RemoteAccessException
Creates list of endpoints available for service. Method obtains list of urls that corresponds to required service key from the ConsumingRegistry and based on this list tries to create list of endpoints.

If endpoint creation is failed for some service url, method notes such url and after endpoints creation notifies ConsumingRegistry that particular service url is invalid.

Specified by:
doRefreshServiceEndpointsList in class AbstractUrlListEndpointProvider<E extends Endpoint<SI>,SI extends ServiceMoniker>
Parameters:
aRemoteInvocationFactory - factory used to create remote invocation
aEndpointFactory - factory used to create endpoints
aBeanName -
Returns:
list of created endpoints
Throws:
org.springframework.remoting.RemoteAccessException - throws if list of endpoints could not be created

markInvalid

public void markInvalid(java.lang.String aBeanName,
                        E aEndpoint)
Marks given endpoint invalid. This endpoint will not be later used for methods invocation. Method removes endpoint from cache (if configured to cache endpoints) and later marks particular service url invalid via ConsumingRegistry.

Specified by:
markInvalid in interface EndpointProvider<E extends Endpoint<SI>,SI extends ServiceMoniker>
Parameters:
aBeanName - name of bean that is used as proxy for remote service
aEndpoint - endpoint to be marked invalid
See Also:
BaseEndpointProvider.setCacheEndpoints(boolean)

onApplicationEvent

public void onApplicationEvent(org.springframework.context.ApplicationEvent aEvent)
Handles application event

Specified by:
onApplicationEvent in interface org.springframework.context.ApplicationListener
Parameters:
aEvent - event to handle
See Also:
onContextRefreshed(), onContextClosed()

onContextRefreshed

protected void onContextRefreshed()
Handles ContextRefreshed application event.

See Also:
onApplicationEvent(ApplicationEvent)

onContextClosed

protected void onContextClosed()
Handles ContextClosed application event. On closing context clears endpoints cache

See Also:
onApplicationEvent(ApplicationEvent)

obtainServiceKey

protected java.lang.String obtainServiceKey(java.lang.String aBeanName)
Returns key of service in consuming registry. This key is used to obtain information about urls for that service.

Key is composed in form serviceGroup/serviceName. If service name is not specified during provider configuration, given name of proxy bean is used instead of service name.

Parameters:
aBeanName - name of proxy bean
Returns:
service key used to obtain services from ConsumingRegistry

createServiceKey

protected java.lang.String createServiceKey(java.lang.String aBeanName)
Represensts implementation of actual algorythm of service key calculation. Currntly, it simply creates key in the following form: serviceGroup/serviceName This method should be overriden if some custom implementation is necesary.

Parameters:
aBeanName -
Returns:
key for service

markServiceInvalidInternal

protected void markServiceInvalidInternal(java.lang.String aServiceKey,
                                          java.util.Set<SI> aServiceInfos,
                                          SI aServiceInfo)
Notifies underlying registry that given service url under given service key is invalid if service url is contained in provided set of service urls

Parameters:
aServiceKey - key used to identify services url in ConsumingRegistry
aServiceInfos - list of services urls
aServiceInfo - service url that should be marked invalid

checkServiceName

protected void checkServiceName()

getServiceGroup

public java.lang.String getServiceGroup()
Returns group to which service belong

Returns:
group to which service belong

setServiceGroup

public void setServiceGroup(java.lang.String aServiceGroup)
Sets group to which service belong. If group name is not specified explicitely, it will be created by default. Name of group on client should correspond to name of group on server.

Parameters:
aServiceGroup - group to which service belong

getServiceName

public java.lang.String getServiceName()
Returns name of service

Returns:
name of service

setServiceName

public void setServiceName(java.lang.String aServiceName)
Sets name of service

Parameters:
aServiceName - name of service

getProtocolName

public java.lang.String getProtocolName()
Returns name of protocol

Returns:
name of protocol

setProtocolName

public void setProtocolName(java.lang.String aProtocolName)
Sets name of protocol. Used for logging and to form default name of group if service group is not specified explicitely

Parameters:
aProtocolName - name of protocol