SeamFramework.orgCommunity Documentation

Chapter 3. How to make OSGi easy peasy

3.1. CDI usage in bean bundles
3.2. Injecting easiness in OSGi world
3.2.1. Service, implementation, instance and registration
3.2.2. OSGi services injection
3.2.3. OSGi service automatic publishing with @Publish annotation
3.2.4. Clearly specify a service implementation
3.2.5. Contextual services
3.2.6. The registration
3.2.7. Service registry
3.2.8. The OSGiServiceUnavailableException exception
3.3. CDI-OSGi events
3.3.1. CDI container lifecycle events
3.3.2. Bundle lifecycle events
3.3.3. Service lifecyle events
3.3.4. Application dependency validation events
3.3.5. Intra and inter bundles communication events
3.4. OSGi utilities
3.4.1. From the current bundle
3.4.2. From external bundle
3.5. CDI-OSGi, what else ?
3.5.1. Getting service references and instances
3.5.2. Publishing a service implementation
3.5.3. Obtaining the Bundles and BundleContexts

Everything possible in CDI applications is possible in bean bundle. They can take advantage of injection, producers, interceptors, decorators and alternative. But influence boundary of the CDI compliant container stay within the bean bundle for classic CDI usages. So external dependencies cannot be injected and interceptor, decorator or alternative of another bean bundle cannot be used (yet interceptors, decorators and alternatives still need to be declares in the bean bundle bean.xml file).

That is all we will say about classic CDI usages, please report to CDI documentation for more information.

CDI-OSGi provides more functionality using CDI in a OSGi environment.

It mainly focuses on the OSGi service layer. It addresses the difficulties in publishing and consuming services. CDI-OSGi allows developers to publish and consume OSGi services as CDI beans. However, since OSGi services are dynamic there are some differences with classic bean injection. This section presents how OSGi services can be published and consumed using CDI-OSGi.

CDI-OSGi also provides utilities for event notification and communication in and between bundles as well as some general OSGi utilities.

Examples use this very sophisticated service interface:

public interface MyService {
    void doSomething();
}

There are two ways to obtain a service instances using CDI-OSGi: direct injection and programmatic lookup.

There might be multiple implementations of the same service. It is possible to provide properties to an implementation in CDI-OSGi. This qualification is available both during publishing and consuming.

There are two ways for qualifying: a CDI like and a OSGi like, both are presented below.

A registration object represent all the bindings between a service contract class and its OSGi ServiceRegistrations. With this object it is possible to navigate through multiple implementations of the same service, obtain the corresponding Service<T> or unregister these implementations.

CDI-OSGi makes heavy usage of CDI events. These events allow CDI-OSGi to do its work. Events are important for the running of CDI-OSGi itself but they can also be used by client bean bundles to perform some operations or override some normal CDI-OSGi behavior.

This section shows the numerous events provided by CDI-OSGi and the ways to use them.

Ten events might be fired during a bundle lifecycle. They represent the possible states of a bundle and monitor the changes occurred in bundle lifecycles. These events carry the Bundle object from where the event comes so developers can retrieve useful information (such as the bundle id).

Three events might be fired during a service lifecycle. They represent the possible states of a service and monitor the changes occurred in service lifecycles. These events carry the BundleContext object from where the event comes and the ServiceReference object corresponding to the service so developers can retrieve useful information (such as the registering bundle) and acces useful actions (such as register or unregister services).

CDI-OSGi provide some facilities for OSGi usage. It allows to obtain, by injection, some of the useful objects of the OSGi environment.

Here there are some examples that concretely show what CDI-OSGi is avoiding to the developer: