01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tctest.webapp.servlets;
05:
06: import java.io.IOException;
07: import java.io.PrintWriter;
08: import java.text.SimpleDateFormat;
09: import java.util.Date;
10:
11: import javax.servlet.http.HttpServlet;
12: import javax.servlet.http.HttpServletRequest;
13: import javax.servlet.http.HttpServletResponse;
14: import javax.servlet.http.HttpSession;
15:
16: public final class SynchWriteMultiThreadsTestServlet extends
17: HttpServlet {
18: public void doGet(HttpServletRequest request,
19: HttpServletResponse response) throws IOException {
20: HttpSession session = request.getSession(true);
21: response.setContentType("text/html");
22: PrintWriter out = response.getWriter();
23:
24: String server = request.getParameter("server");
25: String command = request.getParameter("command");
26: String data = request.getParameter("data");
27:
28: switch (Integer.parseInt(server)) {
29: case 0:
30: handleServer0(session, out, command, data);
31: break;
32: case 1:
33: handleServer1(session, out, command, data);
34: break;
35: default:
36: out.print("unknown value for server param: " + server);
37: }
38: out.flush();
39: }
40:
41: private void handleServer0(HttpSession session, PrintWriter out,
42: String command, String data) {
43: if (command.equals("ping")) {
44: session.setAttribute("ping", "pong");
45: out.println("OK");
46: } else if (command.equals("insert")) {
47: session.setAttribute("data" + data, data + "");
48: out.println("OK");
49: } else if (command.equals("kill")) {
50: out.println("OK");
51: System.err.println("Execute order 66... halt.");
52: System.err.flush();
53: Runtime.getRuntime().halt(1);
54: }
55: }
56:
57: private void handleServer1(HttpSession session, PrintWriter out,
58: String command, String data) {
59: if (command.equals("ping")) {
60: String ping = (String) session.getAttribute("ping");
61: if (ping == null) {
62: out.println("ping is null");
63: } else
64: out.println("OK");
65: } else if (command.equals("query")) {
66:
67: String log = "** "
68: + new SimpleDateFormat("HH:mm:ss")
69: .format(new Date()) + " | sessionId="
70: + session.getId().substring(0, 5) + " | command="
71: + command + " | data=" + data;
72:
73: String queried_data = (String) session.getAttribute("data"
74: + data);
75: if (queried_data == null) {
76: out.println("data" + data + " is null");
77: } else
78: out.println(queried_data);
79:
80: System.err.println(log + "## found=" + queried_data);
81: System.err.flush();
82: }
83: }
84: }
|