In Context aware configuration, how to read sling:configRef property in sling model

Avatar

Avatar

LikesAEM

Avatar

LikesAEM

LikesAEM

21-03-2021

I am leveraging context-aware configuration. In my sling model class, I am using the below code to read the context-aware configuration. As we know, this uses the inheritance logic to find the first available sling:configRef property. I have sling:configRef property at multiple root levels. So, In my sling model, I need to know the following two things. I don’t see any exposed methods to get these values. I have an option to use heritanceValueMap to sling:configRef property, but I would like to know if anything is exposed out of configurationbuilder or any other implementing class. We are on 6.5.3

 

  1.  Path from where sling:configRef property is getting picked up
  2.  Value of sling:configRef property

                ConfigurationBuilder configurationBuilder = resource.adaptTo(ConfigurationBuilder.class);

                SampleConfig sampleConfig = configurationBuilder.as(SampleConfig.class);

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Umesh_Thakur

MVP

Avatar

Umesh_Thakur

MVP

Umesh_Thakur
MVP

21-03-2021

@LikesAEM,

In AEM, On our content path we use the property slign:configRef to pass the path of the context aware Siteconfiguration at this path we used to have one .xml file that will have all the parameters mapped with the fields your siteconfiguration.java file. and thus if you have an object of the .java file you will get those parameters easily.

For example:

1. In our site path at : /content/my-site/us/en_us/jcr:content,  we have sling:configRef pointing to /conf/my-site/us/en_us

2. In our code base at /conf/my-site/us/en_us we have on sling folder type node named sling:configs

3. Under /conf/my-site/us/en_us/sling:configs we have created on .xml file with pid of the site configuration like

com.mysite.configurations.SiteConfiguration.xml

4. in the above file we pass all the parameter that will be mapped to SiteConfiguration.java like

 Java:

 @Property(label = "Site ID", description = "Configurable Site ID")

    String siteID();

XML:

siteID="1"

 

Now in sling model you will be getting reference of the SiteConfiguration like

ConfigurationBuilder configBuilder = contentResource.adaptTo(ConfigurationBuilder.class);

                if (null != configBuilder) {

                    siteConfig = configBuilder.as(SiteConfiguration.class);

                }

And then from

siteConfig.siteID();

 

this is how you can get the value of context aware config.

Now you need to check what are step you missed.

Hope this will help.

Umesh Thakur

 

Answers (2)

Answers (2)

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

3.1K

Likes

1.2K

Correct Reply

883

Avatar

Arun_Patidar

MVP

Total Posts

3.1K

Likes

1.2K

Correct Reply

883
Arun_Patidar
MVP

22-03-2021

you can use below API

https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/javadoc/co...

e.g.

Conf conf = getCurrentPage().adaptTo(Conf.class);
		//log.info("conf ===>After");
		if (conf != null) {
			dbSettings = conf.getItem("dbDetails");
			dbUserName = dbSettings.get("dbUser", "No UserId found");
			dbUserPwd = dbSettings.get("dbPwd", "No password found");
			dbUserURI = dbSettings.get("dbUrl", "No URL found");
		//	log.info("Values from conf : " + dbUserName + dbUserPwd + dbUserURI);
		} else {
			log.info("conf is Null");
		}

Avatar

Avatar

asutosh_j3

Avatar

asutosh_j3

asutosh_j3

21-03-2021

Hi @LikesAEM 

Please refer Context-Aware Resources and Context-Aware Configurations section from the below link. It has the details what you are looking for.
https://sling.apache.org/documentation/bundles/context-aware-configuration/context-aware-configurati...

 

Hope this helps!
Thanks!