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.web.servlets;
023:
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import javax.ejb.Handle;
027: import javax.naming.Context;
028: import javax.naming.InitialContext;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServlet;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033: import javax.servlet.http.HttpSession;
034: import javax.servlet.http.HttpSessionActivationListener;
035: import javax.servlet.http.HttpSessionEvent;
036:
037: import org.apache.log4j.Logger;
038: import org.jboss.test.web.interfaces.StatelessSession;
039: import org.jboss.test.web.interfaces.StatelessSessionHome;
040:
041: /** A servlet that accesses a stateful session EJB and stores a handle in the session context
042: * to test retrieval of the session from the handle.
043:
044: @author Scott.Stark@jboss.org
045: @version $Revision: 57211 $
046: */
047: public class StatefulSessionServlet extends HttpServlet {
048: static private Logger log = Logger
049: .getLogger(StatefulSessionServlet.class);
050:
051: static class SessionHandle implements HttpSessionActivationListener {
052: Handle h;
053:
054: SessionHandle(Handle h) {
055: this .h = h;
056: }
057:
058: public void sessionWillPassivate(HttpSessionEvent event) {
059: log.info("sessionWillPassivate, event=" + event);
060: }
061:
062: public void sessionDidActivate(HttpSessionEvent event) {
063: log.info("sessionDidActivate, event=" + event);
064: }
065: }
066:
067: protected void processRequest(HttpServletRequest request,
068: HttpServletResponse response) throws ServletException,
069: IOException {
070: HttpSession session = request.getSession();
071: try {
072: StatelessSession localBean = null;
073: // See if there is an existing session
074: if (session.isNew()) {
075: log.info("Creating a new stateful session");
076: InitialContext ctx = new InitialContext();
077: Context enc = (Context) ctx.lookup("java:comp/env");
078: StatelessSessionHome localHome = (StatelessSessionHome) enc
079: .lookup("ejb/StatefulEJB");
080: localBean = localHome.create();
081: Handle h = localBean.getHandle();
082: SessionHandle wrapper = new SessionHandle(h);
083: session.setAttribute("StatefulEJB", wrapper);
084: } else {
085: log.info("Getting existing stateful session");
086: SessionHandle wrapper = (SessionHandle) session
087: .getAttribute("StatefulEJB");
088: localBean = (StatelessSession) wrapper.h.getEJBObject();
089: }
090: localBean.echo("Hello");
091: } catch (Exception e) {
092: throw new ServletException("Failed to call StatefulEJB", e);
093: }
094: response.setContentType("text/html");
095: PrintWriter out = response.getWriter();
096: out.println("<html>");
097: out
098: .println("<head><title>StatefulSessionServlet</title></head>");
099: out.println("<body>");
100: out.println("<h1>Session Information</h1>");
101: out.println("SessionID: " + session.getId());
102: out.println("IsNew: " + session.isNew());
103: out.println("CreationTime: " + session.getCreationTime());
104: out.println("LastAccessedTime: "
105: + session.getLastAccessedTime());
106: out.println("Now: " + System.currentTimeMillis());
107: out.println("MaxInactiveInterval: "
108: + session.getMaxInactiveInterval());
109: out.println("</body>");
110: out.println("</html>");
111: out.close();
112: }
113:
114: protected void doGet(HttpServletRequest request,
115: HttpServletResponse response) throws ServletException,
116: IOException {
117: processRequest(request, response);
118: }
119:
120: protected void doPost(HttpServletRequest request,
121: HttpServletResponse response) throws ServletException,
122: IOException {
123: processRequest(request, response);
124: }
125: }
|