org.softamis.cluster4spring.support.context
Class AbstractServicePublisher<EC extends AutoDiscoveredServiceExporter>

java.lang.Object
  extended by org.softamis.cluster4spring.support.context.AbstractServicePublisher<EC>
Type Parameters:
EC - type of service exporter which is supported by publisher
All Implemented Interfaces:
java.util.EventListener, ServicePublisher, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener
Direct Known Subclasses:
GenericServicePublisher

public abstract class AbstractServicePublisher<EC extends AutoDiscoveredServiceExporter>
extends java.lang.Object
implements ServicePublisher, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationListener, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.BeanNameAware

Abstract base implementation of service publisher. During refreshing application context, publisher collects all service exporters with needed class and if they are applicable for service publishing, it publishes information about these services in distributed registry. Therefore, clients that requires autodiscoverable services may obtain information about service urls and so use them.

To determine whether particular service exporter should be published by this publisher, delegates actual logic to associated ExporterAcceptor.

On closing service context, publisher notifies distributed registry for unregistration of all service urls previosly published by publisher.

While there is ready to use implementation of this class wich uses distributed registry is included into part of the library, it's possible to use other implementation of distributed services storage. If it's desired to use another storage (say, JBoss Cache), this class should be considered as base one for implementing such integration.

Author:
Andrew Sazonov
See Also:
ExporterAcceptor

Field Summary
protected  java.lang.Class<EC> fAutodiscoveredExporterClass
          Class of exporter should be published
protected  java.util.List<ServiceInfo> fAutodiscoveredServicesInfo
          Cache of discovered services
protected  java.lang.String fBeanName
          Name of bean - used in logging
protected  boolean fCacheAutoDiscoveredServicesInfo
          Indicates whether services discovered for publishing during context refreshing should be cached until context closing
protected  org.springframework.context.ApplicationContext fContext
          Application context
protected  ExporterAcceptor<EC,ServicePublisher> fExporterAcceptor
          ExporterAcceptor used to determine whether service exported by particular exporter could be published
protected  java.lang.String fProtocolName
          Name of protocol
protected  long fServerID
          If there are several servers of the same type exists withing the same cluster, it is necessary to distinguish them.
protected  java.lang.String fServerType
          Type of server.
protected  java.lang.String fServiceGroup
          Services group
 
Fields inherited from interface org.softamis.cluster4spring.ServicePublisher
SERVER_TYPE_DELIMITER
 
Constructor Summary
protected AbstractServicePublisher()
          Creates publisher
 
Method Summary
 void afterPropertiesSet()
          Called by Spring as part of bean lifecycle.
protected  java.util.List<ServiceInfo> collectAutoDiscoveredServices()
          Collects information about services that could be published.
protected  void contextClosed()
          Handles closing application context.
protected  void contextRefreshed()
          Handles refreshing of application context.
protected  ExporterAcceptor<EC,ServicePublisher> createDefaultExporterAcceptor()
          Creates ExporterAcceptor that will be used if ones is not specified explicitely.
protected  java.util.List<ServiceInfo> createServiceInfos(java.lang.String aBeanName, EC aExporter)
          Creates information about service based on given service exporter.
protected  ServiceMoniker createServiceMoniker(ServiceInfo aServiceInfo)
          Creates ServiceMoniker by given discovered ServiceInfo
protected  java.lang.String createServicesGroup()
          Creates default name for services group if one is not specified explicitely.
protected  java.util.Map<java.lang.String,EC> doGetAutoDiscoveredServices(java.lang.Class aExporterClass)
          Collects exporters that potentially published from application context Current implementation simply selects all beans from Spring Context with provided class
protected abstract  void doRegisterServiceInServicesRegistry(java.lang.String aServiceKey, ServiceMoniker aMoniker)
          Notifies underlying services registry that information about service should be published
protected abstract  void doUnregisterServiceInServicesRegistry(java.lang.String aServiceKey)
          Notifies that service under given key should be unregistered in underlying services registry
protected  java.lang.Class<EC> getAutodiscoveredExporterClass()
          Return class of servicie exporter that is used to select information about services which should be published by this publisher
