A basic structure in java is a bundle which helps in modular development (Core benefit of the framework). Bundle basically a Jar file consists of Java source.
A bundle participates in a life cycle (Start,stop,etc). When the container is in a life cycle, all its java files has to be. So how OSGi achieves this?
Let us discuss in detail about how Java files participate in OSGi life cycle in this post.
A plain java file participate into OSGi life cycle using component.
Declarative services or Service Component Runtime SCR is an extender that creates components from an XML resource that is defined your bundle.
Each java file will be injecting a component descriptor xml to Service-Component header of the bundle. A reference to the component description file is entered in the MANIFEST.MF file via the Service-Component property.
A sample component XML will look like below example:
<?xml version="1.0" encoding="UTF-8"?>
<component name="sample.component" immediate="true">
<implementation class="sample.SampleComparator" />
<property name="service.description" value="Sample Comparator Service" />
<property name="service.vendor" value="Apache Software Foundation" />
<provide interface="java.util.Comparator" />
Properties in Component XML
name – Uniquely identifies this component and is also used to retrieve optional configuration from the Configuration Admin Service (if available).
immediate – Defines whether the component is to be instantiated immediately (true) or on-demand (false).
implementation.class – The fully qualified name of the class implementing the component. This class must be public and have a public default constructor for it to be usable by the Service Component Runtime. This class is not required to be exported and may as well be private to the bundle. In fact, you will generally not export the component implementation class.
property – These elements define configuration properties to the component. These properties are available through the ComponentContext which is presented to the component in the activate method (see below).
service – If the component is to be registered as a service, the service names are listed in provide elements inside the service element. These names will generally be interfaces and must be visible to other bundles for the service to be usable. In this sample, the service is java.util.Comparator class, which is always visible.
The developer no need to worry about all these while declaring an component in OSGi.
Here comes various bundling tools which will does the task automatically.
We just have to specify the annotation called @Component.
Various properties of @Component annotation are:
Declarative service descriptor is getting generated or not, by default it will get generated
Declarative service specification version is being set here
Whether Metatype Service data is generated or not. If this parameter is set to true Metatype Service data is generated in the metatype.xml file for this component. Otherwise no Metatype Service data is generated for this component. [This we can cover in another post]
SCR Descriptor: component.enabled
Whether the component is enabled when the bundle starts
SCR Descriptor: component.immediate
Whether the component is immediately activated
Once the bundle gets activated, the Declarative service will take care of activating, resolving dependency (DS handles dynamic dependencies), and registering the service (A component can be registered as a service using @Service annotation. This post explains it well).
There are few more annotations associated with a component. Those are:
@Activate: Do something at the time of component activation, mostly initializing the objects or obtaining the services
@Deactivate: Do something at the time of component activation, releasing objects
A component is a standalone entity of a OSGi container. It can not be accessed or communicated with other components right away. In order to achieve this, we need to define this as a service, we use @Service for the same.
These are all about the OSGi components, in next posts we can discuss in detail about OSGi services and how service bindings happening and all.
You may also interested in: