changeset 69:498bb84334d0

Added base class for services and service factories. artifacts/trunk@595 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 22 Jan 2010 10:19:27 +0000
parents 541814404822
children ce488c1d3fc4
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultService.java artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultServiceFactory.java artifacts/src/main/java/de/intevation/artifacts/Service.java
diffstat 4 files changed, 131 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Jan 22 09:48:18 2010 +0000
+++ b/ChangeLog	Fri Jan 22 10:19:27 2010 +0000
@@ -1,3 +1,12 @@
+2010-01-17	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultService.java:
+	  Default implementation of a service. Useful as a base class.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultServiceFactory.java:
+	  Default implementation of a service factory. Modelled right after
+	  DefaultArtifactFactory. Useful as a base class.
+
 2010-01-17	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifacts/src/main/java/de/intevation/artifacts/Service.java: New.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultService.java	Fri Jan 22 10:19:27 2010 +0000
@@ -0,0 +1,29 @@
+package de.intevation.artifactdatabase;
+
+import de.intevation.artifacts.CallMeta;
+import de.intevation.artifacts.Service;
+import de.intevation.artifacts.ServiceFactory;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+
+public class DefaultService
+implements   Service
+{
+    private static Logger logger = Logger.getLogger(DefaultService.class);
+
+    public Document process(Document data, Object globalContext, CallMeta callMeta) {
+        if (logger.isDebugEnabled()) {
+            logger.debug("Service.process");
+        }
+        return XMLUtils.newDocument();
+    }
+
+    public void setup(ServiceFactory factory, Object globalContext) {
+        if (logger.isDebugEnabled()) {
+            logger.debug("Service.setup");
+        }
+    }
+}
+// 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/DefaultServiceFactory.java	Fri Jan 22 10:19:27 2010 +0000
@@ -0,0 +1,91 @@
+package de.intevation.artifactdatabase;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+
+import de.intevation.artifacts.ServiceFactory;
+import de.intevation.artifacts.Service;
+
+import org.apache.log4j.Logger;
+
+/**
+ * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
+ */
+public class DefaultServiceFactory
+implements   ServiceFactory
+{
+    private static Logger logger =
+        Logger.getLogger(DefaultServiceFactory.class);
+
+    public static final String XPATH_NAME        = "@name";
+    public static final String XPATH_DESCRIPTION = "@description";
+    public static final String XPATH_SERVICE     = "@service";
+
+    public static final String DEFAULT_DESCRIPTION =
+        "No description available";
+
+    public static final String DEFAULT_SERVICE =
+        "de.intevation.artifactdatabase.DefaultService";
+
+    protected String name;
+
+    protected String description;
+
+    protected Class  serviceClass;
+
+    public DefaultServiceFactory() {
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public Service createService(Object globalContext) {
+        try {
+            Service service = (Service)serviceClass.newInstance();
+
+            service.setup(this, globalContext);
+
+            return service;
+        }
+        catch (InstantiationException ie) {
+            logger.error(ie.getLocalizedMessage(), ie);
+        }
+        catch (IllegalAccessException iae) {
+            logger.error(iae.getLocalizedMessage(), iae);
+        }
+        catch (ClassCastException cce) {
+            logger.error(cce.getLocalizedMessage(), cce);
+        }
+
+        return null;
+    }
+
+    public void setup(Document config, Node factoryNode) {
+
+        description = Config.getStringXPath(
+            factoryNode, XPATH_DESCRIPTION, DEFAULT_DESCRIPTION);
+
+        name = Config.getStringXPath(
+            factoryNode, XPATH_NAME, toString());
+
+        String service = Config.getStringXPath(
+            factoryNode, XPATH_SERVICE, DEFAULT_SERVICE);
+
+        try {
+            serviceClass = Class.forName(service);
+        }
+        catch (ClassNotFoundException cnfe) {
+            logger.error(cnfe.getLocalizedMessage(), cnfe);
+        }
+
+        if (serviceClass == null) {
+            serviceClass = DefaultService.class;
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifacts/src/main/java/de/intevation/artifacts/Service.java	Fri Jan 22 09:48:18 2010 +0000
+++ b/artifacts/src/main/java/de/intevation/artifacts/Service.java	Fri Jan 22 10:19:27 2010 +0000
@@ -11,5 +11,7 @@
 extends          Serializable
 {
     Document process(Document data, Object globalContext, CallMeta callMeta);
+
+    void setup(ServiceFactory factory, Object globalContext);
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org