summaryrefslogtreecommitdiffstats
path: root/sandbox/rfeng
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-10-10 06:47:55 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-10-10 06:47:55 +0000
commit89b23362d1920d3e2cedbff399d847131c4f3601 (patch)
treef4f2d1c5c0cc1fa8947aef9af08097c7dc1b317a /sandbox/rfeng
parentb7878fb9752f9fdd41449e44743a87534ece73ce (diff)
Improve GUI and clean up code
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@823807 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/rfeng')
-rw-r--r--sandbox/rfeng/helloworld-jsp-google-appengine/src/sample/HelloworldServiceImpl.java76
1 files changed, 51 insertions, 25 deletions
diff --git a/sandbox/rfeng/helloworld-jsp-google-appengine/src/sample/HelloworldServiceImpl.java b/sandbox/rfeng/helloworld-jsp-google-appengine/src/sample/HelloworldServiceImpl.java
index 4257fea3b1..6c3addf7b7 100644
--- a/sandbox/rfeng/helloworld-jsp-google-appengine/src/sample/HelloworldServiceImpl.java
+++ b/sandbox/rfeng/helloworld-jsp-google-appengine/src/sample/HelloworldServiceImpl.java
@@ -35,6 +35,7 @@ import com.google.appengine.api.datastore.EntityNotFoundException;
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 @@ public class HelloworldServiceImpl implements HelloworldService {
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 @@ public class HelloworldServiceImpl implements HelloworldService {
}
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