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: ReleaseServlet.java 9280 2006-08-01 10:15:24Z japaz $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.sampleCluster2.web;
025:
026: import java.io.IOException;
027: import java.rmi.RemoteException;
028:
029: import javax.ejb.Handle;
030: import javax.ejb.RemoveException;
031: import javax.rmi.PortableRemoteObject;
032: import javax.servlet.RequestDispatcher;
033: import javax.servlet.ServletException;
034: import javax.servlet.http.Cookie;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037: import javax.servlet.http.HttpSession;
038:
039: import org.objectweb.sampleCluster2.ejb.MyStateful;
040: import org.objectweb.util.monolog.api.BasicLevel;
041:
042: /**
043: * @author goebelg Release an http session servlet
044: */
045: public class ReleaseServlet extends AbstractServlet {
046:
047: /**
048: *
049: */
050: private static final long serialVersionUID = 1L;
051:
052: /**
053: * doGet methode of the servlet
054: * @param req http servlet request
055: * @param res http servlet response
056: * @throws ServletException servlet exception
057: * @throws IOException io exception
058: */
059: public void doGet(HttpServletRequest req, HttpServletResponse res)
060: throws ServletException, IOException {
061:
062: // Get the session object to invalidate it
063:
064: HttpSession session = req.getSession(false);
065: String sessionToReleaseInfo = "no session to invalidate";
066: if (session != null) {
067: sessionToReleaseInfo = "session=" + session.getId()
068: + " invalidated";
069: // Remove the SFSB if present
070: // Retrieve or create the Stateful EJB to log history
071: Handle h = (Handle) session.getAttribute("myStateful");
072: if (h != null) {
073: MyStateful statefulBean = (MyStateful) PortableRemoteObject
074: .narrow(h.getEJBObject(), MyStateful.class);
075: try {
076: statefulBean.remove();
077: } catch (RemoteException e) {
078: e.printStackTrace();
079: } catch (RemoveException e) {
080: e.printStackTrace();
081: }
082: }
083:
084: session.invalidate();
085: }
086: getLogger().log(BasicLevel.INFO, sessionToReleaseInfo);
087: req.setAttribute("sessionToReleaseInfo", sessionToReleaseInfo);
088:
089: Cookie[] cookies = req.getCookies();
090: if (cookies != null) {
091: Cookie ck = null;
092: for (int i = 0; i < cookies.length; i++) {
093: if (cookies[i].getName().equals("JSESSIONID")) {
094: ck = (Cookie) cookies[i].clone();
095: ck.setMaxAge(0);
096: res.addCookie(ck);
097: }
098: }
099: }
100:
101: // --------------
102: // Write response
103: // --------------
104:
105: RequestDispatcher disp = req
106: .getRequestDispatcher("../jsp/releaseRsp.jsp");
107: disp.forward(req, res);
108:
109: return;
110: }
111: }
|