OSGI Components Configuration

13/06/2021 / BY / IN OSGI Components and Services / 1 Comments

Welcome to part 6 of the lecture on OSGI, OSGI Component Configuration.

After reading this article, you should be able to

  • Declare component lifecycle methods.
  • Make your component configurable.

We will not review the older list five ways of configuring components. If
that is what is used in your project you should better read documentation.

Configuration Meta Type

In OSGI, to make a component configurable, the
developer defines a certain data structure called MetaType.

That MetaType has two important parts

  1. Object class Definition
  2. Designate

Object Class Definition defines Attributes, that determines the configurable properties of a component and

Designate binds the Object Class Definition to the component.

In a bundle this data structure exists as an XML document that is a separate
XML document plus entries in the main component configuration XML.
In the Java code this structure is defined using several annotations.

Configuration Manager

Configuration Manager allows editing  designated component configurations  manually.

Object Class Definition

@ObjectClassDefinition
public @interface StringGeneratorConfig {

@AttributeDefinition(name = "Parts", description = "Elements the string is generated from",
type = AttributeType.STRING)
String[] parts() default { "Lorem", "ipsum", "dolor", "consectetur", "adipiscing" };


@AttributeDefinition(name = "Length", type = AttributeType.INTEGER)  int length() default 15;


@AttributeDefinition(name = "Trim to Fit", type = AttributeType.BOOLEAN)  boolean truncateToFit() default false;

}
  1. The first element returns an array type, The string is generated from by the StringGenerator & by default those are first five words of the lorem ipsum text.
  2. The second element returns an integer value that will be the length of the string generated by the method & by default it is 15.
  3. The third element returns a Boolean value, it indicated whether the string is truncated if it is longer than the desired number of characters.

Then you see that I annotated each element with AttributeDefinition annotation that annotation makes an element an OSGI attribute definition, you can see that I defined the name, description & type for each attribute.

The name & description are shown in the configuration edit dialog. The type attribute determines the widget that is used to add the field in the edit dialog, a text field or a checkbox. In most cases AEM can guess which widget to use from the element return type, a type attribute can be used to override that guess if it is incorrect.


Designate Annotation & Activation Method

@Component (...)
@Designate(ocd = StringGeneratorConfig.class)
public class StringGeneratorImpl implements StringGenerator {
. . .

@Activate
@Modified
public void activate(StringGeneratorConfig config) {
this.parts = config.parts();  this.length = config.length();
this.truncateToFit = config.truncateToFit();
}


. . .


After we created and Object Class Definition, We need to connect the component to it, for that we go to the component class and add another annotation to it that is Designate. The Object Class Definition is specified as an attribute of that annotation.

We have declared the configuration, bound the component to that configuration, now one more step remains – read the configuration values in the component class.

The environment passes the configuration to the component in two cases

  1. When the component is activated or
  2. When the configuration is updated

Can attribute change, configuration is deleted and so on.

For the one needs to do the following

First add a method to the component that takes an instance of the object class definition as the argument, add the code that reads the configuration attribute in the method and for eg. Store the values in the instance variables and then annotate with method Activate & Modified annotation.

Usually, the logic for both the Activation & Modification is the same so the same method can be used. If the logic is different one can have separate methods each annotate with the respective annotation.

Note: that Activate & Modified methods can take other arguments than the configuration class such as Map or component context but those are supported for the compatibility with older prior list with these approaches, so now the component is fully configurable.

Summary

• Component configuration can be added in the configuration manager.
• To make a component configurable one must declare a meta type.
• Meta type consist of object class definition & designate elements.
• Object Class Definition is declared as annotation type interface with attribute definitions.
• Designate is an annotation in the component class.
• Attribute values are passed to the component as arguments in Activate & Modified methods.

Hope you enjoyed this article, I would like to suggest, Kindly watch video below for practical session, Thank you for your attention! In the next part we will learn pre-defined configuration & Run Mode. Click here to visit page!

Leave a Reply