001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: SessionServlet.java 8276 2006-04-24 14:19:40Z japaz $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.sampleCluster2.web;
025:
026: import java.io.IOException;
027: import java.util.Properties;
028:
029: import javax.ejb.Handle;
030: import javax.rmi.PortableRemoteObject;
031: import javax.servlet.RequestDispatcher;
032: import javax.servlet.ServletException;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035: import javax.servlet.http.HttpSession;
036:
037: import org.objectweb.sampleCluster2.ejb.MyEjb1;
038: import org.objectweb.sampleCluster2.ejb.MyStateful;
039:
040: import org.objectweb.util.monolog.api.BasicLevel;
041:
042: /**
043: * @author goebelg
044: *
045: * servlet which manages the different instantiations of the ejbs
046: */
047: public class SessionServlet extends AbstractServlet {
048:
049: /**
050: *
051: */
052: private static final long serialVersionUID = 1L;
053:
054: /**
055: * doGet methode of the servlet
056: * @param req http servlet request
057: * @param res http servlet response
058: * @throws ServletException servlet exception
059: * @throws IOException io exception
060: */
061: public void doGet(HttpServletRequest req, HttpServletResponse res)
062: throws ServletException, IOException {
063: String jonasEJBServer = null;
064: String ejbId = null;
065: String ejbTotalCallsCount = null;
066: String ejbEntityCreated = null;
067: Properties prop = null;
068:
069: /*
070: * ---------------------------------------- Update the session context -
071: * sessiontest.counter : number of times this servlet has been called -
072: * sessiontest.jonas. <name> : counts the number of time the EJB was
073: * served by <name> JOnAS instance
074: * -----------------------------------------
075: */
076: HttpSession session = req.getSession(true);
077:
078: // update sessiontest.counter
079:
080: Integer ival = (Integer) session
081: .getAttribute("sessiontest.counter");
082: if (ival == null) { // If counter is not in session, then create it
083: ival = new Integer(1);
084: } else {
085: ival = new Integer(ival.intValue() + 1);
086: }
087: session.setAttribute("sessiontest.counter", ival);
088:
089: // Call EJB then update sessiontest.jonas.x
090:
091: // creating a new stateless EJB -> EJBCreate
092: MyEjb1 bean = theEJB();
093: try {
094: prop = bean.getInfoProps();
095: jonasEJBServer = prop.getProperty("EJB server");
096: ejbId = prop.getProperty("EJB id");
097: ejbTotalCallsCount = prop.getProperty("EJB total calls");
098: ejbEntityCreated = prop
099: .getProperty("EJB server entity created");
100: } catch (Exception e) {
101: e.printStackTrace();
102: }
103:
104: // Retrieve or create the Stateful EJB to log history
105: Handle h = (Handle) session.getAttribute("myStateful");
106: MyStateful statefulBean = null;
107: if (h == null) {
108:
109: statefulBean = createStatefulEJB();
110: if (statefulBean != null) {
111: h = statefulBean.getHandle();
112: session.setAttribute("myStateful", h);
113: statefulBean.setHTTPSessionId(session.getId());
114: }
115: } else {
116: statefulBean = (MyStateful) PortableRemoteObject.narrow(h
117: .getEJBObject(), MyStateful.class);
118: }
119:
120: if (!session.isNew()) {
121: session.setAttribute("sessiontest.old.EJB", session
122: .getAttribute("sessiontest.EJB"));
123: session.setAttribute("sessiontest.old.WEB", session
124: .getAttribute("sessiontest.WEB"));
125: }
126: session.setAttribute("sessiontest.EJB", jonasEJBServer);
127: session.setAttribute("sessiontest.WEB",
128: getMyJonasInstanceName());
129:
130: req.setAttribute("jonas.EJB.served.by", jonasEJBServer);
131: req.setAttribute("jonas.WEB.served.by",
132: getMyJonasInstanceName());
133:
134: session.setAttribute("sessiontest.jonas.EJB_Server",
135: jonasEJBServer);
136: session.setAttribute("sessiontest.jonas.EJB_id", ejbId);
137: session.setAttribute("sessiontest.jonas.nb_EJB_totalCalls",
138: ejbTotalCallsCount);
139:
140: // Log information
141:
142: getLogger().log(
143: BasicLevel.INFO,
144: "session=" + session.getId() + " on jonas="
145: + getMyJonasInstanceName() + " - calls=" + ival
146: + " - EJB served by jonas=" + jonasEJBServer
147: + " - EJB total calls=" + ejbTotalCallsCount);
148:
149: if (statefulBean != null) {
150: try {
151: statefulBean.log("<tr>" + "<td>"
152: + getMyJonasInstanceName() + "</td>" + "<td>"
153: + jonasEJBServer + "</td>" + "<td>"
154: + ejbTotalCallsCount + "</td>" + "<td>"
155: + ejbEntityCreated + "</td>" + "</tr>");
156: } catch (Exception e) {
157: getLogger()
158: .log(
159: BasicLevel.INFO,
160: "session="
161: + session.getId()
162: + " : Exception when calling MyStateful EJB ("
163: + e.getMessage() + ")");
164: session.removeAttribute("myStateful");
165: }
166: }
167: // --------------
168: // Write response
169: // --------------
170:
171: RequestDispatcher disp = req
172: .getRequestDispatcher("../jsp/sessionRsp.jsp");
173: disp.forward(req, res);
174:
175: return;
176: }
177: }
|