You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2009/10/10 08:47:55 UTC

svn commit: r823807 - /tuscany/sandbox/rfeng/helloworld-jsp-google-appengine/src/sample/HelloworldServiceImpl.java

Author: rfeng
Date: Sat Oct 10 06:47:55 2009
New Revision: 823807

URL: http://svn.apache.org/viewvc?rev=823807&view=rev
Log:
Improve GUI and clean up code

Modified:
    tuscany/sandbox/rfeng/helloworld-jsp-google-appengine/src/sample/HelloworldServiceImpl.java

Modified: tuscany/sandbox/rfeng/helloworld-jsp-google-appengine/src/sample/HelloworldServiceImpl.java
URL: http://svn.apache.org/viewvc/tuscany/sandbox/rfeng/helloworld-jsp-google-appengine/src/sample/HelloworldServiceImpl.java?rev=823807&r1=823806&r2=823807&view=diff
==============================================================================
--- tuscany/sandbox/rfeng/helloworld-jsp-google-appengine/src/sample/HelloworldServiceImpl.java (original)
+++ tuscany/sandbox/rfeng/helloworld-jsp-google-appengine/src/sample/HelloworldServiceImpl.java Sat Oct 10 06:47:55 2009
@@ -35,6 +35,7 @@
 import com.google.appengine.api.datastore.Key;
 import com.google.appengine.api.datastore.KeyFactory;
 import com.google.appengine.api.datastore.Transaction;
+import com.google.appengine.api.memcache.Expiration;
 import com.google.appengine.api.memcache.MemcacheService;
 import com.google.appengine.api.urlfetch.HTTPResponse;
 import com.google.appengine.api.urlfetch.URLFetchService;
@@ -61,13 +62,38 @@
         User user = userService.getCurrentUser();
         String id = (user == null) ? "" : user.getUserId();
 
-        Stack<String> stack = (Stack<String>)memcacheService.get("history");
-        if (stack == null) {
-            stack = new Stack<String>();
+        Stack<String> stack = cacheHistory(name);
+
+        Long count = countInStore();
+
+        String content = fetch();
+        return "[" + id
+            + "] Hello "
+            + name
+            + "<hr><h2>Content from Tuscany Web Site via URLFetch</h2><p>"
+            + content
+            + "<p>"
+            + toHTML(stack)
+            + "</p><b>Counter from DataStore: "
+            + count
+            + "</b><p>";
+    }
+
+    private String fetch() {
+        String content = "";
+        try {
+            HTTPResponse response = fetchService.fetch(new URL("http://tuscany.apache.org"));
+            content = new String(response.getContent(), 0, 1024);
+            content = content.replace("<", "&lt;");
+            content = content.replace(">", "&gt;");
+            content = content.replace("\"", "&quot;");
+        } catch (IOException e) {
+            e.printStackTrace();
         }
-        stack.push(new Date().toString() + ": " + name);
-        memcacheService.put("history", stack);
+        return content;
+    }
 
+    private Long countInStore() {
         Transaction tx = datastoreService.beginTransaction();
         Entity entity = null;
         Key key = KeyFactory.createKey("HitCounter", "countter");
@@ -79,31 +105,31 @@
         }
 
         Long count = (Long)entity.getProperty("counter");
-        entity.setProperty("counter", new Long(count.longValue() + 1));
+        count = new Long(count.longValue() + 1);
+        entity.setProperty("counter", count);
 
         datastoreService.put(tx, entity);
         tx.commit();
+        return count;
+    }
 
-        String content = "";
-        try {
-            HTTPResponse response = fetchService.fetch(new URL("http://tuscany.apache.org"));
-            content = new String(response.getContent(), 0, 1024);
-            content = content.replace("<", "&lt;");
-            content = content.replace(">", "&gt;");
-            content = content.replace("\"", "&quot;");
-        } catch (IOException e) {
-            e.printStackTrace();
+    private Stack<String> cacheHistory(String name) {
+        Stack<String> stack = (Stack<String>)memcacheService.get("history");
+        if (stack == null) {
+            stack = new Stack<String>();
         }
-        return "[" + id
-            + "] Hello "
-            + name
-            + "<hr><h1>Content from Tuscany Web Site</h1><p>"
-            + content
-            + "<p>History<hr>"
-            + stack
-            + "</p><b>"
-            + count
-            + "</b>";
+        stack.push(new Date().toString() + ": " + name);
+        memcacheService.put("history", stack, Expiration.byDeltaSeconds(60));
+        return stack;
+    }
+
+    private String toHTML(Stack<String> stack) {
+        StringBuffer html = new StringBuffer("<h2>History from Memcache (expired in 1 minute)</h2><hr><ul>");
+        for (String item : stack) {
+            html.append("<li>").append(item).append("</li>");
+        }
+        html.append("</ul><hr>");
+        return html.toString();
     }
 
     @Init