public class XmlConfiguration
extends java.lang.Object
Configures objects from XML.
This class reads an XML file conforming to the configure.dtd DTD and uses it to configure and object by calling set, put or other methods on the object.
The actual XML file format may be changed (eg to spring XML) by implementing the
ConfigurationProcessorFactory
interface to be found by the
ServiceLoader
by using the DTD and first tag element in the file.
Note that DTD will be null if validation is off.
The configuration can be parameterised with properties that are looked up via the
Property XML element and set on the configuration via the map returned from
getProperties()
The configuration can create and lookup beans by ID. If multiple configurations are used, then it
is good practise to copy the entries from the getIdMap()
of a configuration to the next
configuration so that they can share an ID space for beans.
Constructor | Description |
---|---|
XmlConfiguration(java.io.InputStream configuration) |
Reads and parses the XML configuration stream.
|
XmlConfiguration(java.lang.String configuration) |
Reads and parses the XML configuration string.
|
XmlConfiguration(java.net.URL configuration) |
Reads and parses the XML configuration file.
|
Modifier and Type | Method | Description |
---|---|---|
java.lang.Object |
configure() |
Applies the XML configuration script.
|
java.lang.Object |
configure(java.lang.Object obj) |
Applies the XML configuration script to the given object.
|
java.util.Map<java.lang.String,java.lang.Object> |
getIdMap() |
Get the map of ID String to Objects that is used to hold
and lookup any objects by ID.
|
java.util.Map<java.lang.String,java.lang.String> |
getProperties() |
Get the map of properties used by the Property XML element
to parameterise configuration.
|
void |
initializeDefaults(java.lang.Object object) |
Initialize a new Object defaults.
|
static void |
main(java.lang.String... args) |
Run the XML configurations as a main application.
|
static java.lang.String |
normalizeURI(java.lang.String uri) |
|
void |
setJettyStandardIdsAndProperties(java.lang.Object server,
Resource webapp) |
Set the standard IDs and properties expected in a jetty XML file:
RefId Server
Property jetty.home
Property jetty.home.uri
Property jetty.base
Property jetty.base.uri
Property jetty.webapps
Property jetty.webapps.uri
|
public XmlConfiguration(java.net.URL configuration) throws org.xml.sax.SAXException, java.io.IOException
configuration
- the URL of the XML configurationjava.io.IOException
- if the configuration could not be readorg.xml.sax.SAXException
- if the configuration could not be parsedpublic XmlConfiguration(java.lang.String configuration) throws org.xml.sax.SAXException, java.io.IOException
configuration
- String of XML configuration commands excluding the normal XML preamble.
The String should start with a "<Configure ....>" element.java.io.IOException
- if the configuration could not be readorg.xml.sax.SAXException
- if the configuration could not be parsedpublic XmlConfiguration(java.io.InputStream configuration) throws org.xml.sax.SAXException, java.io.IOException
configuration
- An input stream containing a complete configuration filejava.io.IOException
- if the configuration could not be readorg.xml.sax.SAXException
- if the configuration could not be parsedpublic void setJettyStandardIdsAndProperties(java.lang.Object server, Resource webapp)
server
- The Server object to setwebapp
- The webapps Resourcepublic static java.lang.String normalizeURI(java.lang.String uri)
public java.util.Map<java.lang.String,java.lang.Object> getIdMap()
A New, Get or Call XML element may have an id attribute which will cause the resulting object to be placed into this map. A Ref XML element will lookup an object from this map.
When chaining configuration files, it is good practise to copy the ID entries from the ID map to the map of the next configuration, so that they may share an ID space
public java.util.Map<java.lang.String,java.lang.String> getProperties()
public java.lang.Object configure(java.lang.Object obj) throws java.lang.Exception
obj
- The object to be configured, which must be of a type or super type
of the class attribute of the <Configure> element.java.lang.Exception
- if the configuration failspublic java.lang.Object configure() throws java.lang.Exception
java.lang.Exception
- if the configuration failspublic void initializeDefaults(java.lang.Object object)
This method must be called by any ConfigurationProcessor
when it
creates a new instance of an object before configuring it, so that a derived
XmlConfiguration class may inject default values.
object
- the object to initialize defaults onpublic static void main(java.lang.String... args) throws java.lang.Exception
Any property file on the command line is added to a combined Property instance that is passed to each configuration file via
getProperties()
.
Each configuration file on the command line is used to create a new XmlConfiguration instance and the configure()
method is used
to create the configured object. If the resulting object is an instance of LifeCycle
, then it is started.
Any IDs created in a configuration are passed to the next configuration file on the command line using getIdMap()
.
This allows objects with IDs created in one config file to be referenced in subsequent config files on the command line.
args
- array of property and xml configuration filenames or Resource
s.java.lang.Exception
- if the XML configurations cannot be runCopyright © 1995–2018 Webtide. All rights reserved.