Commit 2331262d authored by R.W.Majeed's avatar R.W.Majeed

loadable xml configuration using jaxb

parent fe604cf4
......@@ -33,6 +33,28 @@
</configuration>
</plugin>
<!--
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>schemagen</id>
<goals>
<goal>schemagen</goal>
</goals>
<phase>package</phase>
<configuration>
<includes>
<include>**/*.java</include>
</includes>
<outputDirectory>${project.build.directory}/generated-resources/schemagen</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>-->
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
......@@ -62,57 +84,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-xjc-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<!--<extensions>
<extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:2.3.0</extension>
</extensions>-->
</configuration>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>xsdtojava</goal>
</goals>
<configuration>
<sourceRoot>${basedir}/target/generated-sources/xjc</sourceRoot>
<xsdOptions>
<xsdOption>
<xsd>${basedir}/src/main/resources/histream-config.xsd</xsd>
<packagename>de.sekmi.histream.config</packagename>
<extensionArgs>
<arg>-encoding</arg>
<arg>${project.build.sourceEncoding}</arg>
</extensionArgs>
</xsdOption>
</xsdOptions>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source> ${project.build.directory}/generated-sources/xjc</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
......
package de.sekmi.histream.conf;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "http://some/namespace",
name = "histream", propOrder = {
"plugins",
"source",
"destination"
})
public class Configuration {
@XmlElement(name="plugin")
@XmlElementWrapper(name="plugins", required=true)
protected PluginConfig[] plugins;
@XmlElement(required = true)
protected List<PluginRef> source;
@XmlElement(required = true)
protected List<PluginRef> destination;
public PluginConfig[] getPlugins(){return plugins;}
}
package de.sekmi.histream.conf;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "plugin")
public class PluginConfig {
@XmlAttribute(name = "class", required = true)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlID
@XmlSchemaType(name = "ID")
protected String clazz;
@XmlElement
protected PluginProperty[] property;
}
package de.sekmi.histream.conf;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "property")
public class PluginProperty {
@XmlAttribute(required = true)
protected String name;
@XmlValue
protected String value;
}
package de.sekmi.histream.conf;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "pluginref")
public class PluginRef {
@XmlAttribute(name = "plugin", required = true)
@XmlIDREF
@XmlSchemaType(name = "IDREF")
protected PluginConfig plugin;
}
package de.sekmi.histream.impl;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import javax.xml.bind.JAXB;
import de.sekmi.histream.Plugin;
import de.sekmi.histream.conf.Configuration;
public class HIStream {
......@@ -16,6 +23,9 @@ public class HIStream {
plugins.add(constructor.newInstance(configuration));
}
public HIStream(){
plugins = new LinkedList<>();
}
public void shutdown(){
for( Plugin plugin : plugins ){
try {
......@@ -30,6 +40,9 @@ public class HIStream {
public static void main(String[] args){
HIStream hs = new HIStream();
// load configuration
Configuration config = JAXB.unmarshal(new File("src/test/resources/histream.xml"), Configuration.class);
System.out.println("Configuration with "+config.getPlugins().length+" plugins");
// load plugins
hs.shutdown();
......
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:version="1.0"
>
<xs:element name="histream" type="histream"/>
......@@ -26,7 +30,15 @@
<xs:complexType name="pluginref">
<xs:sequence/>
<xs:attribute name="plugin" type="xs:IDREF" use="required"/>
<xs:attribute name="plugin" type="xs:IDREF" use="required">
<xs:annotation>
<xs:appinfo>
<jxb:property>
<jxb:baseType name="Plugin"/>
</jxb:property>
</xs:appinfo>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:complexType name="plugin-property">
......
package de.sekmi.histream;
import java.io.File;
import javax.xml.bind.JAXB;
import org.junit.Assert;
import org.junit.Test;
import de.sekmi.histream.conf.Configuration;
public class TestLoadConfig {
@Test
public void loadNewConfig(){
Configuration config = JAXB.unmarshal(new File("src/test/resources/histream.xml"), Configuration.class);
Assert.assertNotNull(config);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment