01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tctest.webapp.servlets;
06:
07: import com.tctest.webapp.listeners.BindingListener;
08:
09: import java.io.IOException;
10: import java.util.HashMap;
11: import java.util.Map;
12:
13: import javax.servlet.http.HttpServlet;
14: import javax.servlet.http.HttpServletRequest;
15: import javax.servlet.http.HttpServletResponse;
16:
17: public class ListenerReportingServlet extends HttpServlet {
18: private static Map callCounts = new HashMap();
19:
20: protected void doGet(HttpServletRequest req,
21: HttpServletResponse resp) throws IOException {
22: // requests should have 2 standard params: action=get|set|remove|call_count and key
23: final String action = req.getParameter("action");
24: final String key = req.getParameter("key");
25: String reply = "OK";
26: if ("get".equals(action)) {
27: reply = key + "=" + req.getSession().getAttribute(key);
28: } else if ("set".equals(action)) {
29: req.getSession()
30: .setAttribute(key, new BindingListener(key));
31: } else if ("remove".equals(action)) {
32: req.getSession().removeAttribute(key);
33: } else if ("call_count".equals(action)) {
34: reply = key + "=" + getCallCount(key);
35: } else if ("invalidate".equals(action)) {
36: req.getSession().invalidate();
37: } else if ("isNew".equals(action)) {
38: if (!req.getSession().isNew())
39: reply = "ERROR: OLD SESSION!";
40: } else {
41: reply = "INVALID REQUEST";
42: }
43: resp.getWriter().print(reply);
44: resp.flushBuffer();
45: }
46:
47: private synchronized static int getCallCount(String key) {
48: Integer i = (Integer) callCounts.get(key);
49: return i == null ? 0 : i.intValue();
50: }
51:
52: public synchronized static void incrementCallCount(String key) {
53: Integer i = (Integer) callCounts.get(key);
54: if (i == null) {
55: i = new Integer(1);
56: } else {
57: i = new Integer(i.intValue() + 1);
58: }
59: callCounts.put(key, i);
60: }
61: }
|