org.robokind.api.common.lifecycle
Class AbstractLifecycleProvider<I,T extends I>

java.lang.Object
  extended by org.robokind.api.common.property.PropertyChangeNotifier
      extended by org.robokind.api.common.lifecycle.AbstractLifecycleProvider<I,T>
Type Parameters:
I - Interface or Base Class of the service managed by this lifecycle
T - actual type of the service managed
All Implemented Interfaces:
ServiceLifecycleProvider<I>, PropertyChangeSource
Direct Known Subclasses:
ConfiguredServiceLifecycle, GenericLifecycle

public abstract class AbstractLifecycleProvider<I,T extends I>
extends PropertyChangeNotifier
implements ServiceLifecycleProvider<I>

Abstract implementation of ServiceLifecycleProvider. Automatically validates dependencies against the descriptors provided at creation.

Author:
Matthew Stevenson

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.robokind.api.common.lifecycle.ServiceLifecycleProvider
ServiceLifecycleProvider.Validator
 
Field Summary
protected  Properties myRegistrationProperties
          OSGi registration properties
protected  T myService
          Instance of the service being managed
protected  String[] myServiceClassNames
          Names of the classes provided by this service.
 
Fields inherited from interface org.robokind.api.common.lifecycle.ServiceLifecycleProvider
PROP_DEPENDENCY_ADDED, PROP_DEPENDENCY_REMOVED
 
Constructor Summary
AbstractLifecycleProvider(List<DependencyDescriptor> deps)
          Creates a new AbstractLifecycleProvider with the given DependencyDescriptors.
 
Method Summary
protected  boolean addRuntimeDependency(DependencyDescriptor desc)
          Adds a dependency after the lifecycle is initialized.
protected abstract  T create(Map<String,Object> dependencies)
          Called when all dependencies are available.
 void dependencyChanged(String dependencyName, Object dependency, Map<String,Object> dependencies)
          Validates the arguments before calling handleChange.
 List<DependencyDescriptor> getDependencyDescriptors()
          Returns a list describing the service dependencies required by this lifecycle provider.
 Properties getRegistrationProperties()
          Returns the properties to be used when register the service managed by this lifecycle provider.
 T getService()
          Returns the service managed by this lifecycle provider.
protected abstract  Class<I> getServiceClass()
          Returns the Class of the service managed by this lifecycle provider.
 String[] getServiceClassNames()
          Returns the names of the interfaces this service implements and makes available.
protected abstract  void handleChange(String name, Object dependency, Map<String,Object> availableDependencies)
          Called from dependencyChanged with validated values.
 boolean isSatisfied()
           
protected  boolean removeRuntimeDependency(String depName)
          Removes a dependency after the lifecycle is initialized.
 void start(Map<String,Object> dependencies)
          Called the first time all dependencies are available.
 void stop()
           
 
Methods inherited from class org.robokind.api.common.property.PropertyChangeNotifier
addPropertyChangeListener, addPropertyChangeListener, clearAllListeners, clearListeners, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getPropertyChangeListeners, getPropertyChangeListeners, hasListeners, removePropertyChangeListener, removePropertyChangeListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.robokind.api.common.property.PropertyChangeSource
addPropertyChangeListener, addPropertyChangeListener, removePropertyChangeListener, removePropertyChangeListener
 

Field Detail

myService

protected T extends I myService
Instance of the service being managed


myRegistrationProperties

protected Properties myRegistrationProperties
OSGi registration properties


myServiceClassNames

protected String[] myServiceClassNames
Names of the classes provided by this service.

Constructor Detail

AbstractLifecycleProvider

public AbstractLifecycleProvider(List<DependencyDescriptor> deps)
Creates a new AbstractLifecycleProvider with the given DependencyDescriptors.

Parameters:
deps - descriptors of the dependencies for this lifecycle
Method Detail

start

public void start(Map<String,Object> dependencies)
Description copied from interface: ServiceLifecycleProvider
Called the first time all dependencies are available.

Specified by:
start in interface ServiceLifecycleProvider<I>
Parameters:
dependencies - dependencies needed to start the service lifecycle

stop

public void stop()
Specified by:
stop in interface ServiceLifecycleProvider<I>

create

protected abstract T create(Map<String,Object> dependencies)
Called when all dependencies are available. This should create and return the service for this lifecycle.

Parameters:
dependencies - validated map of the lifecycle's dependencies
Returns:
the service managed by this lifecycle, null if the service cannot be created

getService

public T getService()
Description copied from interface: ServiceLifecycleProvider
Returns the service managed by this lifecycle provider. Returns null if the lifecycle has not been started or if the service is unavailable.

Specified by:
getService in interface ServiceLifecycleProvider<I>
Returns:
service the service managed by this lifecycle provider

getRegistrationProperties

public Properties getRegistrationProperties()
Description copied from interface: ServiceLifecycleProvider
Returns the properties to be used when register the service managed by this lifecycle provider. This is intended to be used as the registration properties for the OSGi Service Registry.

Specified by:
getRegistrationProperties in interface ServiceLifecycleProvider<I>
Returns:
properties to be used when register the service managed by this lifecycle provider

getDependencyDescriptors

public List<DependencyDescriptor> getDependencyDescriptors()
Description copied from interface: ServiceLifecycleProvider
Returns a list describing the service dependencies required by this lifecycle provider.

Specified by:
getDependencyDescriptors in interface ServiceLifecycleProvider<I>
Returns:
list describing the service dependencies required by this lifecycle provider

isSatisfied

public boolean isSatisfied()

dependencyChanged

public void dependencyChanged(String dependencyName,
                              Object dependency,
                              Map<String,Object> dependencies)
Validates the arguments before calling handleChange. If the service is null and the dependencies are satisfied, a service is created and handleChange is not called.

Specified by:
dependencyChanged in interface ServiceLifecycleProvider<I>
Parameters:
dependencyName - name of the dependency changed
dependency - new dependency
dependencies - all available dependencies

handleChange

protected abstract void handleChange(String name,
                                     Object dependency,
                                     Map<String,Object> availableDependencies)
Called from dependencyChanged with validated values.

Parameters:
name - name of the dependency changed
dependency - new dependency value, null if it was removed
availableDependencies - a map of all available dependencies and their names

getServiceClass

protected abstract Class<I> getServiceClass()
Returns the Class of the service managed by this lifecycle provider.

Returns:
Class of the service managed by this lifecycle provider

getServiceClassNames

public String[] getServiceClassNames()
Description copied from interface: ServiceLifecycleProvider
Returns the names of the interfaces this service implements and makes available.

Specified by:
getServiceClassNames in interface ServiceLifecycleProvider<I>
Returns:
names of the interfaces this service implements and makes available

addRuntimeDependency

protected boolean addRuntimeDependency(DependencyDescriptor desc)
Adds a dependency after the lifecycle is initialized.

Parameters:
desc - dependency to add
Returns:
true if successful, false if the dependency name is in use

removeRuntimeDependency

protected boolean removeRuntimeDependency(String depName)
Removes a dependency after the lifecycle is initialized. If the dependency was not added after initialization, it will not be removed.

Parameters:
desc - dependency to remove
Returns:
true if successful, false if the dependency name is not found, or does not belong to a runtime dependency.


Copyright © 2011-2013. All Rights Reserved.