From 89b23362d1920d3e2cedbff399d847131c4f3601 Mon Sep 17 00:00:00 2001 From: rfeng Date: Sat, 10 Oct 2009 06:47:55 +0000 Subject: Improve GUI and clean up code git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@823807 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/sample/HelloworldServiceImpl.java | 76 +++++++++++++++------- 1 file changed, 51 insertions(+), 25 deletions(-) (limited to 'sandbox/rfeng') 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 stack = (Stack)memcacheService.get("history"); - if (stack == null) { - stack = new Stack(); + Stack stack = cacheHistory(name); + + Long count = countInStore(); + + String content = fetch(); + return "[" + id + + "] Hello " + + name + + "

Content from Tuscany Web Site via URLFetch

" + + content + + "

" + + toHTML(stack) + + "

Counter from DataStore: " + + count + + "

"; + } + + 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("<", "<"); + content = content.replace(">", ">"); + content = content.replace("\"", """); + } 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("<", "<"); - content = content.replace(">", ">"); - content = content.replace("\"", """); - } catch (IOException e) { - e.printStackTrace(); + private Stack cacheHistory(String name) { + Stack stack = (Stack)memcacheService.get("history"); + if (stack == null) { + stack = new Stack(); } - return "[" + id - + "] Hello " - + name - + "


Content from Tuscany Web Site

" - + content - + "

History


" - + stack - + "

" - + count - + ""; + stack.push(new Date().toString() + ": " + name); + memcacheService.put("history", stack, Expiration.byDeltaSeconds(60)); + return stack; + } + + private String toHTML(Stack stack) { + StringBuffer html = new StringBuffer("

History from Memcache (expired in 1 minute)


    "); + for (String item : stack) { + html.append("
  • ").append(item).append("
  • "); + } + html.append("

"); + return html.toString(); } @Init -- cgit v1.2.3