Reading sling:OsgiConfig into @ObjectClassDefinition | Community
Skip to main content
Adilmo
Level 4
August 21, 2020
Solved

Reading sling:OsgiConfig into @ObjectClassDefinition

  • August 21, 2020
  • 5 replies
  • 6838 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

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

5 replies

Shashi_Mulugu
Community Advisor
Community Advisor
August 21, 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

Adilmo
AdilmoAuthor
Level 4
August 21, 2020
component is not starting ... it is in "no config" state .... I would like to the component to fetch SLING CONFIG and start on its own .... no manual configuration through console
Adilmo
AdilmoAuthor
Level 4
August 21, 2020
component is not starting ... it is in "no config" state .... I would like to the component to fetch sling:osgi configs and start on its own .... no manual configuration through console
arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
August 22, 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

Arun Patidar
arunpatidar
Community Advisor
Community Advisor
September 1, 2020

hi, You can check examples from https://github.com/Adobe-Consulting-Services/acs-aem-commons/tree/master/ui.apps/src/main/content/jcr_root/apps/acs-commons/config

I don't have any now but I will setup a package and share with you.

Arun Patidar
joerghoh
Adobe Employee
Adobe Employee
August 22, 2020

You can find a working example at https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/0ffd40a15460f8be8d3591e17d5648184f63e42f/bundle/src/main/java/com/adobe/acs/commons/throttling/RequestThrottler.java

 

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

Adilmo
AdilmoAuthor
Level 4
August 23, 2020
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
Shashi_Mulugu
Community Advisor
Community Advisor
August 24, 2020

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

 

Corrected code below:-

 

 

@8220494(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;
@580286 Configuration configuration;
@580286 ComponentContext componentContext;
@580286 @9182423 protected void activate(Config c) {
try {

this.configuration = c;

}
}
}