001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cluster.web;
023:
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import java.net.InetAddress;
027: import javax.servlet.http.HttpServlet;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030: import javax.servlet.http.HttpSession;
031: import javax.servlet.ServletException;
032: import javax.servlet.ServletConfig;
033:
034: import org.apache.log4j.Logger;
035:
036: /**
037: * @author Scott.Stark@jboss.org
038: * @version $Revision: 57211 $
039: */
040: public class StatefulSessionServlet extends HttpServlet {
041: private static Logger log = Logger
042: .getLogger(StatefulSessionServlet.class);
043: private boolean synchSessionAccess;
044:
045: public void init(ServletConfig servletConfig)
046: throws ServletException {
047: super .init(servletConfig);
048: String flag = servletConfig
049: .getInitParameter("synchSessionAccess");
050: synchSessionAccess = Boolean.valueOf(flag).booleanValue();
051: }
052:
053: protected void doGet(HttpServletRequest request,
054: HttpServletResponse response) throws ServletException,
055: IOException {
056: handleRequest(request, response);
057: }
058:
059: protected void doPost(HttpServletRequest request,
060: HttpServletResponse response) throws ServletException,
061: IOException {
062: handleRequest(request, response);
063: }
064:
065: private void handleRequest(HttpServletRequest request,
066: HttpServletResponse response) throws IOException {
067: HttpSession session = request.getSession();
068: log.info("handleRequest, session=" + session.getId());
069: SessionValue value = null;
070: if (synchSessionAccess) {
071: synchronized (session) {
072: value = (SessionValue) session
073: .getAttribute("TheSessionKey");
074: }
075: } else {
076: value = (SessionValue) session
077: .getAttribute("TheSessionKey");
078: }
079:
080: SessionValue prevValue = null;
081: if (value == null) {
082: value = new SessionValue();
083: } else {
084: prevValue = new SessionValue();
085: prevValue.accessCount = value.accessCount;
086: prevValue.username = value.username;
087: prevValue.lastAccessHost = value.lastAccessHost;
088: }
089: value.accessCount++;
090: value.username = request.getRemoteUser();
091: value.lastAccessHost = request.getServerName();
092: if (synchSessionAccess) {
093: synchronized (session) {
094: session.setAttribute("TheSessionKey", value);
095: }
096: } else {
097: session.setAttribute("TheSessionKey", value);
098: }
099: log.info(value);
100:
101: PrintWriter pw = response.getWriter();
102: response.setContentType("text/html");
103: response.addIntHeader("X-AccessCount", value.accessCount);
104: pw.write("<html>\n");
105: String node = InetAddress.getLocalHost().getHostName();
106: pw.write("<head><title>StatefulSessionServlet on: " + node
107: + "</title></head>\n");
108: pw.write("<body><h1>StatefulSessionServlet on: " + node
109: + "</h1>\n");
110: pw.write("<h2>SessionID: " + session.getId() + "</h2>\n");
111: pw.write("<pre>\n");
112: pw.write(value.toString());
113: pw.write("\n</pre>\n");
114: if (prevValue != null) {
115: pw.write("<pre>\n");
116: pw.write(prevValue.toString());
117: pw.write("\n</pre>\n");
118: }
119: pw.write("</body>\n");
120: pw.write("</html>\n");
121: }
122: }
|