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.ejb3.test.dd.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.jboss.logging.Logger;
038: import org.jboss.ejb3.test.dd.web.interfaces.StatelessSession;
039: import org.jboss.security.SecurityAssociation;
040: import org.jboss.security.SimplePrincipal;
041:
042: /** A servlet that accesses a stateful session EJB and stores a handle in the session context
043: * to test retrieval of the session from the handle.
044:
045: @author Scott.Stark@jboss.org
046: @version $Revision: 60233 $
047: */
048: public class StatefulSessionServlet extends HttpServlet {
049: static private Logger log = Logger
050: .getLogger(StatefulSessionServlet.class);
051:
052: static class SessionHandle implements HttpSessionActivationListener {
053: Handle h;
054:
055: SessionHandle(Handle h) {
056: this .h = h;
057: }
058:
059: public void sessionWillPassivate(HttpSessionEvent event) {
060: log.info("sessionWillPassivate, event=" + event);
061: }
062:
063: public void sessionDidActivate(HttpSessionEvent event) {
064: log.info("sessionDidActivate, event=" + event);
065: }
066: }
067:
068: protected void processRequest(HttpServletRequest request,
069: HttpServletResponse response) throws ServletException,
070: IOException {
071: SecurityAssociation.setPrincipal(new SimplePrincipal("jduke"));
072: SecurityAssociation.setCredential("theduke".toCharArray());
073:
074: HttpSession session = request.getSession();
075: try {
076: StatelessSession localBean = null;
077: // See if there is an existing session
078: if (session.isNew()) {
079: log.info("Creating a new stateful session");
080: InitialContext ctx = new InitialContext();
081: Context enc = (Context) ctx.lookup("java:comp/env");
082: localBean = (StatelessSession) enc
083: .lookup("ejb/StatefulEJB");
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: }
|