protected  java.lang.String getProtocolName()
          Returns name of protocol used exporters.
protected  java.lang.String getPublisherName()
          Return name of the bean in bean factory
 long getServerID()
          If there are several servers of the same type exists withing the same cluster, it is necessary to distinguish them.
 java.lang.String getServerType()
          Returns type of server.
 java.lang.String getServiceGroup()
          Returns services group
protected  java.lang.String getServiceKey(ServiceInfo aServiceInfo)
          Creates key for published service.
 boolean isCacheAutoDiscoveredServicesInfo()
          Specifies whether information about published services should be cached until context closing
 void onApplicationEvent(org.springframework.context.ApplicationEvent aEvent)
          Handles Spring application event.
protected  java.lang.String prepareServiceURL(java.lang.String aServiceUrl)
          Prepared URL for service.
protected  void publishService(ServiceInfo aServiceInfo)
          Performs publishing of service denoted by given parameter by notifying underlying services registry.
 void publishServices()
          Collects and publishes all applicable services for auto-discovering.
 void setApplicationContext(org.springframework.context.ApplicationContext aContext)
           
 void setBeanName(java.lang.String aName)
          Set the aName of the bean in the bean factory that created this bean.
 void setCacheAutoDiscoveredServicesInfo(boolean aCache)
          Specifies whether information about published services should be cached until context closing.
 void setExporterAcceptor(ExporterAcceptor<EC,ServicePublisher> aExporterAcceptor)
          Sets exporterAcceptor used to determine whether service exported by particular exporter could be published
 void setServerID(long aServerID)
          Setter for ServerID property
 void setServerType(java.lang.String aServerType)
          Setter for server types.
 void setServiceGroup(java.lang.String aServiceGroup)
          Sets services group.
protected  void unPublishService(ServiceInfo serviceInfo)
          Performs un-publishing of service denoted by given parameter by notifying underlying services registry.
 void unPublishServices()
          Removes information about own published services from auto-discovering context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fContext

protected org.springframework.context.ApplicationContext fContext
Application context


fServiceGroup

protected java.lang.String fServiceGroup
Services group


fCacheAutoDiscoveredServicesInfo

protected boolean fCacheAutoDiscoveredServicesInfo
Indicates whether services discovered for publishing during context refreshing should be cached until context closing


fAutodiscoveredServicesInfo

protected java.util.List<ServiceInfo> fAutodiscoveredServicesInfo
Cache of discovered services


fExporterAcceptor

protected ExporterAcceptor<EC extends AutoDiscoveredServiceExporter,ServicePublisher> fExporterAcceptor
ExporterAcceptor used to determine whether service exported by particular exporter could be published


fBeanName

protected java.lang.String fBeanName
Name of bean - used in logging


fServerType

protected java.lang.String fServerType
Type of server. If cluster represents group of servers that includes servers with different types, this field allows to define to which kind of servers particular one represents (just as example - "pdf generation server", "web server", "image processor" etc).


fServerID

protected long fServerID
If there are several servers of the same type exists withing the same cluster, it is necessary to distinguish them. Server ID represents unques ID of server within a cluster.


fAutodiscoveredExporterClass

protected java.lang.Class<EC extends AutoDiscoveredServiceExporter> fAutodiscoveredExporterClass
Class of exporter should be published


fProtocolName

protected java.lang.String fProtocolName
Name of protocol

Constructor Detail

AbstractServicePublisher

protected AbstractServicePublisher()
Creates publisher

Method Detail

setBeanName

public void setBeanName(java.lang.String aName)
Set the aName of the bean in the bean factory that created this bean.

Specified by:
setBeanName in interface org.springframework.beans.factory.BeanNameAware
Parameters:
aName - aName of the bean

getPublisherName

protected java.lang.String getPublisherName()
Return name of the bean in bean factory

Returns:
bean name

onApplicationEvent

public void onApplicationEvent(org.springframework.context.ApplicationEvent aEvent)
Handles Spring application event. During refreshing application context, publisher collects all service exporters with needed class and if they are applicable for service publishing, it publishes information about these services in distributed registry. Therefore, clients that requires autodiscoverable services may obtain information about service urls and so use them.

On closing service context, publisher notifies distributed registry for unresistration of all service urls previosly published by publisher.

Specified by:
onApplicationEvent in interface org.springframework.context.ApplicationListener
Parameters:
aEvent - event from Spring
See Also:
contextRefreshed(), contextClosed()

collectAutoDiscoveredServices

protected java.util.List<ServiceInfo> collectAutoDiscoveredServices()
Collects information about services that could be published. If there is no services information in the cache, method tries to discover it.

First, method selects service exporters with applicable type. Then, for every publisher found it performs check whether it could be published by given publisher (using ExporterAcceptor) and if service could be published, appropriate service information is created based on exporter data.

Returns:
list of service items
See Also:
createServiceInfos(String,AutoDiscoveredServiceExporter), ExporterAcceptor, getAutodiscoveredExporterClass()

doGetAutoDiscoveredServices

protected java.util.Map<java.lang.String,EC> doGetAutoDiscoveredServices(java.lang.Class aExporterClass)
                                                                                                        throws org.springframework.beans.BeansException
Collects exporters that potentially published from application context Current implementation simply selects all beans from Spring Context with provided class

Parameters:
aExporterClass - class of exporter
Returns:
list of exporters
Throws:
org.springframework.beans.BeansException - thrown by Spring ApplicationContext during obtainig list of beans

contextClosed

protected void contextClosed()
                      throws java.lang.Exception
Handles closing application context. Performs unpublishing of services.

Throws:
java.lang.Exception - throw if exception occured during closing context
See Also:
onApplicationEvent(org.springframework.context.ApplicationEvent)

contextRefreshed

protected void contextRefreshed()
                         throws java.lang.Exception
Handles refreshing of application context. Performs publishing of services.

Throws:
java.lang.Exception
See Also:
onApplicationEvent(org.springframework.context.ApplicationEvent)

getServiceKey

protected java.lang.String getServiceKey(ServiceInfo aServiceInfo)
Creates key for published service. Using this key, information about server will be stored in distributed registry and later will be obtained by DiscoveringServiceProvider. Current implementation created key in form
SERVICE_GROUP/SERVICE_NAME
.

Parameters:
aServiceInfo - information about exported service.
Returns:
service key

unPublishServices

public void unPublishServices()
Removes information about own published services from auto-discovering context. Simply walks on all collected services and notifies distributed registry that service url should be unregistered.

Specified by:
unPublishServices in interface ServicePublisher
See Also:
collectAutoDiscoveredServices(), getServiceKey(ServiceInfo)

publishServices

public void publishServices()
Collects and publishes all applicable services for auto-discovering. Simply walks on all collected services and notifies distributed registry that service url should be registered.

NOTE: If service group is specified for found exporter, it will be used as for creation of ServiceInfo. If service group is not specified for exporter, during creation of ServiceInfo group from publisher will be used.

Specified by:
publishServices in interface ServicePublisher
See Also:
collectAutoDiscoveredServices(), getServiceKey(ServiceInfo)

unPublishService

protected void unPublishService(ServiceInfo serviceInfo)
Performs un-publishing of service denoted by given parameter by notifying underlying services registry.

Parameters:
serviceInfo - key used to denote service which should be unpublished

publishService

protected void publishService(ServiceInfo aServiceInfo)
Performs publishing of service denoted by given parameter by notifying underlying services registry.

Parameters:
aServiceInfo - key used to denote service which should be published

createServiceMoniker

protected ServiceMoniker createServiceMoniker(ServiceInfo aServiceInfo)
Creates ServiceMoniker by given discovered ServiceInfo

Parameters:
aServiceInfo - collected information about service
Returns:
created ServiceMoniker

afterPropertiesSet

public void afterPropertiesSet()
                        throws java.lang.Exception
Called by Spring as part of bean lifecycle. Performs checking that service group is specified externally (or creates default group name). Also checks whether ExporterAcceptor is specified and if necessary creates default one.

Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
java.lang.Exception
See Also:
createServicesGroup(), createDefaultExporterAcceptor()

createDefaultExporterAcceptor

protected ExporterAcceptor<EC,ServicePublisher> createDefaultExporterAcceptor()
Creates ExporterAcceptor that will be used if ones is not specified explicitely.

