Package org.apache.felix.dm.context
Class AbstractDependency<T extends Dependency>
java.lang.Object
org.apache.felix.dm.context.AbstractDependency<T>
- Type Parameters:
T- The type of the interface representing a Dependency Manager Dependency (must extends the Dependency interface).
- All Implemented Interfaces:
ComponentDependencyDeclaration,DependencyContext,Dependency
public abstract class AbstractDependency<T extends Dependency>
extends Object
implements Dependency, DependencyContext, ComponentDependencyDeclaration
Abstract class for implementing Dependencies.
You can extends this class in order to supply your own custom dependencies to any Dependency Manager Component.
- Author:
- Felix Project Team
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Dictionary<Object, Object> Default empty dependency properties.protected StringComponent callback used to inject an added dependency.protected booleanCan this Dependency be auto configured in the component instance fields ?protected StringThe Component field name where the Dependency can be injected (null means any field with a compatible type will be injected).protected booleanIndicates if the setAutoConfig method has been invoked.protected booleanIs this Dependency available ? Volatile because the getState method (part of theComponentDependencyDeclarationinterface) may be called by any thread, at any time.protected ObjectThe object instance on which the dependency callbacks are invoked on.protected StringComponent callback invoked when the dependency has changed.protected ComponentContextThe Component implementation is exposed to Dependencies through this interface.protected booleanIs this Dependency "instance bound" ? A dependency is "instance bound" if it is defined within the component's init method, meaning that it won't deactivate the component if it is not currently available when being added from the component's init method.protected booleanHas this Dependency been started by the Component implementation ? Volatile because the getState method (part of theComponentDependencyDeclarationinterface) may be called by any thread, at any time.protected booleanTells if the dependency service properties have to be propagated to the Component service properties.protected ObjectThe propagate callback instance that is invoked in order to supply dynamically some dependency service properties.protected StringThe propagate callback method that is invoked in order to supply dynamically some dependency service properties.protected booleanTells if the dependency service properties should override default component service properties (false by default).protected StringComponent callback invoked when the dependency becomes unavailable.protected booleanIs this dependency required (false by default) ?Fields inherited from interface org.apache.felix.dm.ComponentDependencyDeclaration
STATE_AVAILABLE_OPTIONAL, STATE_AVAILABLE_REQUIRED, STATE_NAMES, STATE_OPTIONAL, STATE_REQUIRED, STATE_UNAVAILABLE_OPTIONAL, STATE_UNAVAILABLE_REQUIRED -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new Dependency.AbstractDependency(AbstractDependency<T> prototype) Create a clone of a given Dependency. -
Method Summary
Modifier and TypeMethodDescriptionvoidcopyToCollection(Collection<Object> services) Copy all dependency service instances to the given collection.voidcopyToMap(Map<Object, Dictionary<?, ?>> map) Copy all dependency service instances to the given map (key = dependency service, value = dependency service properties.abstract DependencyContextCreates a copy of this Dependency.protected voidChecks if the component dependency is not started.Returns the field name when the dependency can be injected to.abstract Class<?> Returns the type of the field where this dependency can be injected (auto config), or return null if autoconfig is not supported.Returns the component implementation contextprotected ObjectgetDefaultService(boolean nullObject) Returns the default service, or null.Returns the dependency filter, if any.Object[]Returns the dependency callback instancesgetName()Returns a description of this dependency (like the dependency service class name with associated filters)<K,V> Dictionary <K, V> Returns the dependency service properties (empty by default).Get the highest ranked available dependency service, or null.abstract StringReturns a simple name for this dependency (like the dependency service class name).intgetState()Returns this dependency state.abstract StringgetType()Returns the dependency symbolic type.voidinvokeCallback(EventType type, Event... events) A Component callback must be invoked with dependency event(s).booleanCan this dependency be injected in a component class field (by reflexion, true by default) ?booleanIs this Dependency satisfied and available ?booleanIs this Dependency "instance bound" (has been defined within the component's init method) ?booleanReturns the propagate callback method that is invoked in order to supply dynamically some dependency service properties.booleanIs this Dependency required (false by default) ?booleanIndicates if this dependency has been started by the Component implementation.booleanTells if the Component must be first instantiated before starting this dependency (false by default).booleanDetermines if the propagated dependency properties must override the component service properties.setAutoConfig(boolean autoConfig) setAutoConfig(String instanceName) voidsetAvailable(boolean available) Called by the Component implementation when the dependency is considered to be available.setCallbacks(Object instance, String add, String remove) Sets the callbacks for this service.setCallbacks(Object instance, String add, String change, String remove) Sets the callbacks for this service.setCallbacks(String add, String remove) Sets the add/remove callbacks.setCallbacks(String add, String change, String remove) Sets the add/change/remove callbacks.voidsetComponentContext(ComponentContext component) Called by the Component implementation before the Dependency can be started.voidsetInstanceBound(boolean instanceBound) Called by the Component implementation when the dependency is declared within the Component's init method.setPropagate(boolean propagate) Activates Dependency service properties propagation (to the service properties of the component to which this dependency is added).setPropagate(boolean propagate, boolean overrideServiceProperties) setPropagate(Object instance, String method) Sets a callback instance which can ba invoked with the given method in order to dynamically retrieve the dependency service properties.setRequired(boolean required) voidstart()Starts this dependency.voidstop()Starts this dependency.toString()
-
Field Details
-
m_component
The Component implementation is exposed to Dependencies through this interface. -
m_available
protected volatile boolean m_availableIs this Dependency available ? Volatile because the getState method (part of theComponentDependencyDeclarationinterface) may be called by any thread, at any time. -
m_instanceBound
protected boolean m_instanceBoundIs this Dependency "instance bound" ? A dependency is "instance bound" if it is defined within the component's init method, meaning that it won't deactivate the component if it is not currently available when being added from the component's init method. -
m_required
protected volatile boolean m_requiredIs this dependency required (false by default) ? -
m_add
Component callback used to inject an added dependency. -
m_change
Component callback invoked when the dependency has changed. -
m_remove
Component callback invoked when the dependency becomes unavailable. -
m_autoConfig
protected volatile boolean m_autoConfigCan this Dependency be auto configured in the component instance fields ? -
m_autoConfigInstance
The Component field name where the Dependency can be injected (null means any field with a compatible type will be injected). -
m_autoConfigInvoked
protected volatile boolean m_autoConfigInvokedIndicates if the setAutoConfig method has been invoked. This flag is used to force autoconfig to "false" when the setCallbacks method is invoked, unless the setAutoConfig method has been called. -
m_isStarted
protected volatile boolean m_isStartedHas this Dependency been started by the Component implementation ? Volatile because the getState method (part of theComponentDependencyDeclarationinterface) may be called by any thread, at any time. -
m_callbackInstance
The object instance on which the dependency callbacks are invoked on. Null means the dependency will be injected to the Component implementation instance(s). -
m_propagate
protected volatile boolean m_propagateTells if the dependency service properties have to be propagated to the Component service properties. -
m_propagateOverrides
protected volatile boolean m_propagateOverridesTells if the dependency service properties should override default component service properties (false by default). -
m_propagateCallbackInstance
The propagate callback instance that is invoked in order to supply dynamically some dependency service properties. -
m_propagateCallbackMethod
The propagate callback method that is invoked in order to supply dynamically some dependency service properties. -
EMPTY_PROPERTIES
Default empty dependency properties.
-
-
Constructor Details
-
AbstractDependency
public AbstractDependency()Creates a new Dependency. By default, the dependency is optional and autoconfig. -
AbstractDependency
Create a clone of a given Dependency.- Parameters:
prototype- all the fields of the prototype will be copied to this dependency.
-
-
Method Details
-
toString
-
isRequired
public boolean isRequired()Is this Dependency required (false by default) ?- Specified by:
isRequiredin interfaceDependency- Returns:
trueif the dependency is required
-
isAvailable
public boolean isAvailable()Is this Dependency satisfied and available ?- Specified by:
isAvailablein interfaceDependency- Returns:
trueif the dependency is available
-
isAutoConfig
public boolean isAutoConfig()Can this dependency be injected in a component class field (by reflexion, true by default) ?- Specified by:
isAutoConfigin interfaceDependency- Returns:
- true if auto configuration is enabled for this dependency
-
getAutoConfigName
Returns the field name when the dependency can be injected to.- Specified by:
getAutoConfigNamein interfaceDependency- Returns:
- the name of the member in the class of the component instance to inject into
-
isPropagated
public boolean isPropagated()Returns the propagate callback method that is invoked in order to supply dynamically some dependency service properties.- Specified by:
isPropagatedin interfaceDependency- Returns:
trueif the properties should be propagated- See Also:
-
overrideServiceProperties
public boolean overrideServiceProperties()Description copied from interface:DependencyDetermines if the propagated dependency properties must override the component service properties. By default, propagated service properties don't override the component service properties. It means that a given dependency property does not override the same property in the component service properties.- Specified by:
overrideServicePropertiesin interfaceDependency- Returns:
- true if the dependency service properties are propagated and override the component service properties.
-
getProperties
Returns the dependency service properties (empty by default).- Specified by:
getPropertiesin interfaceDependency- Returns:
- the properties
- See Also:
-
setComponentContext
Called by the Component implementation before the Dependency can be started.- Specified by:
setComponentContextin interfaceDependencyContext- Parameters:
component- the Component implementation context
-
invokeCallback
A Component callback must be invoked with dependency event(s).- Specified by:
invokeCallbackin interfaceDependencyContext- Parameters:
type- the dependency event typeevents- the dependency service event to inject in the component. The number of events depends on the dependency event type: ADDED/CHANGED/REMOVED types only has one event parameter, but the SWAPPED type has two event parameters: the first one is the old event which must be replaced by the second one.- See Also:
-
start
public void start()Starts this dependency. Subclasses can override this method but must then call super.start().- Specified by:
startin interfaceDependencyContext
-
stop
public void stop()Starts this dependency. Subclasses can override this method but must then call super.stop().- Specified by:
stopin interfaceDependencyContext
-
isStarted
public boolean isStarted()Indicates if this dependency has been started by the Component implementation.- Specified by:
isStartedin interfaceDependencyContext- Returns:
- true if the dependency has been started, false if not
-
setAvailable
public void setAvailable(boolean available) Called by the Component implementation when the dependency is considered to be available.- Specified by:
setAvailablein interfaceDependencyContext- Parameters:
available- true to mark this dependency as available, false to mark it as unavailable
-
isInstanceBound
public boolean isInstanceBound()Is this Dependency "instance bound" (has been defined within the component's init method) ?- Specified by:
isInstanceBoundin interfaceDependencyContext- Returns:
- true if this dependency is instance bound, false if not
-
setInstanceBound
public void setInstanceBound(boolean instanceBound) Called by the Component implementation when the dependency is declared within the Component's init method.- Specified by:
setInstanceBoundin interfaceDependencyContext
-
needsInstance
public boolean needsInstance()Tells if the Component must be first instantiated before starting this dependency (false by default).- Specified by:
needsInstancein interfaceDependencyContext- Returns:
- true if the dependency need the component instances before it can be started, false if not.
-
getAutoConfigType
Returns the type of the field where this dependency can be injected (auto config), or return null if autoconfig is not supported.- Specified by:
getAutoConfigTypein interfaceDependencyContext- Returns:
- the type of the field which can be injected with the dependency service, or null if the dependency does not support auto config mode.
-
getService
Get the highest ranked available dependency service, or null.- Specified by:
getServicein interfaceDependencyContext- Returns:
- the highest ranked available dependency service instance, or null
-
copyToCollection
Copy all dependency service instances to the given collection.- Specified by:
copyToCollectionin interfaceDependencyContext- Parameters:
services- the collection where the dependency service instances will be copied
-
copyToMap
Copy all dependency service instances to the given map (key = dependency service, value = dependency service properties.- Specified by:
copyToMapin interfaceDependencyContext- Parameters:
map- the map where the dependency service instances (with the corresponding service properties)
-
createCopy
Creates a copy of this Dependency.- Specified by:
createCopyin interfaceDependencyContext- Returns:
- a clone of this dependency.
-
getName
Returns a description of this dependency (like the dependency service class name with associated filters)- Specified by:
getNamein interfaceComponentDependencyDeclaration
-
getSimpleName
Returns a simple name for this dependency (like the dependency service class name).- Specified by:
getSimpleNamein interfaceComponentDependencyDeclaration
-
getType
Returns the dependency symbolic type.- Specified by:
getTypein interfaceComponentDependencyDeclaration
-
getFilter
Returns the dependency filter, if any.- Specified by:
getFilterin interfaceComponentDependencyDeclaration
-
getState
public int getState()Returns this dependency state.- Specified by:
getStatein interfaceComponentDependencyDeclaration
-
setPropagate
Activates Dependency service properties propagation (to the service properties of the component to which this dependency is added).- Parameters:
propagate- true if the dependency service properties must be propagated to the service properties of the component to which this dependency is added.- Returns:
- this dependency instance
-
setPropagate
-
setPropagate
Sets a callback instance which can ba invoked with the given method in order to dynamically retrieve the dependency service properties.- Parameters:
instance- the callback instancemethod- the method to invoke on the callback instance- Returns:
- this dependency instance
-
setCallbacks
Sets the add/remove callbacks.- Parameters:
add- the callback to invoke when a dependency is addedremove- the callback to invoke when a dependency is removed- Returns:
- this dependency instance
-
setCallbacks
Sets the add/change/remove callbacks.- Parameters:
add- the callback to invoke when a dependency is addedchange- the callback to invoke when a dependency has changedremove- the callback to invoke when a dependency is removed- Returns:
- this dependency instance
-
setCallbacks
Sets the callbacks for this service. These callbacks can be used as hooks whenever a dependency is added or removed. They are called on the instance you provide. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming you don't need it in this case.- Parameters:
instance- the instance to call the callbacks onadd- the method to call when a service was addedremove- the method to call when a service was removed- Returns:
- this service dependency
-
setCallbacks
Sets the callbacks for this service. These callbacks can be used as hooks whenever a dependency is added, changed or removed. They are called on the instance you provide. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming you don't need it in this case.- Parameters:
instance- the instance to call the callbacks onadd- the method to call when a service was addedchange- the method to call when a service was changedremove- the method to call when a service was removed- Returns:
- this service dependency
-
getInstances
Returns the dependency callback instances- Returns:
- the dependency callback instances
-
setRequired
-
setAutoConfig
-
setAutoConfig
-
getComponentContext
Returns the component implementation context- Specified by:
getComponentContextin interfaceDependencyContext- Returns:
- the component implementation context
-
getDefaultService
Returns the default service, or null.- Parameters:
nullObject- if true, a null object may be returned.- Returns:
- the default service
-
ensureNotActive
protected void ensureNotActive()Checks if the component dependency is not started.
-