org.apache.felix.dependencymanager.impl
Class ServiceImpl

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

public class ServiceImpl
extends Object
implements Service

Service implementation.

Author:
Felix Project Team

Constructor Summary
ServiceImpl(DependencyManager dm)
           
 
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.
 List getDependencies()
          Returns a list of dependencies.
 Object getService()
          Returns the service instance for this service.
 Dictionary getServiceProperties()
          Returns the service properties associated with the service.
 ServiceRegistration getServiceRegistration()
          Returns the service registration for this service.
 Service remove(Dependency dependency)
          Removes a dependency from this service.
 void removeStateListener(ServiceStateListener listener)
          Removes a service state listener from this service.
 void removeStateListeners()
           
 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[] serviceNames, 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServiceImpl

public ServiceImpl(DependencyManager dm)
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

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

removeStateListeners

public void removeStateListeners()

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

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

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

getServiceRegistration

public 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

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

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

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

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

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[] serviceNames,
                            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:
serviceNames - the names of the service interface
properties - the properties for this service
Returns:
this service

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

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


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