org.apache.felix.dependencymanager
Class ServiceImpl

java.lang.Object
  extended by org.apache.felix.dependencymanager.ServiceImpl
All Implemented Interfaces:
Service, ServiceComponent

public class ServiceImpl
extends Object
implements Service, ServiceComponent

Service implementation.

Author:
Felix Project Team

Field Summary
 
Fields inherited from interface org.apache.felix.dependencymanager.ServiceComponent
STATE_NAMES, STATE_REGISTERED, STATE_UNREGISTERED
 
Constructor Summary
ServiceImpl(org.osgi.framework.BundleContext context, DependencyManager manager, Logger logger)
           
 
Method Summary
 Service add(Dependency dependency)
          Adds a new dependency to this service.
 void addStateListener(ServiceStateListener listener)
          Adds a service state listener to this service.
 void dependencyAvailable(Dependency dependency)
          Will be called when the dependency becomes available.
 void dependencyChanged(Dependency dependency)
          Will be called when the dependency becomes unavailable.
 void dependencyUnavailable(Dependency dependency)
          Will be called when the dependency changes.
 ServiceComponentDependency[] getComponentDependencies()
          Returns a list of dependencies associated with this service component.
 List getDependencies()
          Returns a list of dependencies.
 String getName()
          Returns the name of this service component.
 Object getService()
          Returns the service instance for this service.
 Dictionary getServiceProperties()
          Returns the service properties associated with the service.
 org.osgi.framework.ServiceRegistration getServiceRegistration()
          Returns the service registration for this service.
 int getState()
          Returns the state of this service component.
 Service remove(Dependency dependency)
          Removes a dependency from this service.
 void removeStateListener(ServiceStateListener listener)
          Removes a service state listener from this service.
 void setAutoConfig(Class clazz, boolean autoConfig)
           
 void setAutoConfig(Class clazz, String instanceName)
           
 Service setCallbacks(String init, String start, String stop, String destroy)
          Sets the names of the methods used as callbacks.
 Service setComposition(Object instance, String getMethod)
          Sets the instance and method to invoke to get back all instances that are part of a composition and need dependencies injected.
 Service setComposition(String getMethod)
          Sets the method to invoke on the service implementation to get back all instances that are part of a composition and need dependencies injected.
 Service setFactory(Object factory, String createMethod)
          Sets the factory to use to create the implementation.
 Service setFactory(String createMethod)
          Sets the factory to use to create the implementation.
 Service setImplementation(Object implementation)
          Sets the implementation for this service.
 Service setInterface(String[] serviceName, Dictionary properties)
          Sets the public interfaces under which this service should be registered in the OSGi service registry.
 Service setInterface(String serviceName, Dictionary properties)
          Sets the public interface under which this service should be registered in the OSGi service registry.
 void setServiceProperties(Dictionary serviceProperties)
          Sets the service properties associated with the service.
 void start()
          Starts the service.
 void stop()
          Stops the service.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ServiceImpl

public ServiceImpl(org.osgi.framework.BundleContext context,
                   DependencyManager manager,
                   Logger logger)
Method Detail

add

public Service add(Dependency dependency)
Description copied from interface: Service
Adds a new dependency to this service.

Specified by:
add in interface Service
Parameters:
dependency - the dependency to add
Returns:
this service

remove

public Service remove(Dependency dependency)
Description copied from interface: Service
Removes a dependency from this service.

Specified by:
remove in interface Service
Parameters:
dependency - the dependency to remove
Returns:
this service

getDependencies

public List getDependencies()
Description copied from interface: Service
Returns a list of dependencies.

Specified by:
getDependencies in interface Service
Returns:
a list of dependencies

getServiceRegistration

public org.osgi.framework.ServiceRegistration getServiceRegistration()
Description copied from interface: Service
Returns the service registration for this service. The method will return null if no service registration is available.

Specified by:
getServiceRegistration in interface Service
Returns:
the service registration

getService

public Object getService()
Description copied from interface: Service
Returns the service instance for this service. The method will return null if no service instance is available.

Specified by:
getService in interface Service
Returns:
the service instance

dependencyAvailable

public void dependencyAvailable(Dependency dependency)
Description copied from interface: Service
Will be called when the dependency becomes available.

Specified by:
dependencyAvailable in interface Service
Parameters:
dependency - the dependency

dependencyChanged

public void dependencyChanged(Dependency dependency)
Description copied from interface: Service
Will be called when the dependency becomes unavailable.

Specified by:
dependencyChanged in interface Service
Parameters:
dependency - the dependency

dependencyUnavailable

public void dependencyUnavailable(Dependency dependency)
Description copied from interface: Service
Will be called when the dependency changes.

Specified by:
dependencyUnavailable in interface Service
Parameters:
dependency - the dependency

start

public void start()
Description copied from interface: Service
Starts the service. This activates the dependency tracking mechanism for this service.

Specified by:
start in interface Service

stop

public void stop()
Description copied from interface: Service
Stops the service. This deactivates the dependency tracking mechanism for this service.

Specified by:
stop in interface Service

setInterface

public Service setInterface(String serviceName,
                            Dictionary properties)
Description copied from interface: Service
Sets the public interface under which this service should be registered in the OSGi service registry.

Specified by:
setInterface in interface Service
Parameters:
serviceName - the name of the service interface
properties - the properties for this service
Returns:
this service

setInterface

public Service setInterface(String[] serviceName,
                            Dictionary properties)
Description copied from interface: Service
Sets the public interfaces under which this service should be registered in the OSGi service registry.

Specified by:
setInterface in interface Service
Parameters:
serviceName - the names of the service interface
properties - the properties for this service
Returns:
this service

setCallbacks

public Service setCallbacks(String init,
                            String start,
                            String stop,
                            String destroy)
Description copied from interface: Service
Sets the names of the methods used as callbacks. These methods, when found, are invoked as part of the life-cycle management of the service implementation. The methods should not have any parameters.

Specified by:
setCallbacks in interface Service
Parameters:
init - the name of the init method
start - the name of the start method
stop - the name of the stop method
destroy - the name of the destroy method
Returns:
the service instance

setImplementation

public Service setImplementation(Object implementation)
Description copied from interface: Service
Sets the implementation for this service. You can actually specify an instance you have instantiated manually, or a Class that will be instantiated using its default constructor when the required dependencies are resolved (effectively giving you a lazy instantiation mechanism). There are four special methods that are called when found through reflection to give you some life-cycle management options:
  1. init() is invoked right after the instance has been created, and before any dependencies are resolved, and can be used to initialize the internal state of the instance
  2. start() is invoked after the required dependencies are resolved and injected, and before the service is registered
  3. stop() is invoked right after the service is unregistered
  4. destroy() is invoked after all dependencies are removed
In short, this allows you to initialize your instance before it is registered, perform some post-initialization and pre-destruction code as well as final cleanup. If a method is not defined, it simply is not called, so you can decide which one(s) you need. If you need even more fine-grained control, you can register as a service state listener too.

Specified by:
setImplementation in interface Service
Parameters:
implementation - the implementation
Returns:
this service
See Also:
ServiceStateListener

setFactory

public Service setFactory(Object factory,
                          String createMethod)
Description copied from interface: Service
Sets the factory to use to create the implementation. You can specify both the factory class and method to invoke. The method should return the implementation, and can use any method to create it. Actually, this can be used together with setComposition to create a composition of instances that work together to implement a service. The factory itself can also be instantiated lazily by not specifying an instance, but a Class.

Specified by:
setFactory in interface Service
Parameters:
factory - the factory instance or class
createMethod - the name of the create method

setFactory

public Service setFactory(String createMethod)
Description copied from interface: Service
Sets the factory to use to create the implementation. You specify the method to invoke. The method should return the implementation, and can use any method to create it. Actually, this can be used together with setComposition to create a composition of instances that work together to implement a service.

Note that currently, there is no default for the factory, so please use setFactory(factory, createMethod) instead.

Specified by:
setFactory in interface Service
Parameters:
createMethod - the name of the create method

setComposition

public Service setComposition(Object instance,
                              String getMethod)
Description copied from interface: Service
Sets the instance and method to invoke to get back all instances that are part of a composition and need dependencies injected. All of them will be searched for any of the dependencies. The method that is invoked must return an Object[].

Specified by:
setComposition in interface Service
Parameters:
instance - the instance that has the method
getMethod - the method to invoke

setComposition

public Service setComposition(String getMethod)
Description copied from interface: Service
Sets the method to invoke on the service implementation to get back all instances that are part of a composition and need dependencies injected. All of them will be searched for any of the dependencies. The method that is invoked must return an Object[].

Specified by:
setComposition in interface Service
Parameters:
getMethod - the method to invoke

toString

public String toString()
Overrides:
toString in class Object

getServiceProperties

public Dictionary getServiceProperties()
Description copied from interface: Service
Returns the service properties associated with the service.

Specified by:
getServiceProperties in interface Service
Returns:
the properties or null if there are none

setServiceProperties

public void setServiceProperties(Dictionary serviceProperties)
Description copied from interface: Service
Sets the service properties associated with the service. If the service was already registered, it will be updated.

Specified by:
setServiceProperties in interface Service
Parameters:
serviceProperties - the properties

addStateListener

public void addStateListener(ServiceStateListener listener)
Description copied from interface: Service
Adds a service state listener to this service.

Specified by:
addStateListener in interface Service
Parameters:
listener - the state listener

removeStateListener

public void removeStateListener(ServiceStateListener listener)
Description copied from interface: Service
Removes a service state listener from this service.

Specified by:
removeStateListener in interface Service
Parameters:
listener - the state listener

setAutoConfig

public void setAutoConfig(Class clazz,
                          boolean autoConfig)

setAutoConfig

public void setAutoConfig(Class clazz,
                          String instanceName)

getComponentDependencies

public ServiceComponentDependency[] getComponentDependencies()
Description copied from interface: ServiceComponent
Returns a list of dependencies associated with this service component.

Specified by:
getComponentDependencies in interface ServiceComponent

getName

public String getName()
Description copied from interface: ServiceComponent
Returns the name of this service component.

Specified by:
getName in interface ServiceComponent

getState

public int getState()
Description copied from interface: ServiceComponent
Returns the state of this service component.

Specified by:
getState in interface ServiceComponent


Copyright © 2006-2011 Apache Software Foundation. All Rights Reserved.