Anchor | ||||
---|---|---|---|---|
|
In This Section
Table of Contents | ||
---|---|---|
|
...
When creating and reading configuration entries, be sure that you conform to the plugin development guidelines (/wiki/spaces/PD/pages/24721296 and /wiki/spaces/PD/pages/24721296 in particular). When you follow the steps explained in this section, your plugin will conform to these guidelines.
Adding Properties to the Configuration Management
Beginning with XperienCentral.version 10.4, it is possible to add properties to the Configuration Management through a plugin. This section explains the steps you need to take to create two new extra properties. One extra property is an array (a list of server names). The second extra property is the portnumber
.
Create config_metatype.xml
Create a new file in the root of the resources folder; the new file is called config_metatype.xml
. This file defines the properties that can be used and groups them into a set. The contents of config_metatype.xml
should look like this in order to create support for the two new properties:
...
The cardinality can be set for each property. Cardinality ‘-1’
means that the property can contain more than one value. Cardinality ‘1’
means that the property can only hold one value. Be sure that the id of the configuration set (my_config_set
) is prefixed by the plugin ID, as described in guideline /wiki/spaces/PD/pages/24721296.
Create config.xml
While the file config_metatype.xml
can be compared to an interface, the file config.xml can be compared to an implementation of this interface. The config.xml
file should also be placed in the root of the resources folder. The config.xml
file contains default values for the properties as defined in config_metatype.xml
. The config.xml
for the above example looks like this:
...
The values for a property that can hold multiple values should be entered in a comma-separated fashion.
Register the Configuration Set
By just creating the two XML files the Configuration Management service still doesn’t know about the new properties. There are two methods in the Configuration Management service that can be called that will process the two XML files and register the new properties:
...
The two methods as described above should only be called when the plugin is loaded. The best place for these lines is in the start()
method of a configuration service. Remember that this configuration set is created by the plugin and should thus be removed automatically when the plugin is purged. So the purge method should delete the configuration set.
...
Using Custom Configuration Properties
This section describes in detail how to use properties that were added to the Configuration Management service. The following steps have to be taken to make the properties available:
- Create a configuration service that has access to the Configuration Management service;
- The component that needs access to the configuration properties depends on the configuration service.
Define a Service Component in the Activator
In the Activator, a service component is defined. The service component will:
...
Code Block | ||
---|---|---|
| ||
ServiceComponentDefinitionImpl serviceDef = new ServiceComponentDefinitionImpl( false ); serviceDef.setId("nl.gx.helloworld.configurationservice"); serviceDef.setName("HelloWorld configuration service."); serviceDef.setDescription( "Service which contains the configuration for this WCB." ); serviceDef.setTypeId( ServiceComponentType.class.getName() ); serviceDef.setProperties( new Hashtable<String, String>() ); serviceDef.setImplementationClassName(HelloWorldConfigurationServiceImpl.class.getName() ); serviceDef.setInterfaceClassNames( new String[] {HelloWorldConfigurationService.class.getName()} ); serviceDef.setWrapperClassNames( new String[] {} ); ComponentDependencyImpl serviceDependency = new ComponentDependencyImpl(); serviceDependency.setServiceName(ConfigurationManagement.class.getName()); serviceDependency.setRequired(true); serviceDef.setDependencies(new ComponentDependency[] { serviceDependency }); |
Define an Interface for the HWC Service
The interface for the HWC (HelloWorld Configuration) service component is very small. The getters in the interface are based on the XML examples of the previous sections. A basic interface looks like this:
...
Code Block | ||
---|---|---|
| ||
public interface HelloWorldConfigurationService { public String[] getServerNames(); public String getPortnumber(); } |
Create an Implementation of the Interface
The implementation of the interface has a dependency on the Configuration Management service so it only needs a private member of the ConfigurationManagement
type to have access to the service. The implementation of the HelloWorldConfigurationService
interface looks like this:
...
Code Block | ||
---|---|---|
| ||
public final class HelloWorldConfigurationServiceImpl extends SimpleServiceComponent implements HelloWorldConfigurationService { private final static Logger LOG = Logger.getLogger(HelloWorldConfigurationServiceImpl.class.getName()); private ConfigurationManagement myConfigService; /** * Callback method for the dependency manager, called when the bundle isstarted. This start method registers the properties * to the ConfigurationManagement service. */ public void onStart() { // Note: when the configuration set already exists, its existing properties will not be affected. myConfigService.addConfigurationSetDefinition( getClass().getResource("/config_metatype.xml")); myConfigService.parseAndAddConfigurationSets( getClass().getResource("/config.xml"), false); } /** * Callback method for the dependency manager, called when the bundle is purged. This purge method removes the configuration set * created in the start method. */ public boolean onPurge() { myConfigService.removeConfigurationSet("my_config_set"); return true; } public String getPortnumber() { try { return myConfigService.getParameter( "my_config_set.my_config_entry_portnumber"); } catch (ConfigurationManagementException e) { LOG.log(Level.WARNING, "Could not find parameter'my_config_set.my_config_entry_portnumber' from the ConfigurationManagement service", e); return null; } } public String[] getServerNames() { try { return myConfigService.getParameters(“my_config_set.my_config_entry_server_names"); } catch (ConfigurationManagementException e) { LOG.log(Level.WARNING, "Could not find parameter'my_config_set.my_config_entry_server_names' from the ConfigurationManagement service", e); return null; } } } } } |
Add a Dependency to the Configuration Service
The components that want access to the properties need to create a dependency on the HWC service that was created in the previous sections. If, for example, an element needs access to the properties, the dependency would look like this:
...
Add this dependency to the definition of a component in the Activator.
Use the Dependency in the Implementation
The dependency to the HWC service can now be used in the element implementation:
...
Code Block | ||
---|---|---|
| ||
INFO: Values of the custom properties are: Server names: www.domain1.com;www.domain2.com; Portnumber : 8080 |
Changing the Values of the Properties
Once the properties are present in the Configuration Management service, the value of the properties can be changed through the /web/setup tool. Log onto the Setup Tool (/web/setup
) and navigate to the General configuration tab. Your configuration set should be present between the default configuration sets (the sets are alphabetically ordered).