changeset 7037:5c6fd2c010dd generator-refactoring

Add setup(Element config) method to Generator interface to be initialized from configuration.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 18 Sep 2013 15:04:26 +0200
parents 557cb3a3d772
children 59e477500a50
files artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContext.java artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContextFactory.java artifacts/src/main/java/org/dive4elements/river/exports/ATExporter.java artifacts/src/main/java/org/dive4elements/river/exports/AbstractExporter.java artifacts/src/main/java/org/dive4elements/river/exports/ChartGenerator.java artifacts/src/main/java/org/dive4elements/river/exports/ChartInfoGenerator.java artifacts/src/main/java/org/dive4elements/river/exports/MapGenerator.java artifacts/src/main/java/org/dive4elements/river/exports/OutGenerator.java artifacts/src/main/java/org/dive4elements/river/exports/ReportGenerator.java artifacts/src/main/java/org/dive4elements/river/exports/ShapeExporter.java artifacts/src/main/java/org/dive4elements/river/exports/sq/SQOverviewGenerator.java artifacts/src/main/java/org/dive4elements/river/utils/Pair.java
diffstat 12 files changed, 73 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContext.java	Wed Sep 18 10:12:47 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContext.java	Wed Sep 18 15:04:26 2013 +0200
@@ -13,10 +13,12 @@
 import org.apache.log4j.Logger;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 
 import org.dive4elements.artifactdatabase.DefaultArtifactContext;
 import org.dive4elements.artifacts.CallContext;
 import org.dive4elements.river.exports.OutGenerator;
