changeset 19:1259d192e3c3

* New configuration based on config directory * Artifact database launches REST web server at startup. artifacts/trunk@43 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 09 Sep 2009 07:55:44 +0000
parents 63a8aa276693
children ff666592c1c3
files Changelog artifact-database/doc/artifactdb-example-conf.xml artifact-database/doc/example-conf/conf.xml artifact-database/doc/example-conf/log4j.properties 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/DBConnection.java artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java contrib/run.sh
diffstat 11 files changed, 265 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/Changelog	Tue Sep 08 07:51:34 2009 +0000
+++ b/Changelog	Wed Sep 09 07:55:44 2009 +0000
@@ -1,7 +1,56 @@
-2009-09-08  Tim Englich  <tim.englich@intevation.de>
+2009-09-09	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
-    * src/main/java/de/intevation/artifactdatabase/DefaultArtifact.java : Edited
-    Constants for Namespace-URL and Namespace-Prefix added 
+	* artifact-database/doc/artifactdb-example-conf.xml: removed
+	* artifact-database/doc/example-conf/conf.xml: Re-added here.
+
+	The configuration is now found in a subdirectory set by the
+	system property 'artifact.database.dir'. If this property is
+	not set '~/.artitactdb' is used. This directory should contain
+	a file 'conf.xml' with the configuration details.
+	  If the artifact database is started as a standalone the config
+	directory is searched for 'log4j.properties' to configure the
+	logging.
+	  The H2 database is also search in this directory with the
+	name 'artifacts.db' if the connection url in config.xml is
+	not overwritten.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Config.java:
+	Refactored a bit to follow the new configuration policy.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/DBConnection.java:
+	Build default connection url as described.
+
+	* artifact-database/doc/example-conf/log4j.properties: New.
+	example logging config
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java:
+	Added some more logging infos.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/App.java:
+	Load the log4j configuration at startup and starts a REST standalone web server.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest: New.
+	Package for REST binding.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java:
+	Convenience wrapper to start a standalone REST web server.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java:
+	Concrete REST application. TODO: attach restlets to interact with artifact database.
+
+	* contrib/run.sh: Little start script to launch artifact database for test
+	purpose from classes without the need to build packages. 
+	Background: The maven exec:exec goal does not work consistently for me (slt). 
+	At home it does, at work it doe not. Anyway. Usage:
+
+	$ ./contrib/run.sh \
+	      -Dartifact.database.dir=artifact-database/doc/example-conf/ \
+	      de.intevation.artifactdatabase.App
+
+2009-09-08	Tim Englich	<tim.englich@intevation.de>
+
+	* src/main/java/de/intevation/artifactdatabase/DefaultArtifact.java : Edited
+	Constants for Namespace-URL and Namespace-Prefix added 
 
 2009-09-08	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
--- a/artifact-database/doc/artifactdb-example-conf.xml	Tue Sep 08 07:51:34 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<artifact-database>
-    <factories>
-        <context-factory>de.intevation.artifactdatabase.DefaultArtifactContextFactory</context-factory>
-        <artifact-factories>
-            <artifact-factory
-             name="dummy-1" description="dummy description 1" ttl="60000"
-             artifact="de.intevation.artifactdatabase.DefaultArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
-            <artifact-factory
-             name="dummy-2" description="dummy description 2" ttl="1800000"
-             artifact="de.intevation.artifactdatabase.DefaultArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
-            <artifact-factory
-             name="dummy-3" description="dummy description 3"
-             artifact="de.intevation.artifactdatabase.DefaultArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
-        </artifact-factories>
-    </factories>
-    <database>
-        <user></user>
-        <password></password>
-        <!-- <url>jdbc:h2:artifacts.db</url> -->
-        <!-- <driver>org.h2.Driver</driver> -->
-        <!-- <sql></sql> -->
-    </database>
-</artifact-database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/doc/example-conf/conf.xml	Wed Sep 09 07:55:44 2009 +0000
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<artifact-database>
+    <factories>
+        <context-factory>de.intevation.artifactdatabase.DefaultArtifactContextFactory</context-factory>
+        <artifact-factories>
+            <artifact-factory
+             name="dummy-1" description="dummy description 1" ttl="60000"
+             artifact="de.intevation.artifactdatabase.DefaultArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
+            <artifact-factory
+             name="dummy-2" description="dummy description 2" ttl="1800000"
+             artifact="de.intevation.artifactdatabase.DefaultArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
+            <artifact-factory
+             name="dummy-3" description="dummy description 3"
+             artifact="de.intevation.artifactdatabase.DefaultArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
+        </artifact-factories>
+    </factories>
+    <!-- configuration of the rest server -->
+    <rest-server>
+        <port>8181</port>
+    </rest-server>
+    <database>
+        <user></user>
+        <password></password>
+        <!-- <url>jdbc:h2:artifacts.db</url> -->
+        <!-- <driver>org.h2.Driver</driver> -->
+        <!-- <sql></sql> -->
+    </database>
+</artifact-database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/doc/example-conf/log4j.properties	Wed Sep 09 07:55:44 2009 +0000
@@ -0,0 +1,9 @@
+# Set root logger level to DEBUG and its only appender to A1.
+log4j.rootLogger=DEBUG, A1
+
+# A1 is set to be a ConsoleAppender.
+log4j.appender.A1=org.apache.log4j.ConsoleAppender
+
+# A1 uses PatternLayout.
+log4j.appender.A1.layout=org.apache.log4j.PatternLayout
+log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/App.java	Tue Sep 08 07:51:34 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/App.java	Wed Sep 09 07:55:44 2009 +0000
@@ -1,5 +1,13 @@
 package de.intevation.artifactdatabase;
 