Returns:
created ExporterAcceptor
See Also:
DefaultExporterAcceptor

createServicesGroup

protected java.lang.String createServicesGroup()
Creates default name for services group if one is not specified explicitely. By default, format of service group is AutoDiscoveredServiceExporter.DEFAULT_SERVICES_GROUP_PREFIX + getProtocolName()

Returns:
service group

setApplicationContext

public void setApplicationContext(org.springframework.context.ApplicationContext aContext)
                           throws org.springframework.beans.BeansException
Specified by:
setApplicationContext in interface org.springframework.context.ApplicationContextAware
Throws:
org.springframework.beans.BeansException

getServiceGroup

public java.lang.String getServiceGroup()
Returns services group

Specified by:
getServiceGroup in interface ServicePublisher
Returns:
services group
See Also:
GroupBasedExporterAcceptor

setServiceGroup

public void setServiceGroup(java.lang.String aServiceGroup)
Sets services group. If services group is not specified, default services group will be created.

Parameters:
aServiceGroup - services group

isCacheAutoDiscoveredServicesInfo

public boolean isCacheAutoDiscoveredServicesInfo()
Specifies whether information about published services should be cached until context closing

Returns:
true if caching is required

setCacheAutoDiscoveredServicesInfo

public void setCacheAutoDiscoveredServicesInfo(boolean aCache)
Specifies whether information about published services should be cached until context closing. Default value: false

Parameters:
aCache - true if autodiscovered services should be cached until unpublishing

setExporterAcceptor

public void setExporterAcceptor(ExporterAcceptor<EC,ServicePublisher> aExporterAcceptor)
Sets exporterAcceptor used to determine whether service exported by particular exporter could be published

Parameters:
aExporterAcceptor - exporterAcceptor

getServerType

public java.lang.String getServerType()
Returns type of server. If cluster contains servers of different types, it's necessary to define which type of server particular service belongs to. In general, all services within the same server will have the same server type.

Returns:
type of server where service recide

setServerType

public void setServerType(java.lang.String aServerType)
Setter for server types. If cluster contains servers of different types, it's necessary to define which type of server particular service belongs to. In general, all services within the same server will have the same server type.

Parameters:
aServerType - type of server

getServerID

public long getServerID()
If there are several servers of the same type exists withing the same cluster, it is necessary to distinguish them. Server ID represents unques ID of server within a cluster. Typically all services published from the same server will have the same server ID.

Returns:
server ID

setServerID

public void setServerID(long aServerID)
Setter for ServerID property

Parameters:
aServerID -

createServiceInfos

protected java.util.List<ServiceInfo> createServiceInfos(java.lang.String aBeanName,
                                                         EC aExporter)
Creates information about service based on given service exporter. This information will later be used to publish service url.

Parameters:
aBeanName - name of bean
aExporter - exporter
Returns:
service information

prepareServiceURL

protected java.lang.String prepareServiceURL(java.lang.String aServiceUrl)
Prepared URL for service. Potentially may be used in inherited classess for additional processing of service URL.

Parameters:
aServiceUrl - url of service
Returns:
resulting URL that will be stored in ServiceMoniker

getProtocolName

protected java.lang.String getProtocolName()
Returns name of protocol used exporters. Used for creation of default name of servicesGroup and for logging.

Returns:
name of protocol

getAutodiscoveredExporterClass

protected java.lang.Class<EC> getAutodiscoveredExporterClass()
Return class of servicie exporter that is used to select information about services which should be published by this publisher

Returns:
class of exporter

doUnregisterServiceInServicesRegistry

protected abstract void doUnregisterServiceInServicesRegistry(java.lang.String aServiceKey)
Notifies that service under given key should be unregistered in underlying services registry

Parameters:
aServiceKey - key for service that should be unregistered

doRegisterServiceInServicesRegistry

protected abstract void doRegisterServiceInServicesRegistry(java.lang.String aServiceKey,
                                                            ServiceMoniker aMoniker)
Notifies underlying services registry that information about service should be published

Parameters:
aServiceKey - key for service
aMoniker - service information