建立巢狀配置元素
您可以利用 meta 類型延伸規格來定義可以在 server.xml 檔中表示為巢狀 XML 元素的配置。
範例
下列範例顯示如何在 server.xml 檔中支援這個使用者配置:
<family mother="jane" father="john">
<child name="susie" age="8" />
<child name="danny" age="5" />
</family>
meta 類型 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 內容集:
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;
}
// 取得配置管理服務
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();
}
}