Expand my Community achievements bar.

SOLVED

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

Avatar

Level 1

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);

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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

 

View solution in original post

7 Replies

Avatar

Community Advisor

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!

Avatar

Level 1
@Asutosh_Jena_ - It doesn't have answer to my query. Please look at my above comment

Avatar

Community Advisor

@LikesAEM 

Please see below:

 

ConfigurationResourceResolver configurationResourceResolver; (You can get this as a Reference)
String contextPath = configurationResourceResolver.getContextPath(contentResource);
log.info("Context Path --> {}", contextPath);

This will give you the path from where the context aware config (i.e. sling:configRef) property is getting picked.

This is what exactly I referred in the link I provided earlier


Hope this helps!

Avatar

Correct answer by
Community Advisor

@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

 

Avatar

Level 1
@Umesh_Thakur - I can read the properties, that is not an issue. My question was how to know the context path (value of sling:configRef property) in the sling modal class?

Avatar

Community Advisor
That you can easily get from resourceIteration of your current component Or from inheritanceValuemap .

Avatar

Community Advisor

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");
		}


Arun Patidar