Expand my Community achievements bar.

SOLVED

Reading sling:OsgiConfig into @ObjectClassDefinition

Avatar

Level 4

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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



Arun Patidar

View solution in original post

36 Replies

Avatar

Community Advisor

yes, I get those when bundle is updated -

 

logger.info("PageJSONServletServiceImpl:activate:refrenceProperties {}",refrenceProperties.length);

 

01.09.2020 15:32:03.228 *INFO* [Background Update com.acc.aem64.AEM64App.core (545)] com.acc.aem64.core.services.impl.PageJSONServletServiceImpl PageJSONServletServiceImpl:activate:refrenceProperties 2



Arun Patidar

Avatar

Employee Advisor
and how does your configuration look like? how do you deploy it? Is the PID for the configuration you deploy really "com.acc.aem64.core.services.impl.PageJSONServletServiceImpl"?

Avatar

Community Advisor

Hi @Jörg_Hoh 
I deploy via code i.e. apps package and config look like below :

 

Arun_Patidar_0-1598991652476.png

 

Arun_Patidar_1-1598991686573.png

 

 

 



Arun Patidar

Avatar

Employee Advisor
well, that configuration looks fine and I don't see why it wouldn't work so if @Adilmo follows this, everything should be fine.

Avatar

Level 4
thanks Arun, Jorg for patiently listening to my query ..... actually the issue was with my understanding .... I was thinking that when the component comes up initially..... it will have the properties ... However, it is only after the UI package containing the config nodes get deployed it activates the component again with fresh set of configs. ... thanks for all the help ...

Avatar

Employee Advisor
glad that you were able to resolve it.

Avatar

Employee Advisor

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.

Avatar

Level 4
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

Avatar

Level 4
I am still having this issue where sling:OsgiConfig is not mapped to my OCD object, even through the PID matches.

Avatar

Employee Advisor
Can you post the code of that class in question somwhere?

Avatar

Level 4
@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 { } } }

Avatar

Level 4
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 ?

Avatar

Employee Advisor

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.

Avatar

Level 4
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.

Avatar

Level 4
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)

Avatar

Community Advisor

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;

}
}
}