구성 요소 중첩
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 특성 세트를 확보하는 방법을 보여줍니다.
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();
}
}