001/*
002 *  Copyright 2011 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.repositoryapp;
017
018import java.util.Map;
019
020import javax.jcr.Session;
021import javax.servlet.http.HttpSession;
022import javax.servlet.http.HttpSessionEvent;
023import javax.servlet.http.HttpSessionListener;
024
025
026/**
027 * Listens session destruction to ensure that the JCR sessions are properly logged out.
028 */
029public class JCRRepositorySessionListener implements HttpSessionListener
030{
031
032    @Override
033    public void sessionCreated(HttpSessionEvent se)
034    {
035        // Ignore the session creation.
036    }
037    
038    @SuppressWarnings("unchecked")
039    @Override
040    public void sessionDestroyed(HttpSessionEvent se)
041    {
042        HttpSession httpSession = se.getSession();
043        
044        Map<String, Session> jcrSessions = (Map<String, Session>) httpSession.getAttribute(RepositoryProvider.SESSION_JCR_SESSIONS_KEY);
045        
046        if (jcrSessions != null)
047        {
048            for (Session session : jcrSessions.values())
049            {
050                session.logout();
051            }
052        }
053    }
054}