嵌套配置元素
可以使用元类型扩展来定义配置,而该配置可以表示为 server.xml 文件中的嵌套 XML 元素。
示例
以下示例显示如何在 server.xml 文件中支持此用户配置:
<family mother="jane" father="john">
<child name="susie" age="8" />
<child name="danny" age="5" />
</family>
元类型 XML 使用 ibm:type="pid" 和 ibm:reference,如以下示例中所示:
<?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>
以下示例显示接收 family 属性的代码如何使用 ConfigurationAdmin 服务来获取 child 属性集:
String father = "null";
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();
}
}