+import org.dive4elements.river.utils.Pair;
 
 
 /**
@@ -99,17 +101,26 @@
             ? (RiverContext) context
             : (RiverContext) context.globalContext();
 
-        Map<String, Class> generators = (Map<String, Class>)
-            flysContext.get(RiverContext.OUTGENERATORS_KEY);
+        Map<String, Pair<Class<OutGenerator>, Element>> generators =
+            (Map<String, Pair<Class<OutGenerator>, Element>>)flysContext
+                .get(RiverContext.OUTGENERATORS_KEY);
 
         if (generators == null) {
             return null;
         }
 
-        Class clazz = generators.get(name);
+        Pair<Class<OutGenerator>, Element> pair = generators.get(name);
+
+        if (pair == null) {
+            logger.warn("No generator class found for " + name);
+            return null;
+        }
 
         try {
-            return clazz != null ? (OutGenerator) clazz.newInstance() : null;
+            Class<OutGenerator> clazz = pair.getA();
+            OutGenerator generator = clazz.newInstance();
+            generator.setup(pair.getB());
+            return generator;
         }
         catch (InstantiationException ie) {
             logger.error(ie, ie);
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContextFactory.java	Wed Sep 18 10:12:47 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContextFactory.java	Wed Sep 18 15:04:26 2013 +0200
@@ -24,6 +24,8 @@
 import org.dive4elements.river.themes.ThemeFactory;
 import org.dive4elements.river.themes.ThemeGroup;
 import org.dive4elements.river.themes.ThemeMapping;
+import org.dive4elements.river.utils.Pair;
+import org.dive4elements.river.exports.OutGenerator;
 
 import java.io.File;
 import java.util.ArrayList;
@@ -307,7 +309,6 @@
      * @param context the RiverContext.
      */
     protected void configureOutGenerators(Document config, RiverContext context){
-        Map<String, Class<?>> generators = new HashMap<String, Class<?>>();
 
         NodeList outGenerators = (NodeList) XMLUtils.xpath(
             config,
@@ -323,10 +324,13 @@
 
         logger.info("Found " + num + " configured output generators.");
 
+        Map<String, Pair<Class<OutGenerator>, Element>> generators =
+            new HashMap<String, Pair<Class<OutGenerator>, Element>>();
+
         int idx = 0;
 
         for (int i = 0; i < num; i++) {
-            Node item = outGenerators.item(i);
+            Element item = (Element)outGenerators.item(i);
 
             String name = (String) XMLUtils.xpath(
                 item, "@name", XPathConstants.STRING);
@@ -339,7 +343,10 @@
             }
 
             try {
-                generators.put(name, Class.forName(clazz));
+                generators.put(
+                    name,
+                    new Pair<Class<OutGenerator>, Element>(
+                        (Class<OutGenerator>)Class.forName(clazz), item));
 
                 idx++;
             }
--- a/artifacts/src/main/java/org/dive4elements/river/exports/ATExporter.java	Wed Sep 18 10:12:47 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/ATExporter.java	Wed Sep 18 15:04:26 2013 +0200
@@ -13,6 +13,7 @@
 import java.io.IOException;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 
 import org.apache.log4j.Logger;
 
@@ -53,6 +54,11 @@
     }
 
     @Override
+    public void setup(Element config) {
+        logger.debug("ATExporter.setup");
+    }
+
+    @Override
     public void init(Document request, OutputStream out, CallContext context) {
         this.context = context;
         this.out     = out;
--- a/artifacts/src/main/java/org/dive4elements/river/exports/AbstractExporter.java	Wed Sep 18 10:12:47 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/AbstractExporter.java	Wed Sep 18 15:04:26 2013 +0200
@@ -15,6 +15,7 @@
 import java.text.NumberFormat;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 
 import org.apache.log4j.Logger;
 
@@ -115,6 +116,10 @@
      */
     protected abstract void addData(Object data);
 
+    public void setup(Element config) {
+        logger.debug("AbstractExporter.setup");
+    }
+
 
     @Override
     public void init(Document request, OutputStream out, CallContext context) {
--- a/artifacts/src/main/java/org/dive4elements/river/exports/ChartGenerator.java	Wed Sep 18 10:12:47 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/ChartGenerator.java	Wed Sep 18 15:04:26 2013 +0200
@@ -159,6 +159,10 @@
         datasets = new TreeMap<Integer, AxisDataset>();
     }
 
+    @Override
+    public void setup(Element config) {
+        logger.debug("ChartGenerator.setup");
+    }
 
     /**
      * Adds annotations to list. The given annotation will be visible.
--- a/artifacts/src/main/java/org/dive4elements/river/exports/ChartInfoGenerator.java	Wed Sep 18 10:12:47 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/ChartInfoGenerator.java	Wed Sep 18 15:04:26 2013 +0200
@@ -23,6 +23,7 @@
 import java.awt.image.BufferedImage;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 
 import org.apache.log4j.Logger;
 
@@ -69,6 +70,10 @@
         this.generator = generator;
     }
 
+    public void setup(Element config) {
+        logger.debug("ChartInfoGenerator.setup");
+    }
+
 
     /**
      * Dispatches the operation to the instantiated generator.
--- a/artifacts/src/main/java/org/dive4elements/river/exports/MapGenerator.java	Wed Sep 18 10:12:47 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/MapGenerator.java	Wed Sep 18 15:04:26 2013 +0200
@@ -65,7 +65,10 @@
 
     protected String srid;
 
-
+    @Override
+    public void setup(Element config) {
+        logger.debug("MapGenerator.setup");
+    }
 
     @Override
     public void init(Document request, OutputStream out, CallContext context) {
--- a/artifacts/src/main/java/org/dive4elements/river/exports/OutGenerator.java	Wed Sep 18 10:12:47 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/OutGenerator.java	Wed Sep 18 15:04:26 2013 +0200
@@ -12,6 +12,7 @@
 import java.io.OutputStream;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 
 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
 import org.dive4elements.artifactdatabase.state.Settings;
@@ -29,6 +30,11 @@
 public interface OutGenerator {
 
     /**
+     * Pre-initialize generator from configuration.
+     */
+    void setup(Element config);
+
+    /**
      * Initializes the OutGenerator with meta information which are necessary
      * for the output generation.
      *
--- a/artifacts/src/main/java/org/dive4elements/river/exports/ReportGenerator.java	Wed Sep 18 10:12:47 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/ReportGenerator.java	Wed Sep 18 15:04:26 2013 +0200
@@ -27,6 +27,7 @@
 import org.dive4elements.river.themes.ThemeDocument;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 
 public class ReportGenerator
 implements   OutGenerator
@@ -41,6 +42,11 @@
     }
 
     @Override
+    public void setup(Element config) {
+        logger.debug("ReportGenerator.setup");
+    }
+
+    @Override
     public void init(Document request, OutputStream out, CallContext context) {
         logger.debug("init");
         this.out     = out;
--- a/artifacts/src/main/java/org/dive4elements/river/exports/ShapeExporter.java	Wed Sep 18 10:12:47 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/ShapeExporter.java	Wed Sep 18 15:04:26 2013 +0200
@@ -16,6 +16,7 @@
 import org.dive4elements.river.collections.D4EArtifactCollection;
 import org.dive4elements.river.themes.ThemeDocument;
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 
 
 public class ShapeExporter implements OutGenerator
@@ -31,6 +32,11 @@
     private File dir;
 
     @Override
+    public void setup(Element config) {
+        logger.debug("ShapeExporter.setup");
+    }
+
+    @Override
     public void init(Document request, OutputStream out, CallContext context) {
         this.request = request;
         this.out = out;
--- a/artifacts/src/main/java/org/dive4elements/river/exports/sq/SQOverviewGenerator.java	Wed Sep 18 10:12:47 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/sq/SQOverviewGenerator.java	Wed Sep 18 15:04:26 2013 +0200
@@ -62,6 +62,11 @@
 
     protected List<JFreeChart> charts;
 
+    @Override
+    public void setup(Element config) {
+        logger.debug("SQOverviewGenerator.setup");
+    }
+
     /**
      * Produce output.
      * @param artifactAndFacet current facet and artifact.
--- a/artifacts/src/main/java/org/dive4elements/river/utils/Pair.java	Wed Sep 18 10:12:47 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/Pair.java	Wed Sep 18 15:04:26 2013 +0200
@@ -21,8 +21,7 @@
     private A a;
     private B b;
 
-    @SuppressWarnings("unused")
-    private Pair() {
+    public Pair() {
     }
 
     public Pair(A a, B b) {

http://dive4elements.wald.intevation.org