Package org.apache.felix.dm
Interface AspectComponent
- All Superinterfaces:
Component
Interface used to configure the various parameters needed when defining
a Dependency Manager aspect component.
Aspects allow you to define an interceptor, or chain of interceptors
for a service (to add features like caching or logging, etc ...). The dependency manager intercepts
the original service, and allows you to execute some code before invoking the original service ...
The aspect will be applied to any service that matches the specified interface and filter and
will be registered with the same interface and properties as the original service, plus any
extra properties you supply here. If you declare the original service as a member it will be injected.
Usage Examples
Here is a sample showing a DatabaseCache aspect which is created each time a Database interface is registered in the registry.public class Activator extends DependencyActivatorBase { &Override public void init(BundleContext context, DependencyManager dm) throws Exception { AspectComponent aspectComponent = createAspectComponent() .setAspect(Database.class, null, 10) .setImplementation(DatabaseCache.class); dm.add(aspectComponent); } } interface Database { String get(String key); } class DatabaseCache implements Database { volatile Database originalDatabase; // injected String get(String key) { String value = cache.get(key); if (value == null) { value = this.originalDatabase.get(key); store(key, value); } return value; } ... }
For "add", "change", "remove" callbacks, the following method signatures are supported:
(Component comp, ServiceReference ref, Service service)
(Component comp, ServiceReference ref, Object service)
(Component comp, ServiceReference ref)
(Component comp, Service service)
(Component comp, Object service)
(Component comp)
(Component comp, Map properties, Service service)
(ServiceReference ref, Service service)
(ServiceReference ref, Object service)
(ServiceReference ref)
(Service service)
(Service service, Map propeerties)
(Map properties, Service, service)
(Service service, Dictionary properties)
(Dictionary properties, Service service)
(Object service)
For "swap" callbacks, the following method signatures are supported:
(Service old, Service replace)
(Object old, Object replace)
(ServiceReference old, Service old, ServiceReference replace, Service replace)
(ServiceReference old, Object old, ServiceReference replace, Object replace)
(Component comp, Service old, Service replace)
(Component comp, Object old, Object replace)
(Component comp, ServiceReference old, Service old, ServiceReference replace, Service replace)
(Component comp, ServiceReference old, Object old, ServiceReference replace, Object replace)
(ServiceReference old, ServiceReference replace)
(Component comp, ServiceReference old, ServiceReference replace)
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.felix.dm.Component
Component.ServiceScope -
Method Summary
Modifier and TypeMethodDescriptionadd(ComponentStateListener listener) Adds a component state listener to this component.add(Dependency... dependencies) Adds dependency(ies) to this component, atomically.remove(ComponentStateListener listener) Removes a component state listener from this component.remove(Dependency d) Removes a dependency from the component.Sets the service interface to apply the aspect to (required parameter)setAspectCallbackInstance(Object callbackInstance) Sets the instance to invoke the callbacks on (optional parameter).setAspectCallbacks(String add, String change, String remove, String swap) Sets name of the callbacks method to invoke on add,change,remove, or swap callbacks (optional parameter).setAspectField(String autoConfig) Sets the aspect implementation field name where to inject original service (optional parameter).setAutoConfig(Class<?> clazz, boolean autoConfig) Configures auto configuration of injected classes in the component instance.setAutoConfig(Class<?> clazz, String instanceName) Configures auto configuration of injected classes in the component instance.Sets the names of the methods used as callbacks.setCallbacks(String init, String start, String stop, String destroy) Sets the names of the methods used as callbacks.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.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.Activate debug for this component.setFactory(Object factory, String createMethod) Sets the factory to use to create the implementation.setFactory(String createMethod) Sets the factory to use to create the implementation.setImplementation(Object implementation) Sets the implementation for this component.setInterface(Class<?>[] serviceNames, Dictionary<?, ?> properties) Sets the public interfaces under which this component should be registered in the OSGi service registry.setInterface(Class<?> serviceName, Dictionary<?, ?> properties) Sets the public interface under which this component should be registered in the OSGi service registry.setInterface(String[] serviceNames, Dictionary<?, ?> properties) Sets the public interfaces under which this component should be registered in the OSGi service registry.setInterface(String serviceName, Dictionary<?, ?> properties) Sets the public interface under which this component should be registered in the OSGi service registry.setScope(Component.ServiceScope scope) Sets the component scope.setServiceProperties(Dictionary<?, ?> serviceProperties) Sets the service properties associated with the component.Methods inherited from interface org.apache.felix.dm.Component
getComponentDeclaration, getDependencyManager, getInstance, getInstances, getServiceProperties, getServiceRegistration
-
Method Details
-
setScope
Sets the component scope. -
setImplementation
Sets the implementation for this component. You can actually specify an instance you have instantiated manually, or aClassthat 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: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 statestart()is invoked right before the service is registeredstop()is invoked right after the service is unregistereddestroy()is invoked after all dependencies are removed
- Specified by:
setImplementationin interfaceComponent- Parameters:
implementation- the implementation- Returns:
- this component
- See Also:
-
add
Adds dependency(ies) to this component, atomically. If the component is already active or if you add dependencies from the init method, then you should add all the dependencies in one single add method call (using the varargs argument), because this method may trigger component activation (like the ServiceTracker.open() method does). -
remove
Removes a dependency from the component. -
add
Adds a component state listener to this component. -
remove
Removes a component state listener from this component. -
setInterface
Sets the public interface under which this component should be registered in the OSGi service registry.- Specified by:
setInterfacein interfaceComponent- Parameters:
serviceName- the name of the service interfaceproperties- the properties for this service- Returns:
- this component
-
setInterface
Sets the public interfaces under which this component should be registered in the OSGi service registry.- Specified by:
setInterfacein interfaceComponent- Parameters:
serviceNames- the names of the service interfaceproperties- the properties for these services- Returns:
- this component
-
setInterface
Sets the public interface under which this component should be registered in the OSGi service registry.- Specified by:
setInterfacein interfaceComponent- Parameters:
serviceName- the name of the service interfaceproperties- the properties for this service- Returns:
- this component
-
setInterface
Sets the public interfaces under which this component should be registered in the OSGi service registry.- Specified by:
setInterfacein interfaceComponent- Parameters:
serviceNames- the names of the service interfaceproperties- the properties for these services- Returns:
- this component
-
setAutoConfig
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:
setAutoConfigin interfaceComponent- Parameters:
clazz- the class (from the list above)autoConfig-falseto turn off auto configuration
-
setAutoConfig
Configures auto configuration of injected classes in the component instance.- Specified by:
setAutoConfigin interfaceComponent- Parameters:
clazz- the class (from the list above)instanceName- the name of the instance to inject the class into- See Also:
-
setServiceProperties
Sets the service properties associated with the component. If the service was already registered, it will be updated.- Specified by:
setServicePropertiesin interfaceComponent- Parameters:
serviceProperties- the properties
-
setCallbacks
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:- method(Component component)
- method()
- Specified by:
setCallbacksin interfaceComponent- Parameters:
init- the name of the init methodstart- the name of the start methodstop- the name of the stop methoddestroy- the name of the destroy method- Returns:
- the component
-
setCallbacks
AspectComponent setCallbacks(Object instance, String init, String start, String stop, String destroy) 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:
setCallbacksin interfaceComponent- Returns:
- this component
-
setFactory
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 withsetCompositionto 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 aClass.- Specified by:
setFactoryin interfaceComponent- Parameters:
factory- the factory instance or classcreateMethod- the name of the create method- Returns:
- this component
-
setFactory
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 withsetCompositionto 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:
setFactoryin interfaceComponent- Parameters:
createMethod- the name of the create method- Returns:
- this component
-
setComposition
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 anObject[].- Specified by:
setCompositionin interfaceComponent- Parameters:
instance- the instance that has the methodgetMethod- the method to invoke- Returns:
- this component
-
setComposition
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 anObject[].- Specified by:
setCompositionin interfaceComponent- Parameters:
getMethod- the method to invoke- Returns:
- this component
-
setDebug
Activate debug for this component. Informations related to dependency processing will be displayed using osgi log service, our to standard output if no log service is currently available. -
setAspect
Sets the service interface to apply the aspect to (required parameter)- Parameters:
service- the service interface to apply the aspect tofilter- the filter condition to use with the service aspect interface (null if no filter)ranking- the level used to organize the aspect chain ordering- Returns:
- this aspect parameter instance
-
setAspectField
Sets the aspect implementation field name where to inject original service (optional parameter). If not set or null, any field matching the original service will be injected.- Parameters:
autoConfig- the aspect implementation field name where to inject original service- Returns:
- this aspect parameter instance
-
setAspectCallbacks
Sets name of the callbacks method to invoke on add,change,remove, or swap callbacks (optional parameter).- Parameters:
add- name of the callback method to invoke on addchange- name of the callback method to invoke on changeremove- name of the callback method to invoke on removeswap- name of the callback method to invoke on swap- Returns:
- this aspect parameter instance
-
setAspectCallbackInstance
Sets the instance to invoke the callbacks on (optional parameter). null means the callbacks will be invoked on the aspect implementation object.- Parameters:
callbackInstance- the instance to invoke the callbacks on- Returns:
- this aspect parameter instance
-