[PATCH] Add setup(Element config) method to Generator interface to be initialized from configuration

Wald Commits scm-commit at wald.intevation.org
Wed Sep 18 15:04:29 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1379509466 -7200
# Branch generator-refactoring
# Node ID 5c6fd2c010ddf0493c6180a6904f4c549847f46f
# Parent  557cb3a3d7723bd19d2d973c1a88302f6ce2f50e
Add setup(Element config) method to Generator interface to be initialized from configuration.

diff -r 557cb3a3d772 -r 5c6fd2c010dd artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContext.java
--- 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);
diff -r 557cb3a3d772 -r 5c6fd2c010dd artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContextFactory.java
--- 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++;
             }
diff -r 557cb3a3d772 -r 5c6fd2c010dd artifacts/src/main/java/org/dive4elements/river/exports/ATExporter.java
--- 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;
diff -r 557cb3a3d772 -r 5c6fd2c010dd artifacts/src/main/java/org/dive4elements/river/exports/AbstractExporter.java
--- 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) {
diff -r 557cb3a3d772 -r 5c6fd2c010dd artifacts/src/main/java/org/dive4elements/river/exports/ChartGenerator.java
--- 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.
diff -r 557cb3a3d772 -r 5c6fd2c010dd artifacts/src/main/java/org/dive4elements/river/exports/ChartInfoGenerator.java
--- 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.
diff -r 557cb3a3d772 -r 5c6fd2c010dd artifacts/src/main/java/org/dive4elements/river/exports/MapGenerator.java
--- 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) {
diff -r 557cb3a3d772 -r 5c6fd2c010dd artifacts/src/main/java/org/dive4elements/river/exports/OutGenerator.java
--- 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.
      *
diff -r 557cb3a3d772 -r 5c6fd2c010dd artifacts/src/main/java/org/dive4elements/river/exports/ReportGenerator.java
--- 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;
diff -r 557cb3a3d772 -r 5c6fd2c010dd artifacts/src/main/java/org/dive4elements/river/exports/ShapeExporter.java
--- 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;
diff -r 557cb3a3d772 -r 5c6fd2c010dd artifacts/src/main/java/org/dive4elements/river/exports/sq/SQOverviewGenerator.java
--- 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.
diff -r 557cb3a3d772 -r 5c6fd2c010dd artifacts/src/main/java/org/dive4elements/river/utils/Pair.java
--- 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) {


More information about the Dive4elements-commits mailing list