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.BindingListenerWithException;
08: import com.tctest.webapp.listeners.InvalidatorBindingListener;
09:
10: import java.io.IOException;
11: import java.util.Date;
12: import java.util.HashMap;
13: import java.util.Map;
14:
15: import javax.servlet.http.HttpServlet;
16: import javax.servlet.http.HttpServletRequest;
17: import javax.servlet.http.HttpServletResponse;
18:
19: public final class InvalidatorServlet extends HttpServlet {
20: private static final Map callCounts = new HashMap();
21:
22: protected void doGet(HttpServletRequest req,
23: HttpServletResponse resp) throws IOException {
24: final String action = req.getParameter("action");
25: final String key = req.getParameter("key");
26: String reply = "OK";
27: if ("get".equals(action)) {
28: reply = key + "=" + req.getSession().getAttribute(key);
29: } else if ("set".equals(action)) {
30: req.getSession().setAttribute(key,
31: new InvalidatorBindingListener(key));
32: } else if ("setwithexception".equals(action)) {
33: try {
34: req.getSession().setAttribute(key,
35: new BindingListenerWithException(key));
36: reply = "Did not get expected exception!";
37: } catch (Throwable e) {
38: // this is expected
39: }
40: } else if ("remove".equals(action)) {
41: req.getSession().removeAttribute(key);
42: } else if ("call_count".equals(action)) {
43: reply = key + "=" + getCallCount(key);
44: } else if ("setmax".equals(action)) {
45: req.getSession().setMaxInactiveInterval(
46: Integer.parseInt(key));
47: } else if ("isNew".equals(action)) {
48: if (!req.getSession().isNew())
49: reply = "OLD SESSION!";
50: } else if ("isOld".equals(action)) {
51: if (req.getSession().isNew())
52: reply = "NEW SESSION!";
53: } else if ("sleep".equals(action)) {
54: // lock session and go to sleep
55: req.getSession();
56: sleep(1000 * Integer.parseInt(key));
57: req.getSession().setMaxInactiveInterval(30 * 60);
58: } else {
59: reply = "INVALID REQUEST";
60: }
61: resp.getWriter().print(reply);
62: resp.flushBuffer();
63: }
64:
65: private void sleep(int i) {
66: try {
67: Date now = new Date();
68: System.err.println("SERVLET: " + now
69: + ": going to sleep for " + i + " millis");
70: Thread.sleep(i);
71: now = new Date();
72: System.err.println("SERVLET: " + now
73: + ": woke up from sleeping for " + i + " millis");
74: } catch (InterruptedException e) {
75: e.printStackTrace();
76: }
77: }
78:
79: private synchronized static int getCallCount(String key) {
80: Integer i = (Integer) callCounts.get(key);
81: return i == null ? 0 : i.intValue();
82: }
83:
84: public synchronized static void incrementCallCount(String key) {
85: Integer i = (Integer) callCounts.get(key);
86: if (i == null) {
87: i = new Integer(1);
88: } else {
89: i = new Integer(i.intValue() + 1);
90: }
91: callCounts.put(key, i);
92: }
93: }
|