Use thread-safe way to render templates.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 12 Dec 2010 19:42:59 +0000 (20:42 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 12 Dec 2010 19:42:59 +0000 (20:42 +0100)
pom.xml
src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java

diff --git a/pom.xml b/pom.xml
index 4024519..68ed621 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -7,7 +7,7 @@
                <dependency>
                        <groupId>net.pterodactylus</groupId>
                        <artifactId>utils</artifactId>
-                       <version>0.7.2</version>
+                       <version>0.7.3</version>
                </dependency>
                <dependency>
                        <groupId>junit</groupId>
index ec0145a..92d5da6 100644 (file)
@@ -20,9 +20,11 @@ package net.pterodactylus.sone.web.ajax;
 import java.io.StringWriter;
 
 import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.json.JsonObject;
+import net.pterodactylus.util.template.DataProvider;
 import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateException;
 
@@ -60,8 +62,7 @@ public class GetPostAjaxPage extends JsonPage {
                if (post == null) {
                        return createErrorJsonObject("invalid-post-id");
                }
-               postTemplate.set("currentSone", getCurrentSone(request.getToadletContext()));
-               return createSuccessJsonObject().put("post", createJsonPost(post));
+               return createSuccessJsonObject().put("post", createJsonPost(post, getCurrentSone(request.getToadletContext())));
        }
 
        /**
@@ -82,24 +83,26 @@ public class GetPostAjaxPage extends JsonPage {
         *
         * @param post
         *            The post to create a JSON object from
+        * @param currentSone
+        *            The currently logged in Sone (to store in the template)
         * @return The JSON representation of the post
         */
-       private JsonObject createJsonPost(Post post) {
+       private JsonObject createJsonPost(Post post, Sone currentSone) {
                JsonObject jsonPost = new JsonObject();
                jsonPost.put("id", post.getId());
                jsonPost.put("sone", post.getSone().getId());
                jsonPost.put("recipient", (post.getRecipient() == null) ? null : post.getRecipient().getId());
                jsonPost.put("time", post.getTime());
                StringWriter stringWriter = new StringWriter();
-               synchronized (postTemplate) {
-                       postTemplate.set("post", post);
-                       try {
-                               postTemplate.render(stringWriter);
-                       } catch (TemplateException te1) {
-                               /* TODO - shouldn’t happen. */
-                       } finally {
-                               Closer.close(stringWriter);
-                       }
+               DataProvider dataProvider = postTemplate.createDataProvider();
+               dataProvider.setData("post", post);
+               dataProvider.setData("currentSone", currentSone);
+               try {
+                       postTemplate.render(dataProvider, stringWriter);
+               } catch (TemplateException te1) {
+                       /* TODO - shouldn’t happen. */
+               } finally {
+                       Closer.close(stringWriter);
                }
                jsonPost.put("html", stringWriter.toString());
                return jsonPost;
index 0963b09..e589191 100644 (file)
@@ -20,9 +20,11 @@ package net.pterodactylus.sone.web.ajax;
 import java.io.StringWriter;
 
 import net.pterodactylus.sone.data.Reply;
+import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.json.JsonObject;
+import net.pterodactylus.util.template.DataProvider;
 import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateException;
 
@@ -63,8 +65,7 @@ public class GetReplyAjaxPage extends JsonPage {
                if ((reply == null) || (reply.getSone() == null)) {
                        return createErrorJsonObject("invalid-reply-id");
                }
-               replyTemplate.set("currentSone", getCurrentSone(request.getToadletContext()));
-               return createSuccessJsonObject().put("reply", createJsonReply(reply));
+               return createSuccessJsonObject().put("reply", createJsonReply(reply, getCurrentSone(request.getToadletContext())));
        }
 
        /**
@@ -84,24 +85,26 @@ public class GetReplyAjaxPage extends JsonPage {
         *
         * @param reply
         *            The reply to convert
+        * @param currentSone
+        *            The currently logged in Sone (to store in the template)
         * @return The JSON representation of the reply
         */
-       private JsonObject createJsonReply(Reply reply) {
+       private JsonObject createJsonReply(Reply reply, Sone currentSone) {
                JsonObject jsonReply = new JsonObject();
                jsonReply.put("id", reply.getId());
                jsonReply.put("postId", reply.getPost().getId());
                jsonReply.put("soneId", reply.getSone().getId());
                jsonReply.put("time", reply.getTime());
                StringWriter stringWriter = new StringWriter();
-               synchronized (replyTemplate) {
-                       replyTemplate.set("reply", reply);
-                       try {
-                               replyTemplate.render(stringWriter);
-                       } catch (TemplateException te1) {
-                               /* TODO - shouldn’t happen. */
-                       } finally {
-                               Closer.close(stringWriter);
-                       }
+               DataProvider dataProvider = replyTemplate.createDataProvider();
+               dataProvider.setData("reply", reply);
+               dataProvider.setData("currentSone", currentSone);
+               try {
+                       replyTemplate.render(dataProvider, stringWriter);
+               } catch (TemplateException te1) {
+                       /* TODO - shouldn’t happen. */
+               } finally {
+                       Closer.close(stringWriter);
                }
                return jsonReply.put("html", stringWriter.toString());
        }