KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > dd > web > servlets > StatefulSessionServlet


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.dd.web.servlets;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import javax.ejb.Handle JavaDoc;
27 import javax.naming.Context JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.http.HttpServlet JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.http.HttpSession JavaDoc;
34 import javax.servlet.http.HttpSessionActivationListener JavaDoc;
35 import javax.servlet.http.HttpSessionEvent JavaDoc;
36
37 import org.jboss.logging.Logger;
38 import org.jboss.ejb3.test.dd.web.interfaces.StatelessSession;
39 import org.jboss.security.SecurityAssociation;
40 import org.jboss.security.SimplePrincipal;
41
42 /** A servlet that accesses a stateful session EJB and stores a handle in the session context
43  * to test retrieval of the session from the handle.
44
45  @author Scott.Stark@jboss.org
46  @version $Revision: 58110 $
47  */

48 public class StatefulSessionServlet extends HttpServlet JavaDoc
49 {
50    static private Logger log = Logger.getLogger(StatefulSessionServlet.class);
51
52    static class SessionHandle implements HttpSessionActivationListener JavaDoc
53    {
54       Handle JavaDoc h;
55       SessionHandle(Handle JavaDoc h)
56       {
57          this.h = h;
58       }
59       public void sessionWillPassivate(HttpSessionEvent JavaDoc event)
60       {
61          log.info("sessionWillPassivate, event="+event);
62       }
63
64       public void sessionDidActivate(HttpSessionEvent JavaDoc event)
65       {
66          log.info("sessionDidActivate, event="+event);
67       }
68    }
69    
70    protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
71          throws ServletException JavaDoc, IOException JavaDoc
72    {
73       SecurityAssociation.setPrincipal(new SimplePrincipal("jduke"));
74       SecurityAssociation.setCredential("theduke".toCharArray());
75       
76       HttpSession JavaDoc session = request.getSession();
77       try
78       {
79          StatelessSession localBean = null;
80          // See if there is an existing session
81
if( session.isNew() )
82          {
83             log.info("Creating a new stateful session");
84             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
85             Context JavaDoc enc = (Context JavaDoc) ctx.lookup("java:comp/env");
86             localBean = (StatelessSession) enc.lookup("ejb/StatefulEJB");
87          }
88          else
89          {
90             log.info("Getting existing stateful session");
91             SessionHandle wrapper = (SessionHandle) session.getAttribute("StatefulEJB");
92             localBean = (StatelessSession) wrapper.h.getEJBObject();
93          }
94          localBean.echo("Hello");
95       }
96       catch (Exception JavaDoc e)
97       {
98          throw new ServletException JavaDoc("Failed to call StatefulEJB", e);
99       }
100       response.setContentType("text/html");
101       PrintWriter JavaDoc out = response.getWriter();
102       out.println("<html>");
103       out.println("<head><title>StatefulSessionServlet</title></head>");
104       out.println("<body>");
105       out.println("<h1>Session Information</h1>");
106       out.println("SessionID: "+session.getId());
107       out.println("IsNew: "+session.isNew());
108       out.println("CreationTime: "+session.getCreationTime());
109       out.println("LastAccessedTime: "+session.getLastAccessedTime());
110       out.println("Now: "+System.currentTimeMillis());
111       out.println("MaxInactiveInterval: "+session.getMaxInactiveInterval());
112       out.println("</body>");
113       out.println("</html>");
114       out.close();
115    }
116
117    protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
118          throws ServletException JavaDoc, IOException JavaDoc
119    {
120       processRequest(request, response);
121    }
122
123    protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
124          throws ServletException JavaDoc, IOException JavaDoc
125    {
126       processRequest(request, response);
127    }
128 }
129
Popular Tags