KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > sampleCluster2 > web > SessionServlet


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: SessionServlet.java,v 1.5 2005/04/28 12:17:19 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.sampleCluster2.web;
27
28 import java.io.IOException JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import javax.ejb.Handle JavaDoc;
32 import javax.rmi.PortableRemoteObject JavaDoc;
33 import javax.servlet.RequestDispatcher JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37 import javax.servlet.http.HttpSession JavaDoc;
38
39 import org.objectweb.sampleCluster2.ejb.MyEjb1;
40 import org.objectweb.sampleCluster2.ejb.MyStateful;
41
42 import org.objectweb.util.monolog.api.BasicLevel;
43
44 /**
45  * @author goebelg
46  *
47  * servlet which manages the different instantiations of the ejbs
48  */

49 public class SessionServlet extends AbstractServlet {
50
51     /**
52      *
53      */

54     private static final long serialVersionUID = 1L;
55
56     /**
57      * doGet methode of the servlet
58      * @param req http servlet request
59      * @param res http servlet response
60      * @throws ServletException servlet exception
61      * @throws IOException io exception
62      */

63     public void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws ServletException JavaDoc, IOException JavaDoc {
64         String JavaDoc jonasEJBServer = null;
65         String JavaDoc ejbId = null;
66         String JavaDoc ejbTotalCallsCount = null;
67         String JavaDoc ejbEntityCreated = null;
68         Properties JavaDoc prop = null;
69
70         /*
71          * ---------------------------------------- Update the session context -
72          * sessiontest.counter : number of times this servlet has been called -
73          * sessiontest.jonas. <name> : counts the number of time the EJB was
74          * served by <name> JOnAS instance
75          * -----------------------------------------
76          */

77         HttpSession JavaDoc session = req.getSession(true);
78
79         // update sessiontest.counter
80

81         Integer JavaDoc ival = (Integer JavaDoc) session.getAttribute("sessiontest.counter");
82         if (ival == null) { // If counter is not in session, then create it
83
ival = new Integer JavaDoc(1);
84         } else {
85             ival = new Integer JavaDoc(ival.intValue() + 1);
86         }
87         session.setAttribute("sessiontest.counter", ival);
88
89         // Call EJB then update sessiontest.jonas.x
90

91         // creating a new statless EJB -> EJBCreate
92
MyEjb1 bean = theEJB();
93         try {
94             prop = bean.getInfoProps();
95             jonasEJBServer = prop.getProperty("EJB server");
96             ejbId = prop.getProperty("EJB id");
97             ejbTotalCallsCount = prop.getProperty("EJB total calls");
98             ejbEntityCreated = prop.getProperty("EJB server entity created");
99         } catch (Exception JavaDoc e) {
100             e.printStackTrace();
101         }
102
103         // Retrieve or create the Stateful EJB to log history
104
Handle JavaDoc h = (Handle JavaDoc) session.getAttribute("myStateful");
105         MyStateful statefulBean = null;
106         if (h == null) {
107
108             statefulBean = createStatefulEJB();
109             if (statefulBean != null) {
110                 h = statefulBean.getHandle();
111                 session.setAttribute("myStateful", h);
112                 statefulBean.setHTTPSessionId(session.getId());
113             }
114         } else {
115             statefulBean = (MyStateful) PortableRemoteObject.narrow(h.getEJBObject(), MyStateful.class);
116         }
117
118         if (!session.isNew()) {
119             session.setAttribute("sessiontest.old.EJB", session.getAttribute("sessiontest.EJB"));
120             session.setAttribute("sessiontest.old.WEB", session.getAttribute("sessiontest.WEB"));
121         }
122         session.setAttribute("sessiontest.EJB", jonasEJBServer);
123         session.setAttribute("sessiontest.WEB", getMyJonasInstanceName());
124
125         req.setAttribute("jonas.EJB.served.by", jonasEJBServer);
126         req.setAttribute("jonas.WEB.served.by", getMyJonasInstanceName());
127
128         session.setAttribute("sessiontest.jonas.EJB_Server", jonasEJBServer);
129         session.setAttribute("sessiontest.jonas.EJB_id", ejbId);
130         session.setAttribute("sessiontest.jonas.nb_EJB_totalCalls", ejbTotalCallsCount);
131
132         // Log information
133

134         getLogger().log(BasicLevel.INFO, "session=" + session.getId() + " on jonas=" + getMyJonasInstanceName()
135                 + " - calls=" + ival + " - EJB served by jonas=" + jonasEJBServer + " - EJB total calls="
136                 + ejbTotalCallsCount);
137
138         if (statefulBean != null) {
139                 try {
140                     statefulBean.log("<tr>" + "<td>" + getMyJonasInstanceName() + "</td>" + "<td>" + jonasEJBServer
141                             + "</td>" + "<td>" + ejbTotalCallsCount + "</td>" + "<td>" + ejbEntityCreated + "</td>"
142                             + "</tr>");
143                 } catch (Exception JavaDoc e) {
144                     getLogger().log(BasicLevel.INFO, "session=" + session.getId()
145                             + " : Exception when calling MyStateful EJB (" + e.getMessage() + ")");
146                     session.removeAttribute("myStateful");
147                 }
148         }
149         // --------------
150
// Write response
151
// --------------
152

153         RequestDispatcher JavaDoc disp = req.getRequestDispatcher("../jsp/sessionRsp.jsp");
154         disp.forward(req, res);
155
156         return;
157     }
158 }
159
160
Popular Tags