April 16, 2020
Estimated Post Reading Time ~

Create Custom OSGi Configuration in AEM

OSGI (Open Service Gateway Initiative) is a major building block in aem architecture which makes it modular. In AEM, there is a common need of creating custom OSGi configuration. These Configurations can be a single value,multi-value,drop down list and checkbox like property.

In this tutorial i have tried to cover all the available options for creating custom OSGi configuration in aem.
  • How to Set and Get OSGI configuration values using Text Field.
  • How to Set and Get OSGI configuration values using Drop Down List.
  • How to Set and Get OSGI configuration values using CheckBox.
  • How to Set and Get OSGI configuration values using Multi Field.
Set and Get OSGI configuration values using Textfield:
When user wants to get and set single value in osgi configuration, Text Field is preferred.

Setting Values:
@Property(label="Name of the Author",value = "author")
@Property(label="Age of the Author",intValue = 15)

Getting Values:
private static final String AUTHOR_NAME = "author.value";
private static final String AUTHOR_AGE = "author.age";

value: This property indicates default value of the configuration.
intValue: If you want to take only integer value ,use this property to set default value.

Note: @Property annotation helps us to define the properties in felix console.

Set and Get OSGI configuration values using Drop Down List:
Drop Down list is a very common requirement that a developer encounters, when customer wants to display list of countries, states, gender etc to choose from felix console.

Note: User can add as many options in the list , but select only once at a time.

Setting Values:
@Property(
label = "Author Gender",
description = "Describe Author Gender",
options = {
@PropertyOption(name = "Male", value = "1. Male"),
@PropertyOption(name = "Female", value = "2. Female")
},
value = "Female")

Getting Values:
private static final String AUTHOR_GENDER = "author.gender";

Set and Get OSGI configuration values using CheckBox :
The checkBox is a good solution when number of options are limited, and customers wants to select single or multiple values from felix console.

Note: The Value of this property is boolean ,we can set default value by the attribute boolValue.

Setting Values:
@Property(label = "CheckBox Property " ,boolValue = false,description = "please Check the Property")
Getting Values:
public static final String CHECKBOX_PROPERTY = "checkBox.property";
Set and Get OSGI configuration values using Multi-Field :
Multifield Components is the most preffered component by developers when their is a requirement of creating a custom configuration which consist dynamic number of fields. We can add as many values in it as per our requirement

@Property(value={"English", "Hindi"}, unbounded = PropertyUnbounded.ARRAY, label = "Subjects", cardinality = 50, description = "Example for Multi field config")
private static final String MULTI_FIELD = "multifield";

Now in order to fetch the values we require two methods:
Activate: This method is called only once when the bundle is activated.
Modified: When we made the changes in our configurations, then we need to fetch the values from this method because the bundle is already activated.

@Activate
protected void activate(@SuppressWarnings("rawtypes") final Map context) {
this.name = PropertiesUtil.toString(context.get(AUTHOR_NAME), "");
this.gender = PropertiesUtil.toString(context.get(AUTHOR_GENDER),"");
this.checkbox = PropertiesUtil.toBoolean(context.get(CHECKBOX_PROPERTY),true);
this.multiString = PropertiesUtil.toStringArray(context.get(MULTI_FIELD));
this.age = PropertiesUtil.toInteger(context.get(AUTHOR_AGE),12);

}

@Modified
protected void modified(ComponentContext context){
this.name = PropertiesUtil.toString(
context.getProperties().get(AUTHOR_NAME), "");
this.gender = PropertiesUtil.toString(
context.getProperties().get(AUTHOR_GENDER), "");
this.checkbox = PropertiesUtil.toBoolean(
context.getProperties().get(CHECKBOX_PROPERTY), true);
this.multiString = PropertiesUtil.toStringArray(
context.getProperties().get(MULTI_FIELD));
this.age = PropertiesUtil.toInteger(
context.getProperties().get(AUTHOR_AGE),12);
}


Testing OSGI Configurations:
Go to Felix Console.
Select the bundle that you want to Verify or Configure.



You can find the complete code here.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.