From f365bd849c997c6a629ab12b443fb655ce4fa380 Mon Sep 17 00:00:00 2001 From: Nelson Tavares de Sousa <stu103017@mail.uni-kiel.de> Date: Wed, 22 Oct 2014 15:23:20 +0200 Subject: [PATCH] first version which searches in the classpath the given file and merges all files into one --- .../java/teetime/framework/FileSearcher.java | 28 +++++++----- .../framework/pipe/PipeFactoryLoader.java | 43 +++++++++++++++++++ 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/main/java/teetime/framework/FileSearcher.java b/src/main/java/teetime/framework/FileSearcher.java index faebb333..cdc25101 100644 --- a/src/main/java/teetime/framework/FileSearcher.java +++ b/src/main/java/teetime/framework/FileSearcher.java @@ -1,20 +1,28 @@ package teetime.framework; import java.io.IOException; -import java.util.StringTokenizer; +import java.net.URL; +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 { - final String classpath = System.getProperty("java.class.path"); - final String pathSeparator = System.getProperty("path.separator"); - // System.out.println(classpath); - // System.out.println(pathSeparator); - - StringTokenizer st = new StringTokenizer(classpath, pathSeparator); - while (st.hasMoreTokens()) { - System.out.println(st.nextToken());// st.nextToken()); - } + + PipeFactoryLoader.mergeConfigFiles("LICENSE.txt", "test.txt"); } + public static List<URL> loadResources(final String name) throws IOException { + + final List<URL> list = new ArrayList<URL>(); + + final Enumeration<URL> systemRes = ClassLoader.getSystemClassLoader().getResources(name); + while (systemRes.hasMoreElements()) { + list.add(systemRes.nextElement()); + } + return list; + } } diff --git a/src/main/java/teetime/framework/pipe/PipeFactoryLoader.java b/src/main/java/teetime/framework/pipe/PipeFactoryLoader.java index 365918f0..a685ee18 100644 --- a/src/main/java/teetime/framework/pipe/PipeFactoryLoader.java +++ b/src/main/java/teetime/framework/pipe/PipeFactoryLoader.java @@ -1,14 +1,24 @@ 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.net.URL; import java.util.LinkedList; import java.util.List; import org.slf4j.Logger; 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); @@ -46,4 +56,37 @@ 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); + } + + List<URL> files = null; + + try { + files = FileSearcher.loadResources(configFileName); + } catch (IOException e) { + throw new IllegalStateException(e); + } + for (URL url : files) { + try { + InputStream is = url.openStream(); + ByteStreams.copy(is, os); + is.close(); + } catch (IOException e) { + throw new IllegalStateException(e); + } + + } + } } -- GitLab