Adobe Experience Manager - Create an OSGI Configuration
In this article, let's create an OSGi configuration, configure it and use it in AEM code. So now let's get started with the creation of an OSGi configuration.
Technical details and Assumptions:
All the following details are tested in AEM version 6.5.8, Java version 11.0.2
Creation of OSGi configuration:
To create an OSGi configuration we need to create an ObjectClassDefinition. I have included a sample OCD configuration, which can be used as a reference to create one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mysite.core.config; | |
import org.osgi.service.metatype.annotations.AttributeDefinition; | |
import org.osgi.service.metatype.annotations.ObjectClassDefinition; | |
@ObjectClassDefinition( | |
name = "OSGI Configuration", | |
description = " OSGI Configuration" | |
) | |
public @interface OSGIClientConfiguration { | |
@AttributeDefinition(name = "config", description = "OSGI Configuration") | |
public String getConfig() default "/content" ; | |
} |
The next step would be to create an interface and an implementation that can help fetch the OSGi configurations.
Interface:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mysite.core.config; | |
public interface OSGIClient { | |
public String execute(); | |
} |
Implementation:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mysite.core.config; | |
import java.util.Objects; | |
import org.apache.commons.lang3.StringUtils; | |
import org.osgi.service.component.annotations.Activate; | |
import org.osgi.service.component.annotations.Component; | |
import org.osgi.service.metatype.annotations.Designate; | |
/** | |
* OSGi client service provides configuration. | |
*/ | |
@Component(name="com.mysite.core.config.OSGIClient", service = OSGIClient.class,immediate = true) | |
@Designate(ocd = OSGIClientConfiguration.class) | |
public class OSGIClientImpl implements OSGIClient{ | |
private OSGIClientConfiguration configuration; | |
@Activate | |
protected void activate(OSGIClientConfiguration configuration) { | |
this.configuration = configuration; | |
} | |
@Override | |
public String execute() { | |
String config = configuration.getConfig(); | |
if(Objects.isNull(config)) | |
config = StringUtils.EMPTY; | |
return config; | |
} | |
} |
Let's try to use the OSGi configuration created so far in Models/Servlets. For demonstration purposes, I used AEM Models here, but the same can be implemented in Servlets too.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mysite.core.config; | |
import org.apache.sling.models.annotations.injectorspecific.OSGiService; | |
import javax.annotation.PostConstruct; | |
import org.apache.sling.api.resource.Resource; | |
import org.apache.sling.api.resource.ResourceResolver; | |
import org.apache.sling.models.annotations.Default; | |
import org.apache.sling.models.annotations.Model; | |
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy; | |
import org.apache.sling.models.annotations.injectorspecific.OSGiService; | |
import org.apache.sling.models.annotations.injectorspecific.SlingObject; | |
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue; | |
@Model(adaptables = Resource.class) | |
public class OSGIModel { | |
@OSGiService | |
private OSGIClient OsgiClient; | |
private String osgiConfig; | |
public void postConstruct() throws Exception { | |
this.osgiConfig = OsgiClient.execute(); | |
} | |
public String getOsgiconfig() { | |
return osgiConfig; | |
} | |
} |
Now that we have created the OSGi configuration. Once building the code, we should be able to see the OSGi configuration in the web console (http://localhost:4502/system/console/configMgr)
Here is an example to have configuration from the code base,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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" | |
getConfig="/ContentConfig" | |
/> |
Now, let's write a simple component to use the Model and get the configuration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div data-sly-use.ogsi="com.mysite.core.config.OSGIModel"> | |
{ogsi.osgiConfig} | |
</div> | |
<div data-sly-test="${wcmmode.edit}" class="${wcmmode.edit ? 'cq-placeholder' : ''}" data-emptytext="OSGI Confirguration"></div> |
Now check the output of the component,
- created an OSGi configuration and validated the same in the web console, where we can edit it too
- used the created OSGi configuration in an AEM Model to take the configuration to the UI using a simple component
Hope this article helps in creating an OSGi configuration from scratch in AEM ❤️. Feel free to comment and suggestions/improvements :P
Thanks for reading :)
References:
1) https://computer-lords.blogspot.com/2021/12/adobe-experience-manager-configure-osgi.html
Comments
Post a Comment