+import de.intevation.artifactdatabase.rest.Standalone;
+
+import java.io.File;
+
+import java.net.MalformedURLException;
+
+import org.apache.log4j.PropertyConfigurator;
+
 /**
  * Starting point of the artifact database.
  *
@@ -7,7 +15,27 @@
  */
 public class App
 {
+    public static final String LOG4J_PROPERTIES =
+        "log4j.properties";
+
+    public static final void configureLogging() {
+        File configDir = Config.getConfigDirectory();
+        File propFile = new File(configDir, LOG4J_PROPERTIES);
+
+        if (propFile.isFile() && propFile.canRead()) {
+            try {
+                PropertyConfigurator.configure(propFile.toURI().toURL());
+            }
+            catch (MalformedURLException mue) {
+                mue.printStackTrace(System.err);
+            }
+        }
+    }
+
     public static void main(String[] args) {
+
+        configureLogging();
+
         FactoryBootstrap bootstrap = new FactoryBootstrap();
 
         bootstrap.boot();
@@ -16,6 +44,8 @@
 
         ArtifactDatabaseImpl db = new ArtifactDatabaseImpl(
             bootstrap, backend);
+
+        Standalone.startAsServer(db);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Config.java	Tue Sep 08 07:51:34 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Config.java	Wed Sep 09 07:55:44 2009 +0000
@@ -29,9 +29,13 @@
 {
     private static Logger logger = Logger.getLogger(Config.class);
 
-    public static final String CONFIG_PROPERTY = "artifact.database.config";
+    public static final String CONFIG_DIR = "artifact.database.dir";
 
-    public static final String CONFIG_DEFAULT  = "artifactdb-conf.xml";
+    public static final File CONFIG_DIR_DEFAULT =
+        new File(new File(System.getProperty("user.home",
+            System.getProperty("user.dir", "."))), ".artifactdb");
+
+    public static final String CONFIG_FILE  = "conf.xml";
 
     private static Document config;
 
@@ -45,9 +49,26 @@
         return config;
     }
 
+    public static File getConfigDirectory() {
+        String configDirString = System.getProperty(CONFIG_DIR);
+
+        File configDir = configDirString != null
+            ? new File(configDirString)
+            : CONFIG_DIR_DEFAULT;
+
+        if (!configDir.isDirectory()) {
+            logger.warn("'" + configDir + "' is not a directory.");
+            configDir = CONFIG_DIR_DEFAULT;
+        }
+
+        return configDir;
+    }
+
     private static Document loadConfig() {
-        File file = new File(
-            System.getProperty(CONFIG_PROPERTY, CONFIG_DEFAULT));
+
+        File configDir = getConfigDirectory();
+
+        File file = new File(configDir, CONFIG_FILE);
 
         if (!file.canRead() && !file.isFile()) {
             logger.error("Cannot read config file '"
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/DBConnection.java	Tue Sep 08 07:51:34 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/DBConnection.java	Wed Sep 09 07:55:44 2009 +0000
@@ -6,6 +6,8 @@
 
 import org.apache.commons.dbcp.BasicDataSource;
 
+import java.io.File;
+
 /**
  *  @author Sascha L. Teichmann
  */
@@ -23,8 +25,10 @@
     public static final String DEFAULT_DRIVER =
         "org.h2.Driver";
 
-    public static final String DEFAULT_URL =
-        "jdbc:h2:artifacts.db";
+    public static final String DEFAULT_DATABASE_FILE =
+        "artifacts.db";
+
+    public static final String DEFAULT_URL = getDefaultURL();
 
     public static final String DEFAULT_USER     = "";
     public static final String DEFAULT_PASSWORD = "";
@@ -32,6 +36,12 @@
     private DBConnection() {
     }
 
+    public static final String getDefaultURL() {
+        File configDir = Config.getConfigDirectory();
+        File databaseFile = new File(configDir, DEFAULT_DATABASE_FILE);
+        return "jdbc:h2:" + databaseFile;
+    }
+
     private static BasicDataSource dataSource;
 
     private static final void addShutdownHook() {
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java	Tue Sep 08 07:51:34 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java	Wed Sep 09 07:55:44 2009 +0000
@@ -62,6 +62,9 @@
             factory = new DefaultArtifactContextFactory();
         }
 
+        logger.info("Using class '" + factory.getClass().getName()
+            + "' for context creation.");
+
         context = factory.createArtifactContext(Config.getConfig());
     }
 
@@ -72,12 +75,11 @@
 
         if (nodes == null) {
             logger.warn("No factories found");
-            return;
         }
 
         Document config = Config.getConfig();
 
-        for (int i = 0, N = nodes.getLength(); i < N; ++i) {
+        for (int i = 0, N = nodes != null ? nodes.getLength() : 0; i < N; ++i) {
             String className = nodes.item(i).getTextContent();
 
             ArtifactFactory factory = null;
@@ -100,8 +102,9 @@
             }
 
             if (factory != null) {
-                factory.setup(config,nodes.item(i));
+                factory.setup(config, nodes.item(i));
                 loadedFactories.add(factory);
+                logger.info("Registering '" + factory.getName() + "' as artifact factory.");
             }
         }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java	Wed Sep 09 07:55:44 2009 +0000
@@ -0,0 +1,35 @@
+package de.intevation.artifactdatabase.rest;
+
+import de.intevation.artifacts.ArtifactDatabase;
+
+import org.restlet.Application;
+import org.restlet.Restlet;
+
+import org.restlet.routing.Router;
+
+/**
+ *
+ * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
+ */
+public class RestApp
+extends      Application
+{
+    protected ArtifactDatabase database;
+
+    public RestApp() {
+    }
+
+    public RestApp(ArtifactDatabase database) {
+        this.database = database;
+    }
+
+    public Restlet createRoot() {
+
+        Router router = new Router(getContext());
+
+        //router.attach(FactoriesResource.PATH, FactoriesResource.class);
+
+        return router;
+    }
+}
+// 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/rest/Standalone.java	Wed Sep 09 07:55:44 2009 +0000
@@ -0,0 +1,58 @@
+package de.intevation.artifactdatabase.rest;
+
+import org.restlet.Component;
+
+import org.restlet.data.Protocol;
+
+import de.intevation.artifacts.ArtifactDatabase;
+
+import de.intevation.artifactdatabase.Config;
+
+import de.intevation.artifactdatabase.rest.RestApp;
+
+import org.apache.log4j.Logger;
+
+public class Standalone
+{
+    private static Logger logger = Logger.getLogger(Standalone.class);
+
+    public static final String REST_PORT =
+        "/artifact-database/rest-server/port/text()";
+
+    public static final int DEFAULT_PORT = 8181;
+
+    public static void startAsServer(ArtifactDatabase db) {
+        String portString = Config.getStringXPath(REST_PORT);
+
+        int port = DEFAULT_PORT;
+
+        if (portString != null) {
+            try {
+                port = Integer.parseInt(portString);
+                if (port < 0) {
+                    throw new NumberFormatException();
+                }
+            }
+            catch (NumberFormatException nfe) {
+                logger.error("rest port is not a positive integer value.", nfe);
+                return;
+            }
+        }
+
+        RestApp app = new RestApp(db);
+
+        Component component = new Component();
+
+        component.getServers().add(Protocol.HTTP, port);
+
+        component.getDefaultHost().attach(app);
+
+        try {
+            component.start();
+        }
+        catch (Exception e) {
+            logger.error(e.getLocalizedMessage(), e);
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contrib/run.sh	Wed Sep 09 07:55:44 2009 +0000
@@ -0,0 +1,10 @@
+#!/bin/bash
+RESTLET=`find ~/.m2 -name org\.restlet\*.jar`
+H2=`find ~/.m2 -name h2-\*.jar`
+LOG4J=`find ~/.m2 -name log4j-1.2.13\*.jar`
+#echo $RESTLET
+#echo $LOG4J
+#echo $H2
+DIR=`dirname $0`/..
+export CLASSPATH=$DIR/artifact-database/target/classes:$DIR/artifacts/target/classes:$RESTLET:$LOG4J:$H2
+java "$@"

http://dive4elements.wald.intevation.org