Mercurial > dive4elements > river
changeset 8219:9667900536b6
Added context injector to generators.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Mon, 08 Sep 2014 13:17:49 +0200 |
parents | ed8c1a0b4cfb |
children | 1d3d61c8f596 |
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/GeneratorLookup.java |
diffstat | 3 files changed, 67 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- 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) {
--- 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++; } }
--- 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; } }