OSGI Configurations are easy way to configure parameters based on run mode. This blogs details on how to create OSGI Configuration with different property attributes and read them through the code.
In this example, I have narrated a way to define Adobe Launch Script for various sites in OSGI configuration and dynamically read the script value based on the content site requested and add to the page template.
A site name is derived from the AEM page url structure
ex: /content/sitea/us/en - Site name is sitea. The Adobe Launch script in the OSGI configuration will be defined as
sitea.launch.script = "<source to the launch script>"
Here are the easy steps to implement them through project:
create the config file Under ui.config,
com.demo.core.services.OSGiConfig.config.cfg.json with following values
{
"sitea.launch.script":"</assets.adobe.com/......xxxxx..min.js",
"siteb.launch.script":"</assets.adobe.com/......xxxxx..min.js"
}
demo.launch.script value will map to environment specific path for each run mode.
Create a new java class OSGiConfig.java with below code
In you page component model, write code to retreive the site name based on the url.
Create the property key as launchkey = siteName + "launch.script".
Create a getter method for getLaunchScript as follows
public String getLaunchScript()
{
return osgiConfig.getPropertyValue(launchKey);
}
In page component, customheaderlibs.html at the entry for including the scripttag
<script src="${pageModel.getLaunchScript}" async></script>