creadur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject svn commit: r1462795 - /creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TemplateBuilder.java
Date Sat, 30 Mar 2013 16:50:54 GMT
Author: rdonkin
Date: Sat Mar 30 16:50:54 2013
New Revision: 1462795

URL: http://svn.apache.org/r1462795
Log:
Tidy up: improving thread safety, names and field usage

Modified:
    creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TemplateBuilder.java

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=1462795&r1=1462794&r2=1462795&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:50:54 2013
@@ -24,44 +24,42 @@ import java.io.InputStreamReader;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.net.URL;
-import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
 
 public class TemplateBuilder {
+    private static final String LOG_TAG_NAME = TemplateBuilder.class.getName();
+
     private final VelocityEngine engine;
     private final IOSystem ioSystem;
-    private final String template;
-    private final Map<String, Object> map = new HashMap<String, Object>();
+    private final String templateName;
+    private final Map<String, Object> templateContextMap =
+            new ConcurrentHashMap<String, Object>();
 
     public TemplateBuilder(final String template, final IOSystem ioSystem,
             final VelocityEngine engine) {
-        this.template = template;
+        this.templateName = template;
         this.ioSystem = ioSystem;
         this.engine = engine;
     }
 
     public TemplateBuilder add(final String key, final Object value) {
-        this.map.put(key, value);
+        this.templateContextMap.put(key, value);
         return this;
     }
 
     public TemplateBuilder addAll(final Map<String, Object> map) {
-        this.map.putAll(map);
+        this.templateContextMap.putAll(map);
         return this;
     }
 
     public String apply() {
         final StringWriter writer = new StringWriter();
 
-        try {
-            evaluate(this.template, this.map, writer);
-        } catch (final IOException ioe) {
-            throw new RuntimeException("can't apply template "
-                    + this.template, ioe);
-        }
+        evaluate(writer);
 
         return writer.toString();
     }
@@ -71,21 +69,26 @@ public class TemplateBuilder {
         return file;
     }
 
-    private void evaluate(final String template,
-            final Map<String, Object> mapContext, final Writer writer)
-            throws IOException {
-
-        final URL resource =
-                Thread.currentThread().getContextClassLoader()
-                        .getResource(template);
+    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 VelocityContext context =
+                    new VelocityContext(this.templateContextMap);
+            this.engine.evaluate(context, writer, LOG_TAG_NAME, templateReader);
 
-        if (resource == null) {
-            throw new IllegalStateException(template);
+        } catch (final IOException ioe) {
+            throw new RuntimeException("can't apply template "
+                    + this.templateName, ioe);
         }
-
-        final VelocityContext context = new VelocityContext(mapContext);
-        this.engine.evaluate(context, writer, Templates.class.getName(),
-                new InputStreamReader(resource.openStream()));
     }
 
 }
\ No newline at end of file



Mime
View raw message