changeset 950:22bc87a42a0d

First step to factor out the "Datenkorb" logic into a service independent singleton. flys-artifacts/trunk@2367 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 20 Jul 2011 09:56:39 +0000
parents ea56940bfc4c
children 9686e1132a81
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java
diffstat 3 files changed, 109 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Jul 20 07:08:10 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Jul 20 09:56:39 2011 +0000
@@ -1,3 +1,12 @@
+2011-07-20  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java:
+	  New. First step to factor out the "Datenkorb" logic into a service independent
+	  singleton.
+
+	* src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java:
+	  Uses the "Datenkorb" singleton now.
+
 2011-07-20  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/ManagedFacet.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java	Wed Jul 20 07:08:10 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java	Wed Jul 20 09:56:39 2011 +0000
@@ -1,10 +1,5 @@
 package de.intevation.flys.artifacts.services;
 
-import java.io.InputStream;
-import java.io.IOException;
-import java.io.File;
-import java.io.FileInputStream;
-
 import java.sql.Connection;
 import java.sql.SQLException;
 
@@ -16,16 +11,15 @@
 import java.util.HashMap;
 
 import de.intevation.artifacts.CallMeta;
-import de.intevation.artifacts.ServiceFactory;
 
 import de.intevation.artifactdatabase.DefaultService;
 
-import de.intevation.artifacts.common.utils.Config;
 import de.intevation.artifacts.common.utils.XMLUtils;
 
 import de.intevation.artifacts.common.ArtifactNamespaceContext;
 
 import de.intevation.flys.artifacts.services.meta.Builder;
+import de.intevation.flys.artifacts.services.meta.DataCage;
 
 import de.intevation.flys.backend.SessionHolder;
 
@@ -38,13 +32,8 @@
 {
     private static Logger log = Logger.getLogger(MetaDataService.class);
 
-    public static final String XPATH_META_DATA_TEMPLATE =
-        "/artifact-database/metadata/@template";
-
     public static final String XPATH_RIVER = "/art:river/text()";
 
-    protected Builder builder;
-
     public MetaDataService() {
     }
 
@@ -73,6 +62,8 @@
 
         final Document result = XMLUtils.newDocument();
 
+        final Builder builder = DataCage.getInstance().getBuilder();
+
         if (builder == null) {
             log.error("MetaDataService is not setup properly.");
             return result;
@@ -99,52 +90,5 @@
 
         return result;
     }
-
-    @Override
-    public void setup(ServiceFactory factory, Object globalContext) {
-        log.debug("MetaDataService.setup");
-
-        String path = Config.getStringXPath(XPATH_META_DATA_TEMPLATE);
-
-        if (path == null) {
-            log.error("no path to template file given");
-            return;
-        }
-
-        path = Config.replaceConfigDir(path);
-
-        File file = new File(path);
-
-        if (!file.isFile() || !file.canRead()) {
-            log.error("Cannot open template file '" + file + "'");
-            return;
-        }
-
-        InputStream in = null;
-
-        try {
-            in = new FileInputStream(file);
-            Document template = XMLUtils.parseDocument(in);
-            if (template == null) {
-                log.error("cannot parse meta data template");
-            }
-            else {
-                builder = new Builder(template);
-            }
-        }
-        catch (IOException ioe) {
-            log.error(ioe);
-        }
-        finally {
-            if (in != null) {
-                try {
-                    in.close();
-                }
-                catch (IOException ioe) {
-                    log.error(ioe);
-                }
-            }
-        }
-    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java	Wed Jul 20 09:56:39 2011 +0000
@@ -0,0 +1,97 @@
+package de.intevation.flys.artifacts.services.meta;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.File;
+
+import java.io.FileInputStream;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+
+import de.intevation.artifacts.common.utils.Config;
+import de.intevation.artifacts.common.utils.XMLUtils;
+
+public class DataCage
+{
+    private static Logger log = Logger.getLogger(DataCage.class);
+
+    public static final String XPATH_META_DATA_TEMPLATE =
+        "/artifact-database/metadata/@template";
+
+    private static DataCage INSTANCE;
+
+    protected Builder builder;
+
+    public DataCage() {
+    }
+
+    public DataCage(Builder builder) {
+        this.builder = builder;
+    }
+
+    public Builder getBuilder() {
+        return builder;
+    }
+
+    public static synchronized DataCage getInstance() {
+        if (INSTANCE == null) {
+            INSTANCE = createDataCage();
+        }
+        return INSTANCE;
+    }
+
+    protected static Builder createBuilder(File file) {
+        log.debug("DataCage.createBuilder");
+
+        if (!file.isFile() || !file.canRead()) {
+            log.error("Cannot open template file '" + file + "'");
+            return null;
+        }
+
+        InputStream in = null;
+
+        try {
+            in = new FileInputStream(file);
+
+            Document template = XMLUtils.parseDocument(in);
+
+            if (template == null) {
+                log.error("cannot parse meta data template");
+            }
+            else {
+                return new Builder(template);
+            }
+        }
+        catch (IOException ioe) {
+            log.error(ioe);
+        }
+        finally {
+            if (in != null) {
+                try {
+                    in.close();
+                }
+                catch (IOException ioe) {
+                    log.error(ioe);
+                }
+            }
+        }
+        return null;
+    }
+
+    protected static DataCage createDataCage() {
+        log.debug("DataCage.createDataCage");
+
+        String path = Config.getStringXPath(XPATH_META_DATA_TEMPLATE);
+        if (path == null) {
+            log.error("no path to template file given");
+            return null;
+        }
+
+        path = Config.replaceConfigDir(path);
+
+        return new DataCage(createBuilder(new File(path)));
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org