Author: rdonkin
Date: Sat Mar 30 16:58:39 2013
New Revision: 1462800

URL: http://svn.apache.org/r1462800
Log:
Pushing classpath resource loading code into separate class

Added:
    
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TentaclesResources.java
   (with props)
Modified:
    creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Main.java
    
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TemplateBuilder.java
    
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Templates.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=1462800&r1=1462799&r2=1462800&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 
Sat Mar 30 16:58:39 2013
@@ -74,7 +74,7 @@ public class Main {
     public Main(final Configuration configuration, final FileSystem fileSystem,
             final IOSystem ioSystem) throws Exception {
         this(configuration, fileSystem, new NexusClient(fileSystem, ioSystem),
-                ioSystem, new Templates(ioSystem));
+                ioSystem, new Templates(ioSystem, new TentaclesResources()));
     }
 
     public Main(final Configuration configuration, final FileSystem fileSystem,

Modified: 
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TemplateBuilder.java
URL: 
http://svn.apache.org/viewvc/creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TemplateBuilder.java?rev=1462800&r1=1462799&r2=1462800&view=diff
==============================================================================
--- 
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TemplateBuilder.java
 (original)
+++ 
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TemplateBuilder.java
 Sat Mar 30 16:58:39 2013
@@ -20,10 +20,9 @@ package org.apache.creadur.tentacles;
 
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStreamReader;
+import java.io.Reader;
 import java.io.StringWriter;
 import java.io.Writer;
-import java.net.URL;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -33,6 +32,7 @@ import org.apache.velocity.app.VelocityE
 public class TemplateBuilder {
     private static final String LOG_TAG_NAME = TemplateBuilder.class.getName();
 
+    private final TentaclesResources tentaclesResources;
     private final VelocityEngine engine;
     private final IOSystem ioSystem;
     private final String templateName;
@@ -40,10 +40,12 @@ public class TemplateBuilder {
             new ConcurrentHashMap<String, Object>();
 
     public TemplateBuilder(final String template, final IOSystem ioSystem,
-            final VelocityEngine engine) {
+            final VelocityEngine engine,
+            final TentaclesResources tentaclesResources) {
         this.templateName = template;
         this.ioSystem = ioSystem;
         this.engine = engine;
+        this.tentaclesResources = tentaclesResources;
     }
 
     public TemplateBuilder add(final String key, final Object value) {
@@ -71,15 +73,8 @@ public class TemplateBuilder {
 
     private void evaluate(final Writer writer) {
         try {
-            final URL resource =
-                    Thread.currentThread().getContextClassLoader()
-                            .getResource(this.templateName);
-
-            if (resource == null) {
-                throw new IllegalStateException(this.templateName);
-            }
-            final InputStreamReader templateReader =
-                    new InputStreamReader(resource.openStream());
+            final Reader templateReader =
+                    this.tentaclesResources.read(this.templateName);
 
             final VelocityContext context =
                     new VelocityContext(this.templateContextMap);

Modified: 
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Templates.java
URL: 
http://svn.apache.org/viewvc/creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Templates.java?rev=1462800&r1=1462799&r2=1462800&view=diff
==============================================================================
--- 
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Templates.java
 (original)
+++ 
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Templates.java
 Sat Mar 30 16:58:39 2013
@@ -25,9 +25,12 @@ public final class Templates {
 
     private final IOSystem ioSystem;
     private final VelocityEngine engine;
+    private final TentaclesResources tentaclesResources;
 
-    public Templates(final IOSystem ioSystem) {
+    public Templates(final IOSystem ioSystem,
+            final TentaclesResources tentaclesResources) {
         this.ioSystem = ioSystem;
+        this.tentaclesResources = tentaclesResources;
         final Properties properties = new Properties();
         properties.setProperty("file.resource.loader.cache", "true");
         properties.setProperty("resource.loader", "file, class");
@@ -46,6 +49,7 @@ public final class Templates {
     }
 
     public TemplateBuilder template(final String name) {
-        return new TemplateBuilder(name, this.ioSystem, this.engine);
+        return new TemplateBuilder(name, this.ioSystem, this.engine,
+                this.tentaclesResources);
     }
 }

Added: 
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=1462800&view=auto
==============================================================================
--- 
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TentaclesResources.java
 (added)
+++ 
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TentaclesResources.java
 Sat Mar 30 16:58:39 2013
@@ -0,0 +1,41 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.creadur.tentacles;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.URL;
+
+public class TentaclesResources {
+
+    public Reader read(final String resourceName) throws IOException {
+        final URL resource =
+                Thread.currentThread().getContextClassLoader()
+                        .getResource(resourceName);
+
+        if (resource == null) {
+            throw new IllegalStateException(resourceName);
+        }
+        final InputStreamReader templateReader =
+                new InputStreamReader(resource.openStream());
+        return templateReader;
+    }
+
+}

Propchange: 
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TentaclesResources.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to