changeset 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
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/App.java artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/HTTPServer.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java
diffstat 5 files changed, 114 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed May 18 10:10:11 2011 +0000
+++ b/ChangeLog	Sun May 22 11:21:40 2011 +0000
@@ -1,3 +1,20 @@
+2011-04-22	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/HTTPServer.java:
+	  New. Interface to run an HTTP server. Enables the system to run on different
+	  HTTP servers.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java:
+	  Implements the new interface.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java:
+	  Load and setup the HTTP server configured by the XPath 
+	  "/artifact-database/rest-server/http-server/text()" in the global config file.
+	  Defaults to "de.intevation.artifactdatabase.rest.Standalone" if not given.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/App.java: 
+	  Boot with the HTTP server configured by FactoryBootstrap.
+
 2011-04-18	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/App.java	Wed May 18 10:10:11 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/App.java	Sun May 22 11:21:40 2011 +0000
@@ -10,7 +10,7 @@
 
 import de.intevation.artifacts.common.utils.Config;
 
-import de.intevation.artifactdatabase.rest.Standalone;
+import de.intevation.artifactdatabase.rest.HTTPServer;
 
 import java.io.File;
 
@@ -72,6 +72,8 @@
         DatabaseCleaner cleaner = new DatabaseCleaner(
             bootstrap.getContext(), backend);
 
+        HTTPServer httpServer = bootstrap.getHTTPServer();
+
         bootstrap = null;
 
         backend.setCleaner(cleaner);
@@ -80,7 +82,7 @@
 
         cleaner.start();
 
-        Standalone.startAsServer(db);
+        httpServer.startAsServer(db);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java	Wed May 18 10:10:11 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java	Sun May 22 11:21:40 2011 +0000
@@ -17,6 +17,9 @@
 import de.intevation.artifacts.ServiceFactory;
 import de.intevation.artifacts.UserFactory;
 
+
+import de.intevation.artifactdatabase.rest.HTTPServer;
+
 import java.util.ArrayList;
 
 import org.apache.log4j.Logger;
@@ -101,6 +104,13 @@
     public static final String CALLCONTEXT_LISTENER =
         "/artifact-database/callcontext-listener";
 
+
+    public static final String HTTP_SERVER =
+        "/artifact-database/rest-server/http-server/text()";
+
+    public static final String DEFAULT_HTTP_SERVER =
+        "de.intevation.artifactdatabase.rest.Standalone";
+
     /**
      * Default export signing secret.
      * <strong>PLEASE CHANGE THE SECRET VIA THE XPATH EXPORT_SECRET
@@ -146,6 +156,8 @@
      */
     protected byte [] exportSecret;
 
+    protected HTTPServer httpServer;
+
 
     /**
      * Default constructor
@@ -375,7 +387,33 @@
             callContextListener.setup(Config.getConfig(), listener);
         }
         catch (ClassNotFoundException cnfe) {
-                logger.error(cnfe.getLocalizedMessage(), cnfe);
+            logger.error(cnfe.getLocalizedMessage(), cnfe);
+        }
+        catch (InstantiationException ie) {
+            logger.error(ie.getLocalizedMessage(), ie);
+        }
+        catch (ClassCastException cce) {
+            logger.error(cce.getLocalizedMessage(), cce);
+        }
+        catch (IllegalAccessException iae) {
+            logger.error(iae.getLocalizedMessage(), iae);
+        }
+    }
+
+    protected void loadHTTPServer() {
+        logger.info("loading HTTPServer");
+
+        String className = Config.getStringXPath(
+            HTTP_SERVER, DEFAULT_HTTP_SERVER);
+
+        try {
+            Class clazz = Class.forName(className);
+            httpServer  = (HTTPServer)clazz.newInstance();
+
+            httpServer.setup(Config.getConfig());
+        }
+        catch (ClassNotFoundException cnfe) {
+            logger.error(cnfe.getLocalizedMessage(), cnfe);
         }
         catch (InstantiationException ie) {
             logger.error(ie.getLocalizedMessage(), ie);
@@ -415,6 +453,7 @@
         loadServiceFactories();
         loadUserFactory();
         loadCallContextListener();
+        loadHTTPServer();
     }
 
     /**
@@ -476,5 +515,9 @@
     public CallContext.Listener getCallContextListener() {
         return callContextListener;
     }
+
+    public HTTPServer getHTTPServer() {
+        return httpServer;
+    }
 }
 // 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/HTTPServer.java	Sun May 22 11:21:40 2011 +0000
@@ -0,0 +1,13 @@
+package de.intevation.artifactdatabase.rest;
+
+import org.w3c.dom.Document;
+
+import de.intevation.artifacts.ArtifactDatabase;
+
+public interface HTTPServer
+{
+    void setup(Document document);
+
+    void startAsServer(ArtifactDatabase database);
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- 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