Nesting configuration elements
We can use metatype extensions to define configuration that can be expressed as nested XML elements in server.xml.
Examples
Support this user configuration in server.xml:
<family mother="jane" father="john"> <child name="susie" age="8" /> <child name="danny" age="5" /> </family>The metatype XML uses ibm:type="pid" and ibm:reference as shown in following example:
<?xml version="1.0" encoding="UTF-8"?> <metatype:MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.1.0" xmlns:ibm="http://www.ibm.com/xmlns/appservers/osgi/metatype/v1.0.0"> <OCD id="family" name="family"> <AD id="mother" name="mother" type="String" default="Ma" /> <AD id="father" name="father" type="String" default="Pa" /> <AD id="child" name="child" ibm:type="pid" ibm:reference="child-pid" required="false" type="String" cardinality="6" /> </OCD> <Designate pid="family"> <Object ocdref="family" /> </Designate> <OCD id="child" name="child" > <AD id="name" name="name" type="String" /> <AD id="age" name="age" type="Integer" /> </OCD> <Designate factoryPid="child-pid"> <Object ocdref="child" /> </Designate> </metatype:MetaData>The following example show how the code that receives the family properties uses the ConfigurationAdmin service to obtain the child property sets:
public void updated(Dictionary<String, ?> properties) throws ConfigurationException { Set<String> pids = new HashSet<String>(); String mother = "null"; String father = "null"; try { if (properties != null) { mother = (String) properties.get("mother"); father = (String) properties.get("father"); String[] children = (String[]) properties.get("child"); if (children == null || children.length == 0) { return; } // Get the configuration admin service ConfigurationAdmin configAdmin = null; ServiceReference configurationAdminReference = bundleContext.getServiceReference(ConfigurationAdmin.class.getName()); if (configurationAdminReference != null) { configAdmin = (ConfigurationAdmin) bundleContext.getService(configurationAdminReference); } for (String childPid : children) { pids.add(childPid); Configuration config = configAdmin.getConfiguration(childPid); String name = (String) config.getProperties().get("name"; Integer age = (Integer) config.getProperties().get("age"); } } } catch (Exception e) { e.printStackTrace(); } }
Parent topic: Extensions to the OSGi metatype service