public interface AdapterComponent extends Component
AspectComponent
, are used to "extend"
existing services, and can publish different services based on the existing one.
An example would be implementing a management interface for an existing service, etc ....
When you create an adapter component, it will be applied to any service that matches the implemented interface and filter. The adapter will be registered with the specified interface and existing properties from the original service plus any extra properties you supply here. If you declare the original service as a member it will be injected.
public class Activator extends DependencyActivatorBase { &Override public void init(BundleContext context, DependencyManager dm) throws Exception { AdapterComponent adapterComponent = createAdapterComponent() .setAdaptee(HelloService.class, "(foo=bar)") .setInterface(HttpServlet.class.getName(), null) .setImplementation(HelloServlet.class); dm.add(adapterComponent); } } public interface HelloService { String sayHello(); } public class HelloServlet extends HttpServlet { volatile HelloService adatpee; // injected void doGet(HttpServletRequest req, HttpServletResponse resp) { ... resp.getWriter().println(adaptee.sayHello()); } }
When you use callbacks to get injected with the adaptee service, the "add", "change", "remove" callbacks support the following method signatures:
(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)
Component.ServiceScope
Modifier and Type | Method and Description |
---|---|
AdapterComponent |
add(ComponentStateListener listener)
Adds a component state listener to this component.
|
AdapterComponent |
add(Dependency... dependencies)
Adds dependency(ies) to this component, atomically.
|
AdapterComponent |
remove(ComponentStateListener listener)
Removes a component state listener from this component.
|
AdapterComponent |
remove(Dependency d)
Removes a dependency from the component.
|
AdapterComponent |
setAdaptee(java.lang.Class<?> service,
java.lang.String filter)
Sets the service interface to apply the adapter to
|
AdapterComponent |
setAdapteeCallbackInstance(java.lang.Object callbackInstance)
Sets the instance to invoke the callbacks on (null by default, meaning the callbacks have to be invoked on the adapter itself)
|
AdapterComponent |
setAdapteeCallbacks(java.lang.String add,
java.lang.String change,
java.lang.String remove,
java.lang.String swap)
Sets the callbacks to invoke when injecting the adaptee service into the adapter component.
|
AdapterComponent |
setAdapteeField(java.lang.String autoConfig)
Sets the name of the member to inject the service into
|
AdapterComponent |
setAutoConfig(java.lang.Class<?> clazz,
boolean autoConfig)
Configures auto configuration of injected classes in the component instance.
|
AdapterComponent |
setAutoConfig(java.lang.Class<?> clazz,
java.lang.String instanceName)
Configures auto configuration of injected classes in the component instance.
|
AdapterComponent |
setCallbacks(java.lang.Object instance,
java.lang.String init,
java.lang.String start,
java.lang.String stop,
java.lang.String destroy)
Sets the names of the methods used as callbacks.
|
AdapterComponent |
setCallbacks(java.lang.String init,
java.lang.String start,
java.lang.String stop,
java.lang.String destroy)
Sets the names of the methods used as callbacks.
|
AdapterComponent |
setComposition(java.lang.Object instance,
java.lang.String getMethod)
Sets the instance and method to invoke to get back all instances that
are part of a composition and need dependencies injected.
|
AdapterComponent |
setComposition(java.lang.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.
|
AdapterComponent |
setDebug(java.lang.String label)
Activate debug for this component.
|
AdapterComponent |
setFactory(java.lang.Object factory,
java.lang.String createMethod)
Sets the factory to use to create the implementation.
|
AdapterComponent |
setFactory(java.lang.String createMethod)
Sets the factory to use to create the implementation.
|
AdapterComponent |
setImplementation(java.lang.Object implementation)
Sets the implementation for this component.
|
AdapterComponent |
setInterface(java.lang.Class<?>[] serviceNames,
java.util.Dictionary<?,?> properties)
Sets the public interfaces under which this component should be registered
in the OSGi service registry.
|
AdapterComponent |
setInterface(java.lang.Class<?> serviceName,
java.util.Dictionary<?,?> properties)
Sets the public interface under which this component should be registered
in the OSGi service registry.
|
AdapterComponent |
setInterface(java.lang.String[] serviceNames,
java.util.Dictionary<?,?> properties)
Sets the public interfaces under which this component should be registered
in the OSGi service registry.
|
AdapterComponent |
setInterface(java.lang.String serviceName,
java.util.Dictionary<?,?> properties)
Sets the public interface under which this component should be registered
in the OSGi service registry.
|
AdapterComponent |
setPropagate(boolean propagate)
Sets if the adaptee service properties should be propagated to the adapter service consumer (true by default)
|
AdapterComponent |
setScope(Component.ServiceScope scope)
Sets the component scope.
|
AdapterComponent |
setServiceProperties(java.util.Dictionary<?,?> serviceProperties)
Sets the service properties associated with the component.
|
getComponentDeclaration, getDependencyManager, getInstance, getInstances, getServiceProperties, getServiceRegistration
AdapterComponent setScope(Component.ServiceScope scope)
AdapterComponent setImplementation(java.lang.Object implementation)
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:
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
removedsetImplementation
in interface Component
implementation
- the implementationComponentStateListener
AdapterComponent add(Dependency... dependencies)
AdapterComponent remove(Dependency d)
AdapterComponent add(ComponentStateListener listener)
AdapterComponent remove(ComponentStateListener listener)
AdapterComponent setInterface(java.lang.String serviceName, java.util.Dictionary<?,?> properties)
setInterface
in interface Component
serviceName
- the name of the service interfaceproperties
- the properties for this serviceAdapterComponent setInterface(java.lang.String[] serviceNames, java.util.Dictionary<?,?> properties)
setInterface
in interface Component
serviceNames
- the names of the service interfaceproperties
- the properties for these servicesAdapterComponent setInterface(java.lang.Class<?> serviceName, java.util.Dictionary<?,?> properties)
setInterface
in interface Component
serviceName
- the name of the service interfaceproperties
- the properties for this serviceAdapterComponent setInterface(java.lang.Class<?>[] serviceNames, java.util.Dictionary<?,?> properties)
setInterface
in interface Component
serviceNames
- the names of the service interfaceproperties
- the properties for these servicesAdapterComponent setAutoConfig(java.lang.Class<?> clazz, boolean autoConfig)
setAutoConfig
in interface Component
clazz
- the class (from the list above)autoConfig
- false
to turn off auto configurationAdapterComponent setAutoConfig(java.lang.Class<?> clazz, java.lang.String instanceName)
setAutoConfig
in interface Component
clazz
- the class (from the list above)instanceName
- the name of the instance to inject the class intosetAutoConfig(Class, boolean)
AdapterComponent setServiceProperties(java.util.Dictionary<?,?> serviceProperties)
setServiceProperties
in interface Component
serviceProperties
- the propertiesAdapterComponent setCallbacks(java.lang.String init, java.lang.String start, java.lang.String stop, java.lang.String destroy)
setCallbacks
in interface Component
init
- the name of the init methodstart
- the name of the start methodstop
- the name of the stop methoddestroy
- the name of the destroy methodAdapterComponent setCallbacks(java.lang.Object instance, java.lang.String init, java.lang.String start, java.lang.String stop, java.lang.String destroy)
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.
setCallbacks
in interface Component
AdapterComponent setFactory(java.lang.Object factory, java.lang.String createMethod)
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
.setFactory
in interface Component
factory
- the factory instance or classcreateMethod
- the name of the create methodAdapterComponent setFactory(java.lang.String createMethod)
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.
setFactory
in interface Component
createMethod
- the name of the create methodAdapterComponent setComposition(java.lang.Object instance, java.lang.String getMethod)
Object[]
.setComposition
in interface Component
instance
- the instance that has the methodgetMethod
- the method to invokeAdapterComponent setComposition(java.lang.String getMethod)
Object[]
.setComposition
in interface Component
getMethod
- the method to invokeAdapterComponent setDebug(java.lang.String label)
AdapterComponent setAdaptee(java.lang.Class<?> service, java.lang.String filter)
service
- the service interface to apply the adapter tofilter
- the filter condition to use with the service interfaceAdapterComponent setAdapteeField(java.lang.String autoConfig)
autoConfig
- the name of the member to inject the service intoAdapterComponent setAdapteeCallbacks(java.lang.String add, java.lang.String change, java.lang.String remove, java.lang.String swap)
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 swapAdapterComponent setAdapteeCallbackInstance(java.lang.Object callbackInstance)
callbackInstance
- the instance to invoke the callbacks on (null by default, meaning the callbacks have to be invoked on the adapter itself)AdapterComponent setPropagate(boolean propagate)
propagate
- true if the adaptee service properties should be propagated to the adapter service consumers.
The provided adapter service properties take precedence over the propagated adaptee service properties.
It means an adaptee service property won't override an adapter service property having the same name.