001    /**
002     * 
003     * $Revision: 14998 $ $Date: 2009-03-24 14:08:58 +0100 (Tue, 24 Mar 2009) $
004     *
005     * This file is part of ** M y C o R e **
006     * Visit our homepage at http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, normally in the file license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     *
023     **/
024    package org.mycore.common.events;
025    
026    import java.util.Enumeration;
027    
028    import javax.servlet.http.HttpSession;
029    import javax.servlet.http.HttpSessionBindingEvent;
030    import javax.servlet.http.HttpSessionBindingListener;
031    import javax.servlet.http.HttpSessionEvent;
032    import javax.servlet.http.HttpSessionListener;
033    
034    import org.apache.log4j.Logger;
035    import org.mycore.common.MCRSession;
036    
037    /**
038     * Handles different HttpSession events.
039     * 
040     * This class is used to free up MCRSessions when their associated HttpSession
041     * is destroyed or a new MCRSession replaces an old one.
042     * 
043     * @author Thomas Scheffler (yagee)
044     */
045    public class MCRHttpSessionListener implements HttpSessionListener, HttpSessionBindingListener {
046        Logger LOGGER = Logger.getLogger(MCRHttpSessionListener.class);
047    
048        /*
049         * (non-Javadoc)
050         * 
051         * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
052         */
053        public void sessionCreated(HttpSessionEvent hse) {
054        }
055    
056        /*
057         * (non-Javadoc)
058         * 
059         * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
060         */
061        @SuppressWarnings("unchecked")
062        public void sessionDestroyed(HttpSessionEvent hse) {
063            // clear MCRSessions
064            LOGGER.debug("HttpSession will be destroyed, clearing up.");
065            HttpSession httpSession = hse.getSession();
066            LOGGER.debug("Removing any MCRSessions from HttpSession");
067            for (Enumeration<String> e = httpSession.getAttributeNames(); e.hasMoreElements();) {
068                String key = e.nextElement();
069                if (httpSession.getAttribute(key) instanceof MCRSession) {
070                    MCRSession mcrSession = (MCRSession) httpSession.getAttribute(key);
071                    mcrSession.close();
072                    // remove reference in httpSession
073                    httpSession.removeAttribute(key);
074                }
075            }
076            LOGGER.debug("Clearing up done");
077        }
078    
079        public void valueBound(HttpSessionBindingEvent hsbe) {
080        }
081    
082        public void valueUnbound(HttpSessionBindingEvent hsbe) {
083            LOGGER.debug("Attribute " + hsbe.getName() + " is beeing unbound from session");
084            Object obj = hsbe.getValue();
085            if (obj instanceof MCRSession) {
086                MCRSession mcrSession = (MCRSession) obj;
087                mcrSession.close();
088            }
089            LOGGER.debug("Attribute " + hsbe.getName() + " is unbounded from session");
090        }
091    
092    }