creadur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject svn commit: r1462900 - in /creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles: Main.java Platform.java TentaclesResources.java
Date Sun, 31 Mar 2013 08:31:55 GMT
Author: rdonkin
Date: Sun Mar 31 08:31:55 2013
New Revision: 1462900

URL: http://svn.apache.org/r1462900
Log:
Push resource wrangling into responsible class

Modified:
    creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Main.java
    creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Platform.java
    creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TentaclesResources.java

Modified: creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Main.java
URL: http://svn.apache.org/viewvc/creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Main.java?rev=1462900&r1=1462899&r2=1462900&view=diff
==============================================================================
--- creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Main.java (original)
+++ creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Main.java Sun Mar 31
08:31:55 2013
@@ -23,7 +23,6 @@ import java.io.File;
 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 @@ public class Main {
     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 @@ public class Main {
         this.configuration = configuration;
         this.fileSystem = platform.getFileSystem();
         this.ioSystem = platform.getIoSystem();
+        this.tentaclesResources = platform.getTentaclesResources();
         this.templates = templates;
 
         this.local =
@@ -105,21 +106,17 @@ public class Main {
 
         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 {

Modified: creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Platform.java
URL: http://svn.apache.org/viewvc/creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Platform.java?rev=1462900&r1=1462899&r2=1462900&view=diff
==============================================================================
--- creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Platform.java (original)
+++ creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Platform.java Sun Mar
31 08:31:55 2013
@@ -23,7 +23,8 @@ public class Platform {
     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);
     }
 

Modified: creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TentaclesResources.java
URL: http://svn.apache.org/viewvc/creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TentaclesResources.java?rev=1462900&r1=1462899&r2=1462900&view=diff
==============================================================================
--- creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TentaclesResources.java
(original)
+++ creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TentaclesResources.java
Sun Mar 31 08:31:55 2013
@@ -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 @@ import java.net.URL;
 
 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;
+    }
 }



Mime
View raw message