org.apache.felix.dm.impl.dependencies
Class ServiceDependencyImpl

java.lang.Object
  extended by org.apache.felix.dm.impl.dependencies.DependencyBase
      extended by org.apache.felix.dm.impl.dependencies.ServiceDependencyImpl
All Implemented Interfaces:
ComponentDependencyDeclaration, Dependency, DependencyActivation, ServiceDependency, ServiceTrackerCustomizer
Direct Known Subclasses:
TemporalServiceDependencyImpl

public class ServiceDependencyImpl
extends DependencyBase
implements ServiceDependency, ServiceTrackerCustomizer, ComponentDependencyDeclaration

Service dependency that can track an OSGi service.

Author:
Felix Project Team

Field Summary
protected  BundleContext m_context
           
protected  ServiceReference m_reference
           
protected  Object m_serviceInstance
           
protected  List m_services
           
protected  Class m_trackedServiceName
           
protected  ServiceTracker m_tracker
           
 
Fields inherited from class org.apache.felix.dm.impl.dependencies.DependencyBase
m_logger
 
Fields inherited from interface org.apache.felix.dm.ComponentDependencyDeclaration
STATE_AVAILABLE_OPTIONAL, STATE_AVAILABLE_REQUIRED, STATE_NAMES, STATE_UNAVAILABLE_OPTIONAL, STATE_UNAVAILABLE_REQUIRED
 
Constructor Summary
ServiceDependencyImpl(BundleContext context, Logger logger)
          Creates a new service dependency.
ServiceDependencyImpl(ServiceDependencyImpl prototype)
          Copying constructor that clones an existing instance.
 
Method Summary
 void addedService(ServiceReference ref, Object service)
          marrs: A service has been added to the ServiceTracker.
 Object addingService(ServiceReference ref)
          A service is being added to the ServiceTracker.
 Dependency createCopy()
          Creates a copy of this dependency, cloning all declared state, but not the runtime state.
 Object getAutoConfigInstance()
          Returns the instance that is injected.
 String getAutoConfigName()
          Returns the name of the member in the class of the component instance to inject into.
 Class getAutoConfigType()
          Returns the type of the instance that is injected.
 Class getInterface()
           
 String getName()
          Returns the name of this dependency.
 Dictionary getProperties()
           
 Object getService()
           
 int getState()
          Returns the state of this dependency.
 String getType()
          Returns the name of the type of this dependency.
 void invoke(DependencyService dependencyService, ServiceReference reference, Object service, String name)
           
 void invokeAdded(DependencyService service)
          Invoke the "added" callback on a required dependency.
 void invokeAdded(DependencyService dependencyService, ServiceReference reference, Object service)
           
 void invokeChanged(DependencyService dependencyService, ServiceReference reference, Object service)
           
 void invokeRemoved(DependencyService service)
          Invoke the "removed" callback on a required dependency.
 void invokeRemoved(DependencyService dependencyService, ServiceReference reference, Object service)
           
 boolean isAutoConfig()
          Returns true>code> if auto configuration is enabled for this dependency.
 boolean isAvailable()
          Returns true if the dependency is available.
 boolean isPropagated()
           
 Object lookupService()
           
 ServiceReference lookupServiceReference()
           
protected  boolean makeAvailable()
           
 void modifiedService(ServiceReference ref, Object service)
          A service tracked by the ServiceTracker has been modified.
 void removedService(ServiceReference ref, Object service)
          A service tracked by the ServiceTracker has been removed.
 ServiceDependency setAutoConfig(boolean autoConfig)
          Sets auto configuration for this service.
 ServiceDependency setAutoConfig(String instanceName)
          Sets auto configuration for this service.
 ServiceDependency setCallbacks(Object instance, String added, String removed)
          Sets the callbacks for this service.
 ServiceDependency setCallbacks(Object instance, String added, String changed, String removed)
          Sets the callbacks for this service.
 ServiceDependency setCallbacks(String added, String removed)
          Sets the callbacks for this service.
 ServiceDependency setCallbacks(String added, String changed, String removed)
          Sets the callbacks for this service.
 ServiceDependency setDefaultImplementation(Object implementation)
          Sets the default implementation for this service dependency.
 ServiceDependency setInstanceBound(boolean isInstanceBound)
           
 ServiceDependency setPropagate(boolean propagate)
          Sets propagation of the service dependency properties to the provided service properties.
 ServiceDependency setPropagate(Object instance, String method)
          Sets an Object instance and a callback method used to propagate some properties to the provided service properties.
 ServiceDependency setRequired(boolean required)
          Sets the required flag which determines if this service is required or not.
 ServiceDependency setService(Class serviceName)
          Sets the name of the service that should be tracked.
 ServiceDependency setService(Class serviceName, ServiceReference serviceReference)
          Sets the name of the service that should be tracked.
 ServiceDependency setService(Class serviceName, String serviceFilter)
          Sets the name of the service that should be tracked.
 ServiceDependency setService(String serviceFilter)
          Sets the filter for the services that should be tracked.
 void start(DependencyService service)
           
 void stop(DependencyService service)
           
 String toString()
           
 
