February 18, 2025
AEM

OSGI Configurations Using OCD

In our application, we might use 3rd party API details. Usually these details will be different for each environment. For dev environment, they might give separate API, username, password (or client id, secret values). These values will differ for stage, prod environments.

Same case for other configurations. We can maintain these configurations as OSGI configuration. So that we can have separate configuration for each environments. Best way of maintaining the OSGI configurations is via OCD(Object Class Definition). Refer the below code. This code acts as OSGI configuration contract. This is not an java interface. This is a different annotation: @interface

@interface Config {
 
        @AttributeDefinition(
                name = "Furniture ID",
                description = "Enter ID"
        )
        String furnitureID() default "fur123";
        
        @AttributeDefinition(
                name = "Company ID",
                description = "Enter Company ID"
        )
        String companyID() default "123";
        
        @AttributeDefinition(
                name = "Company Name",
                description = "Enter Name"
        )
        String companyName() default "dummyCompany" ;
 

}

Annotate this config with the annotation @ObjectClassDefinition

	@ObjectClassDefinition(
            name = "Furniture Company Details",
            description = "Configuration for adding the furniture company details"
    )

Now in the OSGI service implementation(in this case: com.chetasmind.tutor.core.config.impl.FurnitureConfigImpl)
after the @Component annotation, add one more annotation @Designate(ocd=FurnitureConfigImpl.Config.class)
this points to our config class. Now Config is registered with OSGI service as OSGI configuration definition. In the activate method, read the config values .

@Activate
@Modified
protected void activate(Config config) {
	 
		log.debug("------------- activate method of FurnitureConfigImpl");
		furnitureID = config.furnitureID();
		companyID = config.companyID();
		companyName = config.companyName();
	 
}

At the end, add getter methods for the variables: furnitureID, companyID, companyName. So that you can get these values from your java logic(From Sling model, servlet or from service class)

Now, you need to create config xml file. Create a xml file named: com.chetasmind.tutor.core.config.impl.FurnitureConfigImpl for each environment specific config folders.

This file will have these details

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="sling:OsgiConfig"
    furnitureID="1234"
    companyID="comp123"
    companyName="Alexa"/>

Once you build the application, check your local AEM instance. Path: /apps/chetasmind/osgiconfig/config You should see your config file

Accessing this OSGI config from Sling model class
Create a config object in sling Model

Note: To access the OSGI config from Sling servlet, use @Reference annotation for the object: private FurnitureConfig furnitureConfig;

@OSGiService
private FurnitureConfig furnitureConfig;

In the init method, get the OSGI config values
 // Testing the Run mode Configuration
 logger.debug("---------- Testing the Run mode Configuration ------");
        
 logger.debug("Company ID: {}",furnitureConfig.getCompanyID());
 logger.debug("Company Name: {}",furnitureConfig.getCompanyName());
 logger.debug("Furniture ID: {}",furnitureConfig.getFurnitureID());

Refer my github project to get the entire project code. Class name: com.chetasmind.tutor.core.config.impl.FurnitureConfigImpl

Accessed the OSGI configuration from the sling model: com.chetasmind.tutor.core.models.HelloWorldModel

Leave a Reply

Your email address will not be published. Required fields are marked *