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

Avatar

Avatar
Level 1
LikesAEM
Level 1

Likes

0 likes

Total Posts

5 posts

Correct reply

0 solutions
View profile

Avatar
Level 1
LikesAEM
Level 1

Likes

0 likes

Total Posts

5 posts

Correct reply

0 solutions
View profile
LikesAEM
Level 1

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
Validate 1
MVP
Umesh_Thakur
MVP

Likes

147 likes

Total Posts

157 posts

Correct reply

53 solutions
Top badges earned
Validate 1
Applaud 25
Ignite 3
Ignite 1
Give Back 5
View profile

Avatar
Validate 1
MVP
Umesh_Thakur
MVP

Likes

147 likes

Total Posts

157 posts

Correct reply

53 solutions
Top badges earned
Validate 1
Applaud 25
Ignite 3
Ignite 1
Give Back 5
View profile
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
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,314 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,314 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
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
Applaud 25
Level 10
asutosh_jena
Level 10

Likes

593 likes

Total Posts

705 posts

Correct reply

207 solutions
Top badges earned
Applaud 25
Give Back 100
Boost 500
Affirm 100
Ignite 1
View profile

Avatar
Applaud 25
Level 10
asutosh_jena
Level 10

Likes

593 likes

Total Posts

705 posts

Correct reply

207 solutions
Top badges earned
Applaud 25
Give Back 100
Boost 500
Affirm 100
Ignite 1
View profile
asutosh_jena
Level 10

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!