From 3258eb0cc375f2de06988f95d68f452ec81b3994 Mon Sep 17 00:00:00 2001 From: Christian Wulf <chw@informatik.uni-kiel.de> Date: Tue, 19 Aug 2014 05:56:01 +0200 Subject: [PATCH] added PipeFactoryLoader --- conf/logback.groovy | 7 +-- conf/pipe-factories.conf | 4 ++ .../framework/core/pipe/PipeFactory.java | 19 +++++++ .../core/pipe/PipeFactoryLoader.java | 49 +++++++++++++++++++ .../RecordReaderConfiguration.java | 9 ---- 5 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 conf/pipe-factories.conf create mode 100644 src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/PipeFactoryLoader.java diff --git a/conf/logback.groovy b/conf/logback.groovy index 61c19e49..c9173f02 100644 --- a/conf/logback.groovy +++ b/conf/logback.groovy @@ -19,10 +19,11 @@ appender("CONSOLE", ConsoleAppender) { } } -//root WARN, ["CONSOLE"] -root WARN +root WARN, ["CONSOLE"] //logger "teetime.variant.methodcallWithPorts.stage", DEBUG, ["CONSOLE"] logger "teetime.variant.methodcallWithPorts.stage", INFO -logger "teetime.variant.methodcallWithPorts.examples.kiekerdays.TimingsReader", TRACE, ["CONSOLE", "FILE"] \ No newline at end of file +logger "teetime.variant.methodcallWithPorts.framework.core.pipe", INFO + +logger "teetime.variant.methodcallWithPorts.examples.kiekerdays.TimingsReader", TRACE, ["FILE"] \ No newline at end of file diff --git a/conf/pipe-factories.conf b/conf/pipe-factories.conf new file mode 100644 index 00000000..97c2e631 --- /dev/null +++ b/conf/pipe-factories.conf @@ -0,0 +1,4 @@ +teetime.variant.methodcallWithPorts.framework.core.pipe.SingleElementPipeFactory +teetime.variant.methodcallWithPorts.framework.core.pipe.OrderedGrowableArrayPipeFactory +teetime.variant.methodcallWithPorts.framework.core.pipe.UnorderedGrowablePipeFactory +teetime.variant.methodcallWithPorts.framework.core.pipe.SpScPipeFactory diff --git a/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/PipeFactory.java b/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/PipeFactory.java index 1bc509be..fae524cf 100644 --- a/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/PipeFactory.java +++ b/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/PipeFactory.java @@ -1,10 +1,17 @@ package teetime.variant.methodcallWithPorts.framework.core.pipe; +import java.io.IOException; import java.util.HashMap; +import java.util.List; import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class PipeFactory { + private static final Logger LOGGER = LoggerFactory.getLogger(PipeFactory.class); + public enum ThreadCommunication { INTER, INTRA } @@ -23,6 +30,17 @@ public class PipeFactory { private final Map<String, IPipeFactory> pipeFactories = new HashMap<String, IPipeFactory>(); + public PipeFactory() { + try { + List<IPipeFactory> pipeFactories = PipeFactoryLoader.loadFromFile("conf/pipe-factories.conf"); + for (IPipeFactory pipeFactory : pipeFactories) { + this.register(pipeFactory); + } + } catch (IOException e) { + LOGGER.warn("Could not load pipe factories from file", e); + } + } + /** * Creates a new FIFO-ordered, growable pipe with an initial capacity of 1. <br> * <i>This method is suitable for most situations.</i> @@ -47,6 +65,7 @@ public class PipeFactory { public void register(final IPipeFactory pipeFactory) { String key = this.buildKey(pipeFactory.getThreadCommunication(), pipeFactory.getOrdering(), pipeFactory.isGrowable()); this.pipeFactories.put(key, pipeFactory); + LOGGER.info("Registered pipe factory: " + pipeFactory.getClass().getCanonicalName()); } } diff --git a/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/PipeFactoryLoader.java b/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/PipeFactoryLoader.java new file mode 100644 index 00000000..df1b844f --- /dev/null +++ b/src/main/java/teetime/variant/methodcallWithPorts/framework/core/pipe/PipeFactoryLoader.java @@ -0,0 +1,49 @@ +package teetime.variant.methodcallWithPorts.framework.core.pipe; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.LinkedList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PipeFactoryLoader { + + private static final Logger LOGGER = LoggerFactory.getLogger(PipeFactoryLoader.class); + + private PipeFactoryLoader() { + // utility class + } + + public static List<IPipeFactory> loadFromFile(final String fileName) throws IOException { + List<IPipeFactory> pipeFactories = new LinkedList<IPipeFactory>(); + + BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName)); + try { + String line; + while (null != (line = bufferedReader.readLine())) { + try { + line = line.trim(); + if (!line.isEmpty()) { + Class<?> clazz = Class.forName(line); + Class<? extends IPipeFactory> pipeFactoryClass = clazz.asSubclass(IPipeFactory.class); + IPipeFactory pipeFactory = pipeFactoryClass.newInstance(); + pipeFactories.add(pipeFactory); + } + } catch (ClassNotFoundException e) { + LOGGER.warn("Could not find class: " + line, e); + } catch (InstantiationException e) { + LOGGER.warn("Could not instantiate pipe factory", e); + } catch (IllegalAccessException e) { + LOGGER.warn("Could not instantiate pipe factory", e); + } + } + } finally { + bufferedReader.close(); + } + + return pipeFactories; + } +} diff --git a/src/test/java/teetime/variant/methodcallWithPorts/examples/recordReader/RecordReaderConfiguration.java b/src/test/java/teetime/variant/methodcallWithPorts/examples/recordReader/RecordReaderConfiguration.java index 149f97cb..a3ccee70 100644 --- a/src/test/java/teetime/variant/methodcallWithPorts/examples/recordReader/RecordReaderConfiguration.java +++ b/src/test/java/teetime/variant/methodcallWithPorts/examples/recordReader/RecordReaderConfiguration.java @@ -23,14 +23,10 @@ import teetime.variant.methodcallWithPorts.framework.core.Configuration; import teetime.variant.methodcallWithPorts.framework.core.Pipeline; import teetime.variant.methodcallWithPorts.framework.core.StageWithPort; import teetime.variant.methodcallWithPorts.framework.core.pipe.IPipe; -import teetime.variant.methodcallWithPorts.framework.core.pipe.OrderedGrowableArrayPipeFactory; import teetime.variant.methodcallWithPorts.framework.core.pipe.PipeFactory; import teetime.variant.methodcallWithPorts.framework.core.pipe.PipeFactory.PipeOrdering; import teetime.variant.methodcallWithPorts.framework.core.pipe.PipeFactory.ThreadCommunication; -import teetime.variant.methodcallWithPorts.framework.core.pipe.SingleElementPipeFactory; import teetime.variant.methodcallWithPorts.framework.core.pipe.SpScPipe; -import teetime.variant.methodcallWithPorts.framework.core.pipe.SpScPipeFactory; -import teetime.variant.methodcallWithPorts.framework.core.pipe.UnorderedGrowablePipeFactory; import teetime.variant.methodcallWithPorts.stage.CollectorSink; import teetime.variant.methodcallWithPorts.stage.kieker.Dir2RecordsFilter; import teetime.variant.methodcallWithPorts.stage.kieker.className.ClassNameRegistryRepository; @@ -48,12 +44,7 @@ public class RecordReaderConfiguration extends Configuration { private final PipeFactory pipeFactory; public RecordReaderConfiguration() { - // BETTER instantiate one single pipe factory for all analyzes and register all available pipe implementations once this.pipeFactory = new PipeFactory(); - this.pipeFactory.register(new SingleElementPipeFactory()); - this.pipeFactory.register(new OrderedGrowableArrayPipeFactory()); - this.pipeFactory.register(new UnorderedGrowablePipeFactory()); - this.pipeFactory.register(new SpScPipeFactory()); } public void buildConfiguration() { -- GitLab