# HG changeset patch # User Sascha L. Teichmann # Date 1252065852 0 # Node ID 8f2de197bce2dc4371ccdc60575cfa629bc7f2bc # Parent 13a12b607bafbfb45e1204e25c20fb552b704665 Added config to artifact database and modelled bootstap of artifact factories on top of this. artifacts/trunk@15 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 13a12b607baf -r 8f2de197bce2 Changelog --- a/Changelog Fri Sep 04 08:32:09 2009 +0000 +++ b/Changelog Fri Sep 04 12:04:12 2009 +0000 @@ -1,3 +1,30 @@ +2009-09-04 Sascha L. Teichmann + + * artifact-database/doc/artifactdb-example-conf.xml: Example for artifact db + configuration. + + * artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactContextFactory.java: + New. Default implementation of ArtifactContextFactory. Creates a DefaultArtifactContext. + + * artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactContext.java: + New. Default implementation of ArtifactContext. + + * artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java: + New. Class to bootstrap the context and the artifact factories. + + * artifact-database/src/main/java/de/intevation/artifactdatabase/App.java: Bootstrap + factories. + + * artifact-database/src/main/java/de/intevation/artifactdatabase/Config.java: New. + Loads configuration as XML from a file. The file is specified by the system + property "artifact.database.config" and defaults to "artifactdb-conf.xml". + The configuration can be searched via XPath. + + * artifact-database/pom.xml: Made de.intevation.artifactdatabase.App the main + class for start up. + + * TODO: Added remark to add logging to artifact database. + 2009-09-04 Sascha L. Teichmann * artifacts/src/main/java/de/intevation/artifacts/ArtifactContextFactory.java: diff -r 13a12b607baf -r 8f2de197bce2 TODO --- a/TODO Fri Sep 04 08:32:09 2009 +0000 +++ b/TODO Fri Sep 04 12:04:12 2009 +0000 @@ -0,0 +1,2 @@ +TODO: + * integrate logging into artifact database. diff -r 13a12b607baf -r 8f2de197bce2 artifact-database/doc/artifactdb-example-conf.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifact-database/doc/artifactdb-example-conf.xml Fri Sep 04 12:04:12 2009 +0000 @@ -0,0 +1,11 @@ + + + + com.example.Context + + com.example.ArtifactExampleFactoryOne + com.example.ArtifactExampleFactoryTwo + com.example.ArtifactExampleFactoryThree + + + diff -r 13a12b607baf -r 8f2de197bce2 artifact-database/pom.xml --- a/artifact-database/pom.xml Fri Sep 04 08:32:09 2009 +0000 +++ b/artifact-database/pom.xml Fri Sep 04 12:04:12 2009 +0000 @@ -10,6 +10,22 @@ artifact-database 1.0-SNAPSHOT http://maven.apache.org + + + + org.apache.maven.plugins + maven-jar-plugin + 2.2 + + + + de.intevation.artifactdatabase.App + + + + + + de.intevation.bsh.artifacts diff -r 13a12b607baf -r 8f2de197bce2 artifact-database/src/main/java/de/intevation/artifactdatabase/App.java --- a/artifact-database/src/main/java/de/intevation/artifactdatabase/App.java Fri Sep 04 08:32:09 2009 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/App.java Fri Sep 04 12:04:12 2009 +0000 @@ -1,8 +1,16 @@ package de.intevation.artifactdatabase; +/** + * Starting point of the artifact database. + * + * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) + */ public class App { public static void main(String[] args) { + FactoryBootstrap bootstrap = new FactoryBootstrap(); + + bootstrap.boot(); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: diff -r 13a12b607baf -r 8f2de197bce2 artifact-database/src/main/java/de/intevation/artifactdatabase/Config.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Config.java Fri Sep 04 12:04:12 2009 +0000 @@ -0,0 +1,108 @@ +package de.intevation.artifactdatabase; + +import java.io.File; +import java.io.IOException; + +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; + +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilderFactory; + +import javax.xml.parsers.ParserConfigurationException; + +import javax.xml.namespace.QName; + +import javax.xml.xpath.XPathFactory; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathConstants; + +public final class Config +{ + public static final String CONFIG_PROPERTY = "artifact.database.config"; + + public static final String CONFIG_DEFAULT = "artifactdb-conf.xml"; + + private static Document config; + + private Config() { + } + + public static synchronized final Document getConfig() { + if (config == null) { + config = loadConfig(); + } + return config; + } + + private static Document loadConfig() { + File file = new File( + System.getProperty(CONFIG_PROPERTY, CONFIG_DEFAULT)); + + if (!file.canRead() && !file.isFile()) { + System.err.println("ERROR: cannot read config file '" + + file + "'."); + return null; + } + + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setValidating(false); // XXX: This may change in future. + return factory.newDocumentBuilder().parse(file); + } + catch (SAXException se) { + System.err.println("ERROR: while processing XML file '" + + file + "'"); + se.printStackTrace(System.err); + } + catch (ParserConfigurationException pce) { + System.err.println("ERROR: with XML configuration"); + pce.printStackTrace(System.err); + } + catch (IOException ioe) { + System.err.println("ERROR: I/O while processing file '" + + file + "'"); + ioe.printStackTrace(System.err); + } + + return null; + } + + public static final String getStringXPath(String xpath) { + return getStringXPath(xpath, null); + } + + public static final String getStringXPath(String query, String def) { + String s = (String)getXPath(query, XPathConstants.STRING); + return s == null || s.length() == 0 + ? def + : s; + } + + public static final Object getXPath(String query, QName returnType) { + Document document = getConfig(); + if (document == null) { + return null; + } + + XPathFactory factory = XPathFactory.newInstance(); + XPath xpath = factory.newXPath(); + + try { + return xpath.evaluate(query, document, returnType); + } + catch (XPathExpressionException xpee) { + xpee.printStackTrace(System.err); + } + + return null; + } + + public static final NodeList getNodeSetXPath(String query) { + return (NodeList)getXPath(query, XPathConstants.NODESET); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: + diff -r 13a12b607baf -r 8f2de197bce2 artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactContext.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactContext.java Fri Sep 04 12:04:12 2009 +0000 @@ -0,0 +1,40 @@ +package de.intevation.artifactdatabase; + +import java.util.HashMap; + +import org.w3c.dom.Document; + +/** + * Default implementation of the context. + * Besides of the configuration it hosts a map to store key/value pairs. + * + * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) + */ +public class DefaultArtifactContext +{ + protected Document config; + + protected HashMap map; + + public DefaultArtifactContext() { + this(null); + } + + public DefaultArtifactContext(Document config) { + this.config = config; + map = new HashMap(); + } + + public synchronized Object get(Object key) { + return map.get(key); + } + + public synchronized Object put(Object key, Object value) { + return map.put(key, value); + } + + public Document getConfig() { + return config; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: diff -r 13a12b607baf -r 8f2de197bce2 artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactContextFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactContextFactory.java Fri Sep 04 12:04:12 2009 +0000 @@ -0,0 +1,23 @@ +package de.intevation.artifactdatabase; + +import org.w3c.dom.Document; + +import de.intevation.artifacts.ArtifactContextFactory; + +/** + * Default implementation of the context factory. + * Creates a new @see DefaultArtifactContext. + * + * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) + */ +public class DefaultArtifactContextFactory +implements ArtifactContextFactory +{ + public DefaultArtifactContextFactory() { + } + + public Object createArtifactContext(Document config) { + return new DefaultArtifactContext(config); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: diff -r 13a12b607baf -r 8f2de197bce2 artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java Fri Sep 04 12:04:12 2009 +0000 @@ -0,0 +1,116 @@ +package de.intevation.artifactdatabase; + +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; + +import java.util.ArrayList; + +import de.intevation.artifacts.ArtifactFactory; +import de.intevation.artifacts.ArtifactContextFactory; + +/** + * Bootstrap facility for the global context and the artifact factories. + * + * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) + */ +public class FactoryBootstrap +{ + public static final String CONTEXT_FACTORY = + "/artifact-database/factories/context-factory/text()"; + + public static final String DEFAULT_CONTEXT_FACTORY = + "de.intevation.artifactdatabase.DefaultArtifactContextFactory"; + + public static final String ARTIFACT_FACTORIES = + "/artifact-database/factories/artifact-factories/artifact-factory/text()"; + + protected Object context; + + protected ArtifactFactory [] artifactFactories; + + public FactoryBootstrap() { + } + + void buildContext() { + String className = Config.getStringXPath( + CONTEXT_FACTORY, DEFAULT_CONTEXT_FACTORY); + + ArtifactContextFactory factory = null; + + try { + Class clazz = Class.forName(className); + factory = (ArtifactContextFactory)clazz.newInstance(); + } + catch (ClassNotFoundException cnfe) { + cnfe.printStackTrace(System.err); + } + catch (InstantiationException ie) { + ie.printStackTrace(System.err); + } + catch (ClassCastException cce) { + cce.printStackTrace(System.err); + } + catch (IllegalAccessException iae) { + iae.printStackTrace(System.err); + } + + if (factory == null) { + factory = new DefaultArtifactContextFactory(); + } + + context = factory.createArtifactContext(Config.getConfig()); + } + + protected void loadArtifactFactories() { + ArrayList loadedFactories = new ArrayList(); + + NodeList nodes = Config.getNodeSetXPath(ARTIFACT_FACTORIES); + + Document config = Config.getConfig(); + + for (int i = 0, N = nodes.getLength(); i < N; ++i) { + String className = nodes.item(i).getTextContent(); + + ArtifactFactory factory = null; + + try { + Class clazz = Class.forName(className); + factory = (ArtifactFactory)clazz.newInstance(); + } + catch (ClassNotFoundException cnfe) { + cnfe.printStackTrace(System.err); + } + catch (InstantiationException ie) { + ie.printStackTrace(System.err); + } + catch (ClassCastException cce) { + cce.printStackTrace(System.err); + } + catch (IllegalAccessException iae) { + iae.printStackTrace(System.err); + } + + if (factory != null) { + factory.setup(config); + loadedFactories.add(factory); + } + } + + artifactFactories = (ArtifactFactory [])loadedFactories.toArray( + new ArtifactFactory[loadedFactories.size()]); + } + + public void boot() { + buildContext(); + loadArtifactFactories(); + } + + public ArtifactFactory [] getArtifactFactories() { + return artifactFactories; + } + + public Object getContext() { + return context; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: