Improve GUI and clean up code
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@823807 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b7878fb975
commit
89b23362d1
1 changed files with 52 additions and 26 deletions
|
@ -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.push(new Date().toString() + ": " + name);
|
||||
memcacheService.put("history", stack);
|
||||
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();
|
||||
}
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue