Setting properties in Adobe CQ5

I am working on a CQ5-based application, which is a completely new area for me since I used to work on Spring web applications.

The application is a maven project based on the Blue-prints archetype (http://www.cqblueprints.com/xwiki/bin/view/Blue+Prints/The+CQ+Project+Maven+Archetype).

Now I have a question, what is the standard approach for adding some properties that usually go into the config.properties file (or similar) in a standard web application. Properties that contain things like hostNames, accountNumbers, etc.

Greetings.

+4
source share
3 answers

Thanks a lot Bertrand, your answer really pointed me in the right direction.

What I did, I created .ConfigService.xml for each of my running modes, which looks like this:

<?xml version="1.0" encoding="UTF-8"?> <jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" jcr:primaryType="sling:OsgiConfig" myconfig.config="{String}My Value"/> 

Then in my ConfigService it was like this:

 @Component(immediate = true, metatype = true) @Service(ConfigService.class) public class ConfigService { private Dictionary<String, String> properties; @SuppressWarnings("unchecked") protected void activate(ComponentContext context) { properties = context.getProperties(); } protected void deactivate(ComponentContext context) { properties = null; } public String getProperty(String key) { return properties.get(key); } } 

Than I just use ConfigService if I need to get a configuration property that accesses it using @Reference.

I hope this can help someone!

+1
source

I am not familiar with the drawings, but as I understand it, this is just a way to generate the structure of a CQ project, so I assume that it has no real influence on how you control the configuration settings.

CQ5 is based on Apache Sling , which uses the ConfigGmin OSGi service for custom settings and provides several tools to make this easier.

You can see an example of this in the Sling component

This value is then read in the component () method:

  final Dictionary<?, ?> properties = componentContext.getProperties(); final String[] mappingList = (String[]) properties.get(PROP_PATH_MAPPING); 

and the OSGi package containing this component provides a file to determine the name and label of the custom parameter.

What is this with this, the Sling and OSGi framework generate a basic configuration interface for the component, which you can access from / system / console / config, and also manage the activation and activation of the component automatically if the configuration settings change.

These configurations can also come from the JCR repository, thanks to the Sling installer that selects them, you can find several of them in folders called "config" under / libs and / apps in your CQ5 repository.

Another option is to directly use JCR content, depending on how your custom settings are used. You can tell your component that its config is located in / apps / foo / myparameters in the repository (and only this value is configured), and add the JCR properties and child nodes under this node as necessary, so that your component can read it. The disadvantage is that your @Component will not restart automatically when you change the settings, as happens when using OSGi configurations directly.

Long explanation ... hope this helps; -)

+6
source

The ConfigService example may not be the best approach, since for a component, ComponentContext should only be used when activating and deactivating components.

0
source

All Articles