Methods inherited from class org.apache.felix.dm.impl.dependencies.DependencyBase
isInstanceBound, isRequired, setIsInstanceBound, setIsRequired
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.apache.felix.dm.Dependency
isInstanceBound, isRequired
 

Field Detail

m_services

protected List m_services

m_tracker

protected volatile ServiceTracker m_tracker

m_context

protected BundleContext m_context

m_trackedServiceName

protected volatile Class m_trackedServiceName

m_reference

protected ServiceReference m_reference

m_serviceInstance

protected Object m_serviceInstance
Constructor Detail

ServiceDependencyImpl

public ServiceDependencyImpl(BundleContext context,
                             Logger logger)
Creates a new service dependency.

Parameters:
context - the bundle context
logger - the logger

ServiceDependencyImpl

public ServiceDependencyImpl(ServiceDependencyImpl prototype)
Copying constructor that clones an existing instance.

Method Detail

createCopy

public Dependency createCopy()
Description copied from interface: Dependency
Creates a copy of this dependency, cloning all declared state, but not the runtime state.

Specified by:
createCopy in interface Dependency

isAutoConfig

public boolean isAutoConfig()
Description copied from interface: Dependency
Returns true>code> if auto configuration is enabled for this dependency. Auto configuration means that a dependency is injected in the component instance when it's available, and if it's unavailable, a "null object" will be inserted instead.

Specified by:
isAutoConfig in interface Dependency
Returns:
true if auto configuration is enabled for this dependency

isAvailable

public boolean isAvailable()
Description copied from interface: Dependency
Returns true if the dependency is available.

Specified by:
isAvailable in interface Dependency
Returns:
true if the dependency is available

getService

public Object getService()

lookupService

public Object lookupService()

lookupServiceReference

public ServiceReference lookupServiceReference()

getInterface

public Class getInterface()

start

public void start(DependencyService service)
Specified by:
start in interface DependencyActivation

stop

public void stop(DependencyService service)
Specified by:
stop in interface DependencyActivation

addingService

public Object addingService(ServiceReference ref)
Description copied from interface: ServiceTrackerCustomizer
A service is being added to the ServiceTracker.

This method is called before a service which matched the search parameters of the ServiceTracker is added to the ServiceTracker. This method should return the service object to be tracked for the specified ServiceReference. The returned service object is stored in the ServiceTracker and is available from the getService and getServices methods.

Specified by:
addingService in interface ServiceTrackerCustomizer
Parameters:
ref - The reference to the service being added to the ServiceTracker.
Returns:
The service object to be tracked for the specified referenced service or null if the specified referenced service should not be tracked.

addedService

public void addedService(ServiceReference ref,
                         Object service)
Description copied from interface: ServiceTrackerCustomizer
marrs: A service has been added to the ServiceTracker.

Specified by:
addedService in interface ServiceTrackerCustomizer

modifiedService

public void modifiedService(ServiceReference ref,
                            Object service)
Description copied from interface: ServiceTrackerCustomizer
A service tracked by the ServiceTracker has been modified.

This method is called when a service being tracked by the ServiceTracker has had it properties modified.

Specified by:
modifiedService in interface ServiceTrackerCustomizer
Parameters:
ref - The reference to the service that has been modified.
service - The service object for the specified referenced service.

