Skip to content
Snippets Groups Projects
Commit 8e17fafe authored by Nelson Tavares de Sousa's avatar Nelson Tavares de Sousa
Browse files

#25 first working version; PipeFactoryLoader now searches for all

config-files in the classpath, reads them and adds the corresponding
factory to the registry
parent 5dd655ef
No related branches found
No related tags found
No related merge requests found
...@@ -6,15 +6,8 @@ import java.util.ArrayList; ...@@ -6,15 +6,8 @@ import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
import teetime.framework.pipe.PipeFactoryLoader;
public class FileSearcher { 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 { public static List<URL> loadResources(final String name) throws IOException {
final List<URL> list = new ArrayList<URL>(); final List<URL> list = new ArrayList<URL>();
......
package teetime.framework.pipe; package teetime.framework.pipe;
import java.io.BufferedReader; 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.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
...@@ -16,9 +13,6 @@ import org.slf4j.LoggerFactory; ...@@ -16,9 +13,6 @@ import org.slf4j.LoggerFactory;
import teetime.framework.FileSearcher; import teetime.framework.FileSearcher;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
public class PipeFactoryLoader { public class PipeFactoryLoader {
private static final Logger LOGGER = LoggerFactory.getLogger(PipeFactoryLoader.class); private static final Logger LOGGER = LoggerFactory.getLogger(PipeFactoryLoader.class);
...@@ -27,10 +21,10 @@ public class PipeFactoryLoader { ...@@ -27,10 +21,10 @@ public class PipeFactoryLoader {
// utility class // 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>(); List<IPipeFactory> pipeFactories = new LinkedList<IPipeFactory>();
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName)); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
try { try {
String line; String line;
while (null != (line = bufferedReader.readLine())) { while (null != (line = bufferedReader.readLine())) {
...@@ -57,20 +51,9 @@ public class PipeFactoryLoader { ...@@ -57,20 +51,9 @@ public class PipeFactoryLoader {
return pipeFactories; return pipeFactories;
} }
public static void mergeConfigFiles(final String configFileName, final String mergedFileName) { public static List<IPipeFactory> mergeConfigFiles(final String configFileName) {
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);
}
List<IPipeFactory> pipeFactories = new LinkedList<IPipeFactory>();
List<URL> files = null; List<URL> files = null;
try { try {
...@@ -81,12 +64,13 @@ public class PipeFactoryLoader { ...@@ -81,12 +64,13 @@ public class PipeFactoryLoader {
for (URL url : files) { for (URL url : files) {
try { try {
InputStream is = url.openStream(); InputStream is = url.openStream();
ByteStreams.copy(is, os); pipeFactories.addAll(loadFromStream(is));
is.close(); is.close();
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
} }
return pipeFactories;
} }
} }
package teetime.framework.pipe; package teetime.framework.pipe;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -49,13 +48,9 @@ public class PipeFactoryRegistry { ...@@ -49,13 +48,9 @@ public class PipeFactoryRegistry {
public static PipeFactoryRegistry INSTANCE = new PipeFactoryRegistry(); public static PipeFactoryRegistry INSTANCE = new PipeFactoryRegistry();
private PipeFactoryRegistry() { private PipeFactoryRegistry() {
try { List<IPipeFactory> pipeFactories = PipeFactoryLoader.mergeConfigFiles("pipe-factories.conf");
List<IPipeFactory> pipeFactories = PipeFactoryLoader.loadFromFile("conf/pipe-factories.conf"); for (IPipeFactory pipeFactory : pipeFactories) {
for (IPipeFactory pipeFactory : pipeFactories) { this.register(pipeFactory);
this.register(pipeFactory);
}
} catch (IOException e) {
LOGGER.warn("Could not load pipe factories from file", e);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment