Reading sling:OsgiConfig into @ObjectClassDefinition

Adilmo

20-08-2020

We have a OSGI Service (using R7 DS annotation). We are using OCD as innerclass within the service.

Also, we would like the component to read properties from the predefined sling:osgiconfig nodes in JCR.

Configuration policy is defined as required.

When the component/service load it goes into a "no config" state.

Need help to read these configs from sling:osgiConfig nodes.

Accepted Solutions (1)

Accepted Solutions (1)

Arun_Patidar

MVP

22-08-2020

Why do you want to read sling:OsgiConfig values inside OCD?

OCD is used to declare the OSGi config structure.

 

You can declare OCD the same as config values but in the end, the value will be read from the defined value in repository.

 

Example

https://github.com/arunpatidar02/aem63app-repo/blob/master/java/SimpleScheduledTask.java

Answers (3)

Answers (3)

Shashi_Mulugu

MVP

24-08-2020

Can you try as below, Firstly you have configuration policy as optional, and second, you are not declaring right interface for @component annotation.

 

Corrected code below:-

 

 

@component(immediate = true, service = DataConfig.class, property = {
Constants.SERVICE_DESCRIPTION + "=Data Config",
Constants.SERVICE_VENDOR + "=ABC"
})
@ServiceDescription("DataConfig Service")
@Designate(ocd = DataConfigImpl.Configuration.class)
public final class DataConfigImpl implements DataConfig {
@ObjectClassDefinition(name = "DataConfigImpl")
public @interface Configuration {
@AttributeDefinition(name = "paths", description = "Paths", type = AttributeType.STRING) String my_paths() default "";
}
@SuppressWarnings("rawtypes") private Dictionary props = null;
@activate Configuration configuration;
@activate ComponentContext componentContext;
@activate @MODIFIED protected void activate(Config c) {
try {

this.configuration = c;

}
}
}

Jörg_Hoh

Employee

22-08-2020

You can find a working example at https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/0ffd40a15460f8be8d3591e17d5648184f...

 

It features an OSGI service, which uses an inner class for configuration; it should behave according to your requirements.

Could you please provide any method to debug this? We have sling:osgiconfig under the /apps/*/config folder but component never recognize these properties. Please note that when we explicitly open the config, it populates with the sling:osgiconfig correctly and when we save it gets bound. Problem only happens when the bundle starts up, at that point component never finds the config and goes into "no config" state. Please advise and help
I am still having this issue where sling:OsgiConfig is not mapped to my OCD object, even through the PID matches.
Can you post the code of that class in question somwhere?
@component(immediate = true, configurationPolicy = ConfigurationPolicy.OPTIONAL, service = DataConfigImpl.class, property = { Constants.SERVICE_DESCRIPTION+"=Data Config", Constants.SERVICE_VENDOR+"=ABC" } ) @ServiceDescription("DataConfig Service") @Designate(ocd = DataConfigImpl.Configuration.class) public final class DataConfigImpl implements DataConfig { @ObjectClassDefinition(name = "DataConfigImpl" ) public @interface Configuration { @AttributeDefinition( name = "paths", description = "Paths", type = AttributeType.STRING ) String my_paths() default ""; } @SuppressWarnings("rawtypes") private Dictionary props = null; @activate Configuration configuration; @activate ComponentContext componentContext; @activate @MODIFIED protected void activate() { try { } } }
Hi Jorg..... does ConfigAdmin reads sling:OsgiConfig properties to populate its persistence properties data ?? Is there a way to populate configadmin property set before hand so that when component loads we get the configuration ?

The configadmin does all what you expect. Things to check:

  • Is the PID correct? It should be the full qualified class name of the service (and not the one of the configuration class)
  • When you update the config, do you see it picked up by the JCR Installer? (Check the logs for it)
  • Use the webconsole and provide some configuration for it. Does it then work as expected? And normally you will find these configured values in /apps/system ... Compare the node names and the structure with the one you are using.
Hi Jorg.... Our sling:OsgiConfig are under /apps/*/config folder.... they are loaded as sling:OsgiConfig nodes. These nodes are not able to provide initial configuration to the component. (No activate methods called with configuration.policy=required). However, we can open these nodes with webconsole configuration. The same values populates on the console and when we save the configs, they get saved under /apps/*/config folder as jcr:content under the respective sling:osgiconfig nodes. Once this happens, component is activated and even when we restart bundle component gets activated. Basically, component cannot get activated with initial sling:osgiconfig nodes ...it is only when the jcr:content is created under those nodes that the component gets the configuration and gets activated. Is it a good idea to package sling:osgiconfig with jcr content nodes in a content package ... so that cmponent gets activated by default.
Is there any reference OSGI configurator implementation for AEM ?? I need to add the configuration is configadmin before the component is activated .. kindly advise... as of now component activation is not fetching the configadmin configs (created through sling:osgiconfig)

Shashi_Mulugu

MVP

20-08-2020

@Adilmo  Can you go to /system/console/components or /system/console/configuration and see if you are able to see your service class? If yes try to click/open and see what is the PID it is populating..

 

Even you can fill the configuration from osgi console once, copy the content to your local config file and then delete the file