2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.usermanager.security;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
17 import java.util.Map.Entry;
20 import javax.servlet.ServletContext;
21 import javax.servlet.http.HttpSession;
22 import javax.servlet.http.HttpSessionEvent;
24 import org.opendaylight.controller.usermanager.ISessionManager;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class SessionManager implements ISessionManager {
30 private static final Logger logger = LoggerFactory
31 .getLogger(SessionManager.class);
33 private Map<ServletContext, Set<HttpSession>> sessionMap = new HashMap<ServletContext, Set<HttpSession>>();
36 public void sessionCreated(HttpSessionEvent se) {
38 ServletContext ctx = se.getSession().getServletContext();
39 String path = ctx.getContextPath();
41 logger.debug("Servlet Context Path created " + path);
42 logger.debug("Session Id created for ctxt path " + se.getSession().getId());
44 synchronized (sessionMap) {
45 Set<HttpSession> set = sessionMap.get(ctx);
47 set = new HashSet<HttpSession>();
48 sessionMap.put(ctx, set);
50 set.add(se.getSession());
55 public void sessionDestroyed(HttpSessionEvent se) {
56 ServletContext ctx = se.getSession().getServletContext();
57 String path = ctx.getContextPath();
58 logger.debug("Servlet Context Path of destroyed session - " + path);
59 logger.debug("Session Id destroyed " + se.getSession().getId());
61 synchronized (sessionMap) {
62 Set<HttpSession> set = sessionMap.get(ctx);
64 set.remove(se.getSession());
70 public void invalidateSessions(String username, String sessionId) {
72 synchronized (sessionMap) {
73 List<HttpSession> sessionsList = new ArrayList<HttpSession>();
74 Iterator<Map.Entry<ServletContext, Set<HttpSession>>> sessMapIterator = sessionMap
75 .entrySet().iterator();
76 while (sessMapIterator.hasNext()) {
78 Entry<ServletContext, Set<HttpSession>> val = sessMapIterator
80 Iterator<HttpSession> sessIterator = val.getValue().iterator();
82 while (sessIterator.hasNext()) {
83 HttpSession session = sessIterator.next();
84 if (session != null && sessionId != null && session.getId() != null && !session.getId().equals(sessionId)) {
85 sessionsList.add(session);
86 sessIterator.remove();
89 logger.debug(" session or sessionId is null ");
94 Iterator<HttpSession> sessionIt = sessionsList.iterator();
95 while (sessionIt.hasNext()) {
96 HttpSession session = sessionIt.next();