diff options
Diffstat (limited to '')
-rw-r--r-- | sandbox/rfeng/helloworld-jsp-google-appengine/src/sample/HelloworldServiceImpl.java | 76 |
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("<", "<"); + 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<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 |