public interface BundleComponent extends Component
AdapterComponent
, are used to "extend"
existing bundles, and can publish an adapter services based on the existing bundle.
An example would be implementing a video player which adapters a resource bundle having
some specific headers.
When you create a bundle adapter component, it will be applied to any bundle that matches the specified bundle state mask as well as the specified ldap filter used to match the bundle manifest headers. The bundle adapter will be registered with the specified bundle manifest headers as service properties, plus any extra properties you suppl. If you declare a bundle field in your bundle adapter class, it will be injected it will be injected with the original bundle.
public class Activator extends DependencyActivatorBase { &Override public void init(BundleContext context, DependencyManager dm) throws Exception { BundleComponent bundleComponent = createBundleComponent() .setFilter(Bundle.ACTIVE, "(Video-Path=*)") .setInterface(VideoPlayer.class.getName(), null) .setImplementation(VideoPlayerImpl.class); dm.add(bundleComponent); } } public interface VideoPlayer { void play(); } public class VideoPlayerImpl implements VideoPlayer { volatile Bundle bundle; // injected String path; void start() { path = bundle.getHeaders().get("Video-Path"); } void play() { ... } }
When you use callbacks to get injected with the bundle, the "add", "change", "remove" callbacks support the following method signatures:
(Bundle)
(Object)
(COmponent, Bundle)
Component.ServiceScope
Modifier and Type | Method and Description |
---|---|
BundleComponent |
add(ComponentStateListener listener)
Adds a component state listener to this component.
|
BundleComponent |
add(Dependency... dependencies)
Adds dependency(ies) to this component, atomically.
|
BundleComponent |
remove(ComponentStateListener listener)
Removes a component state listener from this component.
|
BundleComponent |
remove(Dependency d)
Removes a dependency from the component.
|
BundleComponent |
setAutoConfig(java.lang.Class<?> clazz,
boolean autoConfig)
Configures auto configuration of injected classes in the component instance.
|
BundleComponent |
setAutoConfig(java.lang.Class<?> clazz,
java.lang.String instanceName)
Configures auto configuration of injected classes in the component instance.
|
BundleComponent |
setBundleCallbackInstance(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)
|
BundleComponent |
setBundleCallbacks(java.lang.String add,
java.lang.String change,
java.lang.String remove)
Sets the callbacks to invoke when injecting the bundle into the adapter component.
|
BundleComponent |
setBundleFilter(int bundleStateMask,
java.lang.String bundleFilter)
Sets the bundle state mask and bundle manifest headers filter.
|
BundleComponent |
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.
|
BundleComponent |
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.
|
BundleComponent |
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.
|
BundleComponent |
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.
|
BundleComponent |
setDebug(java.lang.String label)
Activate debug for this component.
|
BundleComponent |
setFactory(java.lang.Object factory,
java.lang.String createMethod)
Sets the factory to use to create the implementation.
|
BundleComponent |
setFactory(java.lang.String createMethod)
Sets the factory to use to create the implementation.
|
BundleComponent |
setImplementation(java.lang.Object implementation)
Sets the implementation for this component.
|
BundleComponent |
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.
|
BundleComponent |
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.
|
BundleComponent |
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.
|
BundleComponent |
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.
|
BundleComponent |
setPropagate(boolean propagate)
Sets if the bundle manifest headers should be propagated to the bundle component adapter service consumer (true by default).
|
BundleComponent |
setScope(Component.ServiceScope scope)
Sets the component scope.
|
BundleComponent |
setServiceProperties(java.util.Dictionary<?,?> serviceProperties)
Sets the service properties associated with the component.
|
getComponentDeclaration, getDependencyManager, getInstance, getInstances, getServiceProperties, getServiceRegistration
BundleComponent setScope(Component.ServiceScope scope)
BundleComponent 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
BundleComponent add(Dependency... dependencies)
BundleComponent remove(Dependency d)
BundleComponent add(ComponentStateListener listener)
BundleComponent remove(ComponentStateListener listener)
BundleComponent setInterface(java.lang.String serviceName, java.util.Dictionary<?,?> properties)
setInterface
in interface Component
serviceName
- the name of the service interfaceproperties
- the properties for this serviceBundleComponent setInterface(java.lang.String[] serviceNames, java.util.Dictionary<?,?> properties)
setInterface
in interface Component
serviceNames
- the names of the service interfaceproperties
- the properties for these servicesBundleComponent setInterface(java.lang.Class<?> serviceName, java.util.Dictionary<?,?> properties)
setInterface
in interface Component
serviceName
- the name of the service interfaceproperties
- the properties for this serviceBundleComponent setInterface(java.lang.Class<?>[] serviceNames, java.util.Dictionary<?,?> properties)
setInterface
in interface Component
serviceNames
- the names of the service interfaceproperties
- the properties for these servicesBundleComponent setAutoConfig(java.lang.Class<?> clazz, boolean autoConfig)
setAutoConfig
in interface Component
clazz
- the class (from the list above)autoConfig
- false
to turn off auto configurationBundleComponent 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)
BundleComponent setServiceProperties(java.util.Dictionary<?,?> serviceProperties)
setServiceProperties
in interface Component
serviceProperties
- the propertiesBundleComponent 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 methodBundleComponent 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
BundleComponent 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 methodBundleComponent 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 methodBundleComponent setComposition(java.lang.Object instance, java.lang.String getMethod)
Object[]
.setComposition
in interface Component
instance
- the instance that has the methodgetMethod
- the method to invokeBundleComponent setComposition(java.lang.String getMethod)
Object[]
.setComposition
in interface Component
getMethod
- the method to invokeBundleComponent setDebug(java.lang.String label)
BundleComponent setBundleFilter(int bundleStateMask, java.lang.String bundleFilter)
bundleStateMask
- the bundle state mask to applybundleFilter
- the filter to apply to the bundle manifestBundleComponent setBundleCallbacks(java.lang.String add, java.lang.String change, java.lang.String remove)
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 removeBundleComponent setBundleCallbackInstance(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)BundleComponent setPropagate(boolean propagate)
propagate
- true if the bundle manifest headers should be propagated to the adapter service consumers