diff artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java @ 263:c0fb96f88ad1

Make used HTTP server exchangeable. artifacts/trunk@1974 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 22 May 2011 11:21:40 +0000
parents 5cab846eb2a3
children fa0d9acea897
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java	Wed May 18 10:10:11 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java	Sun May 22 11:21:40 2011 +0000
@@ -8,7 +8,7 @@
 
 package de.intevation.artifactdatabase.rest;
 
-import de.intevation.artifacts.common.utils.Config;
+import de.intevation.artifacts.common.utils.XMLUtils;
 
 import de.intevation.artifacts.ArtifactDatabase;
 
@@ -19,6 +19,8 @@
 
 import org.restlet.data.Protocol;
 
+import org.w3c.dom.Document;
+
 /**
  * Starts an HTTP server bound to a RestApp.
  * The server (binding interface and port) is configure via the
@@ -27,6 +29,7 @@
  * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a>
  */
 public class Standalone
+implements   HTTPServer
 {
     private static Logger logger = Logger.getLogger(Standalone.class);
 
@@ -55,22 +58,20 @@
     public static final String MAX_THREADS_DEFAULT =
         "1024";
 
-    /**
-     * Builds a RestApp wrapped around the given artifact database,
-     * and bind this application to HTTP server. The HTTP server
-     * is configured by the global configuration. If no port is
-     * given by the configuration the default port is used. If
-     * no interface is given the HTTP server is reachable from
-     * all interfaces.
-     * @param db The artifact database to be exposed via the
-     * REST application.
-     */
-    public static void startAsServer(ArtifactDatabase db) {
-        String portString   = Config.getStringXPath(REST_PORT);
-        String listenString = Config.getStringXPath(LISTEN_INTERFACE);
-        String maxThreads   = Config.getStringXPath(MAX_THREADS);
+    protected int     port;
 
-        int port = DEFAULT_PORT;
+    protected String  listen;
+
+    protected String  maxThreads;
+
+    public Standalone() {
+    }
+
+    public void setup(Document document) {
+        String portString = XMLUtils.xpathString(document, REST_PORT, null);
+
+
+        port = DEFAULT_PORT;
 
         if (portString != null) {
             try {
@@ -85,14 +86,30 @@
             }
         }
 
+        listen     = XMLUtils.xpathString(document, LISTEN_INTERFACE, null);
+        maxThreads = XMLUtils.xpathString(document, MAX_THREADS, null);
+    }
+
+    /**
+     * Builds a RestApp wrapped around the given artifact database,
+     * and bind this application to HTTP server. The HTTP server
+     * is configured by the global configuration. If no port is
+     * given by the configuration the default port is used. If
+     * no interface is given the HTTP server is reachable from
+     * all interfaces.
+     * @param db The artifact database to be exposed via the
+     * REST application.
+     */
+    public void startAsServer(ArtifactDatabase db) {
+
         RestApp app = new RestApp(db);
 
         Component component = new Component();
 
         Server server = null;
 
-        if (listenString != null) {
-            server = new Server(Protocol.HTTP, listenString, port);
+        if (listen != null) {
+            server = new Server(Protocol.HTTP, listen, port);
         }
         else {
             server = new Server(Protocol.HTTP, port);
@@ -108,7 +125,7 @@
         component.getDefaultHost().attach(app);
 
         logger.info("Starting rest HTTP server on "
-            + (listenString != null ? listenString : "*")
+            + (listen != null ? listen : "*")
             + ":" + port);
 
         try {

http://dive4elements.wald.intevation.org