removedService

public void removedService(ServiceReference ref,
                           Object service)
Description copied from interface: ServiceTrackerCustomizer
A service tracked by the ServiceTracker has been removed.

This method is called after a service is no longer being tracked by the ServiceTracker.

Specified by:
removedService in interface ServiceTrackerCustomizer
Parameters:
ref - The reference to the service that has been removed.
service - The service object for the specified referenced service.

invokeAdded

public void invokeAdded(DependencyService dependencyService,
                        ServiceReference reference,
                        Object service)

invokeChanged

public void invokeChanged(DependencyService dependencyService,
                          ServiceReference reference,
                          Object service)

invokeRemoved

public void invokeRemoved(DependencyService dependencyService,
                          ServiceReference reference,
                          Object service)

invoke

public void invoke(DependencyService dependencyService,
                   ServiceReference reference,
                   Object service,
                   String name)

makeAvailable

protected boolean makeAvailable()

setService

public ServiceDependency setService(Class serviceName)
Sets the name of the service that should be tracked.

Specified by:
setService in interface ServiceDependency
Parameters:
serviceName - the name of the service
Returns:
this service dependency

setService

public ServiceDependency setService(Class serviceName,
                                    String serviceFilter)
Sets the name of the service that should be tracked. You can either specify only the name, or the name and a filter. In the latter case, the filter is used to track the service and should only return services of the type that was specified in the name. To make sure of this, the filter is actually extended internally to filter on the correct name.

Specified by:
setService in interface ServiceDependency
Parameters:
serviceName - the name of the service
serviceFilter - the filter condition
Returns:
this service dependency

setService

public ServiceDependency setService(String serviceFilter)
Description copied from interface: ServiceDependency
Sets the filter for the services that should be tracked. Any service object matching the filter will be returned, without any additional filter on the class.

Specified by:
setService in interface ServiceDependency
Parameters:
serviceFilter - the filter condition
Returns:
this service dependency

setService

public ServiceDependency setService(Class serviceName,
                                    ServiceReference serviceReference)
Sets the name of the service that should be tracked. You can either specify only the name, or the name and a reference. In the latter case, the service reference is used to track the service and should only return services of the type that was specified in the name.

Specified by:
setService in interface ServiceDependency
Parameters:
serviceName - the name of the service
serviceReference - the service reference to track
Returns:
this service dependency

setDefaultImplementation

public ServiceDependency setDefaultImplementation(Object implementation)
Sets the default implementation for this service dependency. You can use this to supply your own implementation that will be used instead of a Null Object when the dependency is not available. This is also convenient if the service dependency is not an interface (which would cause the Null Object creation to fail) but a class.

Specified by:
setDefaultImplementation in interface ServiceDependency
Parameters:
implementation - the instance to use or the class to instantiate if you want to lazily instantiate this implementation
Returns:
this service dependency

setRequired

public ServiceDependency setRequired(boolean required)
Sets the required flag which determines if this service is required or not.

Specified by:
setRequired in interface ServiceDependency
Parameters:
required - the required flag
Returns:
this service dependency

setInstanceBound

public ServiceDependency setInstanceBound(boolean isInstanceBound)
Specified by:
setInstanceBound in interface ServiceDependency

setAutoConfig

public ServiceDependency setAutoConfig(boolean autoConfig)
Sets auto configuration for this service. Auto configuration allows the dependency to fill in any attributes in the service implementation that are of the same type as this dependency. Default is on.

Specified by:
setAutoConfig in interface ServiceDependency
Parameters:
autoConfig - the value of auto config
Returns:
this service dependency

setAutoConfig

public ServiceDependency setAutoConfig(String instanceName)
Sets auto configuration for this service. Auto configuration allows the dependency to fill in the attribute in the service implementation that has the same type and instance name.

Specified by:
setAutoConfig in interface ServiceDependency
Parameters:
instanceName - the name of attribute to auto config
Returns:
this service dependency

setCallbacks

public ServiceDependency setCallbacks(String added,
                                      String removed)
Sets the callbacks for this service. These callbacks can be used as hooks whenever a dependency is added or removed. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming you don't need it in this case.

Specified by:
setCallbacks in interface ServiceDependency
Parameters:
added - the method to call when a service was added
removed - the method to call when a service was removed
Returns:
this service dependency

setCallbacks

public ServiceDependency setCallbacks(String added,
                                      String changed,
                                      String removed)
Sets the callbacks for this service. These callbacks can be used as hooks whenever a dependency is added, changed or removed. When you specify callbacks, the auto configuration feature is automatically turned off, because we're assuming you don't need it in this case.

Specified by:
setCallbacks in interface ServiceDependency
Parameters:
added - the method to call when a service was added
changed - the method to call when a service was changed
removed - the method to call when a service was removed
Returns:
this service dependency

setCallbacks

public ServiceDependency setCallbacks(Object instance,
                                      String added,
                                      String removed)
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.

Specified by:
setCallbacks in interface ServiceDependency
Parameters:
instance - the instance to call the callbacks on
added - the method to call when a service was added
removed - the method to call when a service was removed
Returns:
this service dependency

setCallbacks

public ServiceDependency setCallbacks(Object instance,
                                      String added,
                                      String changed,
                                      String removed)
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.

Specified by:
setCallbacks in interface ServiceDependency
Parameters:
instance - the instance to call the callbacks on
added - the method to call when a service was added
changed - the method to call when a service was changed
removed - the method to call when a service was removed
Returns:
this service dependency

toString

public String toString()
Overrides:
toString in class Object

getAutoConfigName

public String getAutoConfigName()
Description copied from interface: Dependency
Returns the name of the member in the class of the component instance to inject into. If you specify this, not all members of the right type will be injected, only the member whose name matches.

Specified by:
getAutoConfigName in interface Dependency
Returns:

getAutoConfigInstance

public Object getAutoConfigInstance()
Description copied from interface: Dependency
Returns the instance that is injected.

Specified by:
getAutoConfigInstance in interface Dependency
Returns:
the instance that is injected

getAutoConfigType

public Class getAutoConfigType()
Description copied from interface: Dependency
Returns the type of the instance that is injected.

Specified by:
getAutoConfigType in interface Dependency
Returns:
the type of the instance that is injected

getName

public String getName()
Description copied from interface: ComponentDependencyDeclaration
Returns the name of this dependency.

Specified by:
getName in interface ComponentDependencyDeclaration

getState

public int getState()
Description copied from interface: ComponentDependencyDeclaration
Returns the state of this dependency.

Specified by:
getState in interface ComponentDependencyDeclaration

getType

public String getType()
Description copied from interface: ComponentDependencyDeclaration
Returns the name of the type of this dependency.

Specified by:
getType in interface ComponentDependencyDeclaration

invokeAdded

public void invokeAdded(DependencyService service)
Description copied from interface: Dependency
Invoke the "added" callback on a required dependency.

Specified by:
invokeAdded in interface Dependency

invokeRemoved

public void invokeRemoved(DependencyService service)
Description copied from interface: Dependency
Invoke the "removed" callback on a required dependency.

Specified by:
invokeRemoved in interface Dependency

getProperties

public Dictionary getProperties()
Specified by:
getProperties in interface Dependency

isPropagated

public boolean isPropagated()
Specified by:
isPropagated in interface Dependency

setPropagate

public ServiceDependency setPropagate(boolean propagate)
Description copied from interface: ServiceDependency
Sets propagation of the service dependency properties to the provided service properties. Any additional service properties specified directly are merged with these.

Specified by:
setPropagate in interface ServiceDependency

setPropagate

public ServiceDependency setPropagate(Object instance,
                                      String method)
Description copied from interface: ServiceDependency
Sets an Object instance and a callback method used to propagate some properties to the provided service properties. The method will be invoked on the specified object instance and must have one of the following signatures:

  • Dictionary callback(ServiceReference, Object service)
  • Dictionary callback(ServiceReference)

Specified by:
setPropagate in interface ServiceDependency
Parameters:
instance - the Object instance which is used to retrieve propagated service properties
method - the method to invoke for retrieving the properties to be propagated to the service properties.
Returns:
this service dependency.


Copyright © 2013 The Apache Software Foundation. All Rights Reserved.