public class Configuration extends Object
Evo Config opens one or more XML files, merges their content, filters them and then converts into the tree of objects using JAXB.
To use Evo Config you need to define the schema for your configuration file.
This is achieved by creating a number of classes which extend Configurable abstract class
and annotating them with
JAXB annotations.
Let's start by defining classes for generic service and some specific database service:
abstract class Service extends Configurable {
public abstract void start();
}
. @XmlRootElement(name = "database")
class Database extends Service {
. @XmlElement
private String url;
public void start() {
System.out.println("Connecting to database: " + url);
}
}
Also let's create the class which will define root of the configuration schema. Here the common idiom
is to create field of type List annotated with XmlElementRef. Using this JAXB
will be able to unmarshal any subclass of list element type. In this way the schema is open-ended,
allowing anyone to implement our Service. There is no need to list all the implementations:
. @XmlRootElement(name = "config")
class Config extends Configurable {
. @XmlElementRef
. @XmlElementWrapper(name = "services")
. @Valid
private List<Service> services;
}
The above schema will match the following XML:
<config>
<services>
<database>
<url>jdbc:h2:file:/data/sample</url>
</database>
<database>
<url>jdbc:h2:tcp://localhost/~/test</url>
</database>
</services>
</config>
Configuration configuration = new Configuration();
configuration.combine("first.xml");
configuration.combine("second.xml");
configuration.filter(properties);
Root root = configuration.read(Root.class);
The following actions will be performed:
JAXBContext will be created for all the classes extending Configurable,
those classes are indexed at compile-time using ClassIndex facility,XmlCombiner facility,${name} will be substituted
with the value using registered PropertyResolver, see Filtering for details,JAXB into provided root class,Bean Validation framework.| Constructor and Description |
|---|
Configuration()
Create Configuration by discovering all
Configurables. |
Configuration(Iterable<Class<? extends Configurable>> klasses)
Create Configuration by manually specifying all
Configurables. |
| Modifier and Type | Method and Description |
|---|---|
void |
combine(InputStream stream)
Parse an XML file and combine it with the currently stored DOM tree.
|
void |
filter(Properties properties)
Filter
${name} placeholders using given properties. |
void |
filter(PropertyResolver resolver)
Filter
${name} placeholders using values from given PropertyResolver. |
void |
generateSchema(File filename)
Generate an XSD schema for the configuration file.
|
Element |
getRootElement()
Get root XML
Element of the combined configuration file. |
<T extends Configurable> |
read(Class<T> rootClass)
Unmarshals stored configuration DOM tree as object of the given class.
|
<T extends Configurable> |
read(Class<T> rootClass,
InputStream... streams)
Combines several input XML documents and reads the configuration from it.
|
public Configuration()
Configurables.
Uses ClassIndex.getSubclasses(Class) to get Configurables.
public Configuration(Iterable<Class<? extends Configurable>> klasses)
Configurables.klasses - list of Configurable classes.JAXBException - when JAXB context creation failspublic void generateSchema(File filename) throws IOException
filename - file to store the schema toIOException - when IO error occurspublic void filter(Properties properties) throws IncorrectConfigurationException
${name} placeholders using given properties.
This method wraps given properties into PropertiesPropertyResolver
and calls filter(PropertyResolver).
properties - properties to filter into configuration filesIncorrectConfigurationExceptionpublic void filter(PropertyResolver resolver) throws IncorrectConfigurationException
${name} placeholders using values from given PropertyResolver.resolver - property resolver used for filtering the configuration filesIncorrectConfigurationExceptionCompoundPropertyResolverpublic void combine(InputStream stream) throws IncorrectConfigurationException, IOException
stream - stream with the XML fileIncorrectConfigurationException - when configuration file is invalidIOException - when the stream cannot be readpublic <T extends Configurable> T read(Class<T> rootClass) throws IncorrectConfigurationException
T - type of the rootClassrootClass - the class to which unmarshal the DOM treeIncorrectConfigurationException - if configuration is incorrectpublic Element getRootElement()
Element of the combined configuration file.Elementpublic <T extends Configurable> T read(Class<T> rootClass, InputStream... streams) throws IncorrectConfigurationException, IOException
This is equivalent to executing combine(InputStream) for each stream
and then read(Class) for rootClass.
T - type of the rootClassrootClass - the class to which unmarshal the DOM treestreams - input streams with the configuration to combineIncorrectConfigurationException - if configuration is incorrectIOException - when cannot access configuration filesCopyright © 2013 Atteo. All Rights Reserved.