how to create the configuration properties for a service
how to read the configuration values in the service.
How to Create configurable properties:
@Service(value = ConfigurationServiceImpl.class)
@Component(immediate = true, metatype = true, label = "Example Configuration Service")
public class ConfigurationServiceImpl implements ConfigurationService {
private static final Logger LOG = LoggerFactory.getLogger(ConfigurationServiceImpl.class);
private static final String CLASS_NAME = "[ConfigurationService]: ";
@Property(unbounded = PropertyUnbounded.ARRAY, label = "Multi String", cardinality = 50, description = "Example for Multi field config")
private static final String MULTI_FIELD = "multifield";
@Property(unbounded = PropertyUnbounded.DEFAULT, label = "Simple String", description = "Example for Simple text field config")
private static final String SIMPLE_FIELD = "simplefield";
As shown in the above example,
Use @Property annotation for the variable.
The data type of the variable determines the type of value it can hold
Set ‘Unbounded’ attribute as ARRAY if it’s a ‘multi’ property else assign it as DEFAULT
Give the ‘label and ‘description’ to be shown in the /system/console/configMgr
Add ‘metatype=true’ attribute for @Component to enable the properties as configurable values.
As soon as we have done this, you will able to configure the values for these properties via ConfigMgr
But the best way is to create the different configuration based on the run modes if the values are different in each mode and the same can be done by creating the config files under /apps as shown below.
Config Runmodes
Add the properties with the name given in the services like ‘multifield’ and ‘simplefield’ along with their values and save.Config Properties
Now, let us see how to read these values.
Add the properties with the name given in the services like ‘multifield’ and ‘simplefield’ along with their values and save.Config Properties
Now, let us see how to read these values.
protected void readProperties(final Map < String, Object > properties) {
LOG.info(properties.toString());
this.multiString = PropertiesUtil.toStringArray(properties.get(MULTI_FIELD));
LOG.info("Mutli String Size: " + multiString.length);
this.simpleString = PropertiesUtil.toString(properties.get(SIMPLE_FIELD), "default");
LOG.info("Simple String: " + simpleString);
}
The above method is an example of how to read the properties.
You can download the project from the github here. Also, click here to refer my Adobe article on this in detail.
No comments:
Post a Comment
If you have any doubts or questions, please let us know.