From df5e162d8bac8d9214ee43dec09288230163b892 Mon Sep 17 00:00:00 2001
From: Nelson Tavares de Sousa <stu103017@mail.uni-kiel.de>
Date: Wed, 22 Oct 2014 15:32:23 +0200
Subject: [PATCH] #25 first working version; PipeFactoryLoader now searches for
 all config-files in the classpath, reads them and adds the corresponding
 factory to the registry

---
 .../java/teetime/framework/FileSearcher.java  |  7 -----
 .../framework/pipe/PipeFactoryLoader.java     | 30 +++++--------------
 .../framework/pipe/PipeFactoryRegistry.java   | 11 ++-----
 3 files changed, 10 insertions(+), 38 deletions(-)

diff --git a/src/main/java/teetime/framework/FileSearcher.java b/src/main/java/teetime/framework/FileSearcher.java
index cdc25101..adce07e3 100644
--- a/src/main/java/teetime/framework/FileSearcher.java
+++ b/src/main/java/teetime/framework/FileSearcher.java
@@ -6,15 +6,8 @@ import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.List;
 
-import teetime.framework.pipe.PipeFactoryLoader;
-
 public class FileSearcher {
 
-	public static void main(final String[] args) throws IOException {
-
-		PipeFactoryLoader.mergeConfigFiles("LICENSE.txt", "test.txt");
-	}
-
 	public static List<URL> loadResources(final String name) throws IOException {
 
 		final List<URL> list = new ArrayList<URL>();
diff --git a/src/main/java/teetime/framework/pipe/PipeFactoryLoader.java b/src/main/java/teetime/framework/pipe/PipeFactoryLoader.java
index a685ee18..54cf5e29 100644
--- a/src/main/java/teetime/framework/pipe/PipeFactoryLoader.java
+++ b/src/main/java/teetime/framework/pipe/PipeFactoryLoader.java
@@ -1,12 +1,9 @@
 package teetime.framework.pipe;
 
 import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.net.URL;
 import java.util.LinkedList;
 import java.util.List;
@@ -16,9 +13,6 @@ import org.slf4j.LoggerFactory;
 
 import teetime.framework.FileSearcher;
 
-import com.google.common.io.ByteStreams;
-import com.google.common.io.Files;
-
 public class PipeFactoryLoader {
 
 	private static final Logger LOGGER = LoggerFactory.getLogger(PipeFactoryLoader.class);
@@ -27,10 +21,10 @@ public class PipeFactoryLoader {
 		// utility class
 	}
 
-	public static List<IPipeFactory> loadFromFile(final String fileName) throws IOException {
+	public static List<IPipeFactory> loadFromStream(final InputStream stream) throws IOException {
 		List<IPipeFactory> pipeFactories = new LinkedList<IPipeFactory>();
 
-		BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
+		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
 		try {
 			String line;
 			while (null != (line = bufferedReader.readLine())) {
@@ -57,20 +51,9 @@ public class PipeFactoryLoader {
 		return pipeFactories;
 	}
 
-	public static void mergeConfigFiles(final String configFileName, final String mergedFileName) {
-		File output = new File(mergedFileName);
-		FileOutputStream os;
-		try {
-			os = new FileOutputStream(output);
-		} catch (FileNotFoundException e1) {
-			throw new IllegalStateException(e1);
-		}
-		try {
-			Files.touch(output);
-		} catch (IOException e) {
-			throw new IllegalStateException(e);
-		}
+	public static List<IPipeFactory> mergeConfigFiles(final String configFileName) {
 
+		List<IPipeFactory> pipeFactories = new LinkedList<IPipeFactory>();
 		List<URL> files = null;
 
 		try {
@@ -81,12 +64,13 @@ public class PipeFactoryLoader {
 		for (URL url : files) {
 			try {
 				InputStream is = url.openStream();
-				ByteStreams.copy(is, os);
+				pipeFactories.addAll(loadFromStream(is));
 				is.close();
 			} catch (IOException e) {
 				throw new IllegalStateException(e);
 			}
 
 		}
+		return pipeFactories;
 	}
 }
diff --git a/src/main/java/teetime/framework/pipe/PipeFactoryRegistry.java b/src/main/java/teetime/framework/pipe/PipeFactoryRegistry.java
index 44c20920..b829645b 100644
--- a/src/main/java/teetime/framework/pipe/PipeFactoryRegistry.java
+++ b/src/main/java/teetime/framework/pipe/PipeFactoryRegistry.java
@@ -1,6 +1,5 @@
 package teetime.framework.pipe;
 
-import java.io.IOException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -49,13 +48,9 @@ public class PipeFactoryRegistry {
 	public static PipeFactoryRegistry INSTANCE = new PipeFactoryRegistry();
 
 	private PipeFactoryRegistry() {
-		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);
+		List<IPipeFactory> pipeFactories = PipeFactoryLoader.mergeConfigFiles("pipe-factories.conf");
+		for (IPipeFactory pipeFactory : pipeFactories) {
+			this.register(pipeFactory);
 		}
 	}
 
-- 
GitLab