changeset 5:8f2de197bce2

Added config to artifact database and modelled bootstap of artifact factories on top of this. artifacts/trunk@15 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 04 Sep 2009 12:04:12 +0000
parents 13a12b607baf
children 48eb2367de2a
files Changelog TODO artifact-database/doc/artifactdb-example-conf.xml artifact-database/pom.xml artifact-database/src/main/java/de/intevation/artifactdatabase/App.java artifact-database/src/main/java/de/intevation/artifactdatabase/Config.java artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactContext.java artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactContextFactory.java artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java
diffstat 9 files changed, 351 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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	<sascha.teichmann@intevation.de>
+
+	* 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	<sascha.teichmann@intevation.de>
 
 	* artifacts/src/main/java/de/intevation/artifacts/ArtifactContextFactory.java:
--- 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.
--- /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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<artifact-database>
+    <factories>
+        <context-factory>com.example.Context</context-factory>
+        <artifact-factories>
+            <artifact-factory>com.example.ArtifactExampleFactoryOne</artifact-factory>
+            <artifact-factory>com.example.ArtifactExampleFactoryTwo</artifact-factory>
+            <artifact-factory>com.example.ArtifactExampleFactoryThree</artifact-factory>
+        </artifact-factories>
+    </factories>
+</artifact-database>
--- 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 @@
   <name>artifact-database</name>
   <version>1.0-SNAPSHOT</version>
   <url>http://maven.apache.org</url>
+  <build>
+    <plugins>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-jar-plugin</artifactId>
+          <version>2.2</version>
+          <configuration>
+            <archive>
+                <manifest>
+                    <mainClass>de.intevation.artifactdatabase.App</mainClass>
+                </manifest>
+            </archive>
+          </configuration>
+        </plugin>
+    </plugins>
+  </build>
   <dependencies>
     <dependency>
         <groupId>de.intevation.bsh.artifacts</groupId>
--- 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:
--- /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:
+
--- /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:
--- /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:
--- /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:

http://dive4elements.wald.intevation.org