org.apache.felix.dm.impl
Class ComponentImpl

java.lang.Object
  extended by org.apache.felix.dm.impl.ComponentImpl
All Implemented Interfaces:
Component, ComponentDeclaration, DependencyService

public class ComponentImpl
extends Object
implements Component, DependencyService, ComponentDeclaration

Component implementation.

Author:
Felix Project Team

Field Summary
 
Fields inherited from interface org.apache.felix.dm.ComponentDeclaration
STATE_NAMES, STATE_REGISTERED, STATE_UNREGISTERED
 
Constructor Summary
ComponentImpl(BundleContext context, DependencyManager manager, Logger logger)
           
 
Method Summary
 Component add(Dependency dependency)
          Adds a new dependency to this component.
 Component add(List dependencies)
           
 void addStateListener(ComponentStateListener listener)
          Adds a component state listener to this component.
 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.
protected  void ensureNotActive()
           
 boolean getAutoConfig(Class clazz)
          Returns the status of auto configuration of the specified class.
 String getAutoConfigInstance(Class clazz)
          Returns the instance variable name of auto configuration of the specified class.
 ComponentDependencyDeclaration[] getComponentDependencies()
          Returns a list of dependencies associated with this component.
 Object[] getCompositionInstances()
          Returns the composition instances that make up this component, or just the component instance if it does not have a composition, or an empty array if the component has not even been instantiated.
 List getDependencies()
          Returns a list of dependencies.
 DependencyManager getDependencyManager()
          Returns the dependency manager associated with this component.
 String getName()
          Returns the name of this component.
 Object getService()
          Returns the component instance for this component.
 Component getServiceInterface()
           
 Dictionary getServiceProperties()
          Returns the service properties associated with the component.
 ServiceRegistration getServiceRegistration()
          Returns the service registration for this component.
 int getState()
          Returns the state of this component.
 void initService()
           
 void invokeCallbackMethod(Object[] instances, String methodName, Class[][] signatures, Object[][] parameters)
          Can be called by the dependency whenever it wants to invoke callback methods.
 boolean isInstantiated()
           
 boolean isRegistered()
           
 Component remove(Dependency dependency)
          Removes a dependency from this component.
 void removeStateListener(ComponentStateListener listener)
          Removes a component state listener from this component.
 void removeStateListeners()
           
 Component setAutoConfig(Class clazz, boolean autoConfig)
          Configures auto configuration of injected classes in the component instance.
 Component setAutoConfig(Class clazz, String instanceName)
          Configures auto configuration of injected classes in the component instance.
 Component setCallbacks(Object instance, String init, String start, String stop, String destroy)
          Sets the names of the methods used as callbacks.
 Component setCallbacks(String init, String start, String stop, String destroy)
          Sets the names of the methods used as callbacks.
 Component 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.
 Component 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.
 Component setFactory(Object factory, String createMethod)
          Sets the factory to use to create the implementation.
 Component setFactory(String createMethod)
          Sets the factory to use to create the implementation.
 Component setImplementation(Object implementation)
          Sets the implementation for this component.
 Component setInterface(String[] serviceName, Dictionary properties)
          Sets the public interfaces under which this component should be registered in the OSGi service registry.
 Component setInterface(String serviceName, Dictionary properties)
          Sets the public interface under which this component should be registered in the OSGi service registry.
 Component setServiceProperties(Dictionary serviceProperties)
          Sets the service properties associated with the component.
 void start()
          Starts the component.
 void stop()
          Stops the component.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ComponentImpl

public ComponentImpl(BundleContext context,
                     DependencyManager manager,
                     Logger logger)
Method Detail

add

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

Specified by:
add in interface Component
Parameters:
dependency - the dependency to add
Returns:
this component

add

public Component add(List dependencies)
Specified by:
add in interface Component

remove

public Component remove(Dependency dependency)
Description copied from interface: Component
Removes a dependency from this component.

Specified by:
remove in interface Component
Parameters:
dependency - the dependency to remove
Returns:
this component

getDependencies

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

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

getServiceRegistration

public ServiceRegistration getServiceRegistration()
Description copied from interface: Component
Returns the service registration for this component. The method will return null if no service registration is available, for example if this component is not registered as a service at all.

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

getService

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

Specified by:
getService in interface Component
Specified by:
getService in interface DependencyService
Returns:
the component instance

getServiceInterface

public Component getServiceInterface()
Specified by:
getServiceInterface in interface DependencyService

dependencyAvailable

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

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

dependencyChanged

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

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

dependencyUnavailable

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

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

start

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

Specified by:
start in interface Component

stop

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

Specified by:
stop in interface Component

setInterface

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

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

setInterface

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

Specified by:
setInterface in interface Component
Parameters:
serviceName - the names of the service interface
properties - the properties for these services
Returns:
this component

setCallbacks

public Component setCallbacks(String init,
                              String start,
                              String stop,
                              String destroy)
Description copied from interface: Component
Sets the names of the methods used as callbacks. These methods, when found, are invoked as part of the life cycle management of the component implementation. The dependency manager will look for a method of this name with the following signatures, in this order:
  1. method(Component component)
  2. method()

Specified by:
setCallbacks in interface Component
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 component

setCallbacks

public Component setCallbacks(Object instance,
                              String init,
                              String start,
                              String stop,
                              String destroy)
Description copied from interface: Component
Sets the names of the methods used as callbacks. These methods, when found, are invoked on the specified instance as part of the life cycle management of the component implementation.

See setCallbacks(String init, String start, String stop, String destroy) for more information on the signatures. Specifying an instance means you can create a manager that will be invoked whenever the life cycle of a component changes and this manager can then decide how to expose this life cycle to the actual component, offering an important indirection when developing your own component models.

Specified by:
setCallbacks in interface Component

setImplementation

public Component setImplementation(Object implementation)
Description copied from interface: Component
Sets the implementation for this component. 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 life cycle management options:
  1. init() is invoked after the instance has been created and dependencies have been resolved, and can be used to initialize the internal state of the instance or even to add more dependencies based on runtime state
  2. start() is invoked right 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 Component
Parameters:
implementation - the implementation
Returns:
this component
See Also:
ComponentStateListener

setFactory

public Component setFactory(Object factory,
                            String createMethod)
Description copied from interface: Component
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 component. The factory itself can also be instantiated lazily by not specifying an instance, but a Class.

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

setFactory

public Component setFactory(String createMethod)
Description copied from interface: Component
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 component.

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

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

setComposition

public Component setComposition(Object instance,
                                String getMethod)
Description copied from interface: Component
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 Component
Parameters:
instance - the instance that has the method
getMethod - the method to invoke

setComposition

public Component setComposition(String getMethod)
Description copied from interface: Component
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 Component
Parameters:
getMethod - the method to invoke

toString

public String toString()
Overrides:
toString in class Object

getServiceProperties

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

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

setServiceProperties

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

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

addStateListener

public void addStateListener(ComponentStateListener listener)
Description copied from interface: Component
Adds a component state listener to this component.

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

removeStateListener

public void removeStateListener(ComponentStateListener listener)
Description copied from interface: Component
Removes a component state listener from this component.

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

removeStateListeners

public void removeStateListeners()

invokeCallbackMethod

public void invokeCallbackMethod(Object[] instances,
                                 String methodName,
                                 Class[][] signatures,
                                 Object[][] parameters)
Description copied from interface: DependencyService
Can be called by the dependency whenever it wants to invoke callback methods.

Specified by:
invokeCallbackMethod in interface Component
Specified by:
invokeCallbackMethod in interface DependencyService

initService

public void initService()
Specified by:
initService in interface DependencyService

setAutoConfig

public Component setAutoConfig(Class clazz,
                               boolean autoConfig)
Description copied from interface: Component
Configures auto configuration of injected classes in the component instance. The following injections are currently performed, unless you explicitly turn them off:
BundleContext
the bundle context of the bundle
ServiceRegistration
the service registration used to register your service
DependencyManager
the dependency manager instance
Component
the component instance of the dependency manager

Specified by:
setAutoConfig in interface Component
Parameters:
clazz - the class (from the list above)
autoConfig - false to turn off auto configuration

setAutoConfig

public Component setAutoConfig(Class clazz,
                               String instanceName)
Description copied from interface: Component
Configures auto configuration of injected classes in the component instance.

Specified by:
setAutoConfig in interface Component
Parameters:
clazz - the class (from the list above)
instanceName - the name of the instance to inject the class into
See Also:
Component.setAutoConfig(Class, boolean)

getAutoConfig

public boolean getAutoConfig(Class clazz)
Description copied from interface: Component
Returns the status of auto configuration of the specified class.

Specified by:
getAutoConfig in interface Component

getAutoConfigInstance

public String getAutoConfigInstance(Class clazz)
Description copied from interface: Component
Returns the instance variable name of auto configuration of the specified class.

Specified by:
getAutoConfigInstance in interface Component

getCompositionInstances

public Object[] getCompositionInstances()
Description copied from interface: Component
Returns the composition instances that make up this component, or just the component instance if it does not have a composition, or an empty array if the component has not even been instantiated.

Specified by:
getCompositionInstances in interface Component
Specified by:
getCompositionInstances in interface DependencyService

ensureNotActive

protected void ensureNotActive()

isRegistered

public boolean isRegistered()
Specified by:
isRegistered in interface DependencyService

isInstantiated

public boolean isInstantiated()
Specified by:
isInstantiated in interface DependencyService

getComponentDependencies

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

Specified by:
getComponentDependencies in interface ComponentDeclaration

getName

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

Specified by:
getName in interface ComponentDeclaration

getState

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

Specified by:
getState in interface ComponentDeclaration

getDependencyManager

public DependencyManager getDependencyManager()
Description copied from interface: Component
Returns the dependency manager associated with this component.

Specified by:
getDependencyManager in interface Component


Copyright © 2011 Apache Software Foundation. All Rights Reserved.