Push resource wrangling into responsible class

git-svn-id: https://svn.apache.org/repos/asf/creadur/tentacles/trunk@1462900 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/creadur/tentacles/Main.java b/src/main/java/org/apache/creadur/tentacles/Main.java
index 6405678..ada6149 100644
--- a/src/main/java/org/apache/creadur/tentacles/Main.java
+++ b/src/main/java/org/apache/creadur/tentacles/Main.java
@@ -23,7 +23,6 @@
 import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -67,6 +66,7 @@
     private final Configuration configuration;
     private final FileSystem fileSystem;
     private final IOSystem ioSystem;
+    private final TentaclesResources tentaclesResources;
     private final Templates templates;
 
     public Main(final String... args) throws Exception {
@@ -86,6 +86,7 @@
         this.configuration = configuration;
         this.fileSystem = platform.getFileSystem();
         this.ioSystem = platform.getIoSystem();
+        this.tentaclesResources = platform.getTentaclesResources();
         this.templates = templates;
 
         this.local =
@@ -105,21 +106,17 @@
 
         this.reports = new Reports();
 
-        final URL style =
-                this.getClass().getClassLoader().getResource("legal/style.css");
-        this.ioSystem.copy(style.openStream(),
-                new File(this.local, "style.css"));
+        this.tentaclesResources.copyTo("legal/style.css", new File(this.local,
+                "style.css"));
 
         licenses("asl-2.0");
         licenses("cpl-1.0");
         licenses("cddl-1.0");
     }
 
-    private void licenses(final String s) throws IOException {
-        final URL aslURL =
-                this.getClass().getClassLoader()
-                        .getResource("licenses/" + s + ".txt");
-        this.licenses.put(s, this.ioSystem.slurp(aslURL).trim());
+    private void licenses(final String name) throws IOException {
+        final String path = "licenses/" + name + ".txt";
+        this.licenses.put(name, this.tentaclesResources.readText(path).trim());
     }
 
     public static void main(final String[] args) throws Exception {
diff --git a/src/main/java/org/apache/creadur/tentacles/Platform.java b/src/main/java/org/apache/creadur/tentacles/Platform.java
index 0c2df9f..f87fffc 100644
--- a/src/main/java/org/apache/creadur/tentacles/Platform.java
+++ b/src/main/java/org/apache/creadur/tentacles/Platform.java
@@ -23,7 +23,8 @@
     public static Platform aPlatform() {
         final FileSystem fileSystem = new FileSystem();
         final IOSystem ioSystem = new IOSystem();
-        final TentaclesResources tentaclesResources = new TentaclesResources();
+        final TentaclesResources tentaclesResources =
+                new TentaclesResources(ioSystem);
         return new Platform(tentaclesResources, fileSystem, ioSystem);
     }
 
diff --git a/src/main/java/org/apache/creadur/tentacles/TentaclesResources.java b/src/main/java/org/apache/creadur/tentacles/TentaclesResources.java
index 4cbc4c0..098a1d3 100644
--- a/src/main/java/org/apache/creadur/tentacles/TentaclesResources.java
+++ b/src/main/java/org/apache/creadur/tentacles/TentaclesResources.java
@@ -18,6 +18,7 @@
  */
 package org.apache.creadur.tentacles;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
@@ -25,17 +26,38 @@
 
 public class TentaclesResources {
 
-    public Reader read(final String resourceName) throws IOException {
-        final URL resource =
-                Thread.currentThread().getContextClassLoader()
-                        .getResource(resourceName);
+    private final IOSystem ioSystem;
 
-        if (resource == null) {
-            throw new IllegalStateException(resourceName);
-        }
+    public TentaclesResources(final IOSystem ioSystem) {
+        super();
+        this.ioSystem = ioSystem;
+    }
+
+    public Reader read(final String resourceName) throws IOException {
+        final URL resourceUrl = toUrl(resourceName);
         final InputStreamReader templateReader =
-                new InputStreamReader(resource.openStream());
+                new InputStreamReader(resourceUrl.openStream());
         return templateReader;
     }
 
+    public String readText(final String resourcePath) throws IOException {
+        final String text = this.ioSystem.slurp(toUrl(resourcePath));
+        return text;
+    }
+
+    public void copyTo(final String resourcePath, final File to)
+            throws IOException {
+        this.ioSystem.copy(toUrl(resourcePath).openStream(), to);
+    }
+
+    private URL toUrl(final String resourcePath) {
+        final URL resourceUrl =
+                this.getClass().getClassLoader().getResource(resourcePath);
+        if (resourceUrl == null) {
+            throw new IllegalStateException(
+                    "Tentacles expects the classpath to contain "
+                            + resourcePath);
+        }
+        return resourceUrl;
+    }
 }