[PATCH] Added context injector to generators

Wald Commits scm-commit at wald.intevation.org
Mon Sep 8 13:17:53 CEST 2014


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1410175069 -7200
# Node ID 9667900536b680e4778d3db3bc1a71bc33432cca
# Parent  ed8c1a0b4cfbb14771500594e1dcff51419b2188
Added context injector to generators.

diff -r ed8c1a0b4cfb -r 9667900536b6 artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContext.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContext.java	Mon Sep 08 12:28:16 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContext.java	Mon Sep 08 13:17:49 2014 +0200
@@ -16,7 +16,6 @@
 import org.dive4elements.artifacts.CallContext;
 import org.dive4elements.river.exports.GeneratorLookup;
 import org.dive4elements.river.exports.OutGenerator;
-import org.dive4elements.river.utils.Pair;
 
 
 /**
@@ -109,17 +108,17 @@
             return null;
         }
 
-        Pair<Class<OutGenerator>, Object> pair = generators.getGenerator(name);
+        GeneratorLookup.Item item = generators.getGenerator(name);
 
-        if (pair == null) {
+        if (item == null) {
             log.warn("No generator class found for " + name);
             return null;
         }
 
         try {
-            Class<OutGenerator> clazz = pair.getA();
+            Class<OutGenerator> clazz = item.getGenerator();
             OutGenerator generator = clazz.newInstance();
-            generator.setup(pair.getB());
+            generator.setup(item.getContext());
             return generator;
         }
         catch (InstantiationException ie) {
diff -r ed8c1a0b4cfb -r 9667900536b6 artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContextFactory.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContextFactory.java	Mon Sep 08 12:28:16 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContextFactory.java	Mon Sep 08 13:17:49 2014 +0200
@@ -28,6 +28,8 @@
 import org.dive4elements.artifacts.ArtifactContextFactory;
 import org.dive4elements.artifacts.GlobalContext;
 
+import org.dive4elements.artifacts.ContextInjector;
+
 import org.dive4elements.artifacts.common.utils.Config;
 import org.dive4elements.artifacts.common.utils.ElementConverter;
 import org.dive4elements.artifacts.common.utils.XMLUtils;
@@ -350,9 +352,10 @@
         for (int i = 0; i < num; i++) {
             Element item = (Element)outGenerators.item(i);
 
-            String names = item.getAttribute("names").trim();
-            String clazz = item.getAttribute("class").trim();
+            String names     = item.getAttribute("names").trim();
+            String clazz     = item.getAttribute("class").trim();
             String converter = item.getAttribute("converter").trim();
+            String injector  = item.getAttribute("injector").trim();
 
             if (names.isEmpty() || clazz.isEmpty()) {
                 continue;
@@ -388,9 +391,28 @@
                 }
             }
 
+            ContextInjector ci = null;
+
+            if (!injector.isEmpty()) {
+                try {
+                    ci = (ContextInjector)Class.forName(injector)
+                        .newInstance();
+                    ci.setup(item);
+                }
+                catch (ClassNotFoundException cnfe) {
+                    log.error(cnfe, cnfe);
+                }
+                catch (InstantiationException ie) {
+                    log.error(ie);
+                }
+                catch (IllegalAccessException iae) {
+                    log.error(iae);
+                }
+            }
+
             for (String key: names.split("[\\s,]")) {
                 if (!(key = key.trim()).isEmpty()) {
-                    generators.putGenerator(key, generatorClass, cfg);
+                    generators.putGenerator(key, generatorClass, cfg, ci);
                     idx++;
                 }
             }
diff -r ed8c1a0b4cfb -r 9667900536b6 artifacts/src/main/java/org/dive4elements/river/exports/GeneratorLookup.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/GeneratorLookup.java	Mon Sep 08 12:28:16 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/GeneratorLookup.java	Mon Sep 08 13:17:49 2014 +0200
@@ -11,43 +11,68 @@
 import java.util.HashMap;
 import java.util.Map;
 
-import org.dive4elements.river.utils.Pair;
+import org.dive4elements.artifacts.ContextInjector;
+
 import org.dive4elements.river.artifacts.D4EArtifact.FacetFilter;
 
 public class GeneratorLookup
 implements   FacetFilter
 {
-    private Map<String, Pair<Class<OutGenerator>, Object>> generators;
+    public static final class Item {
+        private Class<OutGenerator> generator;
+        private Object              ctx;
+        private ContextInjector     ci;
+
+        public Item(Class<OutGenerator> generator, Object ctx, ContextInjector ci) {
+            this.generator = generator;
+            this.ctx       = ctx;
+            this.ci        = ci;
+        }
+
+        public Class<OutGenerator> getGenerator() {
+            return generator;
+        }
+
+        public Object getContext() {
+            return ctx;
+        }
+
+        public ContextInjector getContextInjector() {
+            return ci;
+        }
+    } // class Item
+
+    private Map<String, Item> generators;
 
     public GeneratorLookup() {
-        generators = new HashMap<String, Pair<Class<OutGenerator>, Object>>();
+        generators = new HashMap<String, Item>();
     }
 
     public void putGenerator(
         String              outName,
         Class<OutGenerator> generatorClass,
-        Object              ctx
+        Object              ctx,
+        ContextInjector     ci
     ) {
-        Pair<Class<OutGenerator>, Object> pair =
-            new Pair<Class<OutGenerator>, Object>(generatorClass, ctx);
-        generators.put(outName, pair);
+        Item item = new Item(generatorClass, ctx, ci);
+        generators.put(outName, item);
     }
 
-    public Pair<Class<OutGenerator>, Object> getGenerator(String outName) {
+    public Item getGenerator(String outName) {
         return generators.get(outName);
     }
 
     @Override
     public boolean accept(String outName, String facetName) {
 
-        Pair<Class<OutGenerator>, Object> pair = generators.get(outName);
-        if (pair == null) {
+        Item item = generators.get(outName);
+        if (item == null) {
             return true;
         }
 
-        Object ff = pair.getB();
-        return ff instanceof FacetFilter
-            ? ((FacetFilter)ff).accept(outName, facetName)
+        Object ctx = item.getContext();
+        return ctx instanceof FacetFilter
+            ? ((FacetFilter)ctx).accept(outName, facetName)
             : true;
     }
 }


More information about the Dive4Elements-commits mailing list