2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
5 package org.opendaylight.controller.clustersession;
7 import java.io.IOException;
8 import java.util.HashMap;
10 import org.apache.catalina.LifecycleException;
11 import org.apache.catalina.LifecycleState;
12 import org.apache.catalina.Session;
13 import org.apache.catalina.session.ManagerBase;
14 import org.apache.catalina.util.SessionIdGenerator;
15 import org.opendaylight.controller.clustersession.impl.ClusterSessionServiceImpl;
16 import org.opendaylight.controller.clustersession.service.ClusterSessionService;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 * ClusterSession Manager is a custom session manager, that is used to persist session data
21 * across cluster of a storage such as infinispan or memcache
22 * @author harman singh
25 public class ClusterSessionManager extends ManagerBase{
27 * Has this component been _started yet?
29 protected boolean started = false;
31 protected ClusterSessionService sessionService;
33 private static final Logger LOGGER = LoggerFactory.getLogger(ClusterSessionManager.class);
35 * The descriptive information about this implementation.
37 protected static final String INFO = "ClusterSessionManager/1.0";
40 * The descriptive name of this Manager implementation (for logging).
42 protected static final String NAME = "ClusterSessionManager";
44 public ClusterSessionManager(){
45 sessionService = new ClusterSessionServiceImpl(this);
49 * Return descriptive information about this Manager implementation and
50 * the corresponding version number, in the format
51 * <code><description>/<version></code>.
54 public String getInfo(){
59 * Return the descriptive short name of this Manager implementation.
62 public String getName(){
70 public void load() throws ClassNotFoundException, IOException {
71 // We are not persisting any session in database, infinispan does not persist data.
72 // loading of persisted session is not required.
79 public void unload() throws IOException {
80 // We are not persisting any session in database, infinispan does not persist data.
81 // unloading of session to persistence layer is not required.
85 * Start this component and implement the requirements
86 * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
88 * @exception LifecycleException if this component detects a fatal error
89 * that prevents this component from being used
92 protected synchronized void startInternal() throws LifecycleException {
93 sessionIdGenerator = new SessionIdGenerator();
94 sessionIdGenerator.setJvmRoute(getJvmRoute());
95 sessionIdGenerator.setSecureRandomAlgorithm(getSecureRandomAlgorithm());
96 sessionIdGenerator.setSecureRandomClass(getSecureRandomClass());
97 sessionIdGenerator.setSecureRandomProvider(getSecureRandomProvider());
98 sessionIdGenerator.setSessionIdLength(getSessionIdLength());
99 sessionService.startInternal(sessionIdGenerator);
100 setState(LifecycleState.STARTING);
104 * Stop this component and implement the requirements
105 * of {@link org.apache.catalina.util.LifecycleBase#stopInternal()}.
107 * @exception LifecycleException if this component detects a fatal error
108 * that prevents this component from being used
111 protected synchronized void stopInternal() throws LifecycleException {
112 setState(LifecycleState.STOPPING);
114 // Expire all active sessions
115 Session sessions[] = findSessions();
116 for (int i = 0; i < sessions.length; i++) {
117 Session session = sessions[i];
119 if (session.isValid()) {
122 } catch (Exception e) {
123 LOGGER.warn(e.toString());
125 // Measure against memory leaking if references to the session
126 // object are kept in a shared field somewhere
130 // Require a new random number generator if we are restarted
131 super.stopInternal();
132 sessionService.stopInternal();
139 public void expireSession(final String sessionId){
140 LOGGER.debug("SESSION EXPIRE : ", sessionId);
141 sessionService.expireSession(sessionId);
148 public void remove(final Session session){
149 LOGGER.debug("SESSION REMOVE : ", session.getId());
150 sessionService.removeSession(session.getId());
157 public void remove(Session session, boolean update) {
158 sessionService.removeSession(session.getId());
165 public Session findSession(final String id) throws IOException{
166 return sessionService.findSession(id);
173 public Session createSession(final String sessionId){
174 LOGGER.debug("SESSION CREATE : ", sessionId);
175 if(sessionId != null){
176 Session session = sessionService.findSession(sessionId);
181 return sessionService.createSession(sessionId);
188 public Session createEmptySession(){
189 return sessionService.createEmptySession();
195 public void add(Session session){
196 LOGGER.debug("SESSION ADD : ", session.getId());
197 sessionService.addSession((ClusterSession)session);
203 public HashMap<String, String> getSession(String sessionId){
204 return sessionService.getSession(sessionId);
210 public Session[] findSessions() {
211 return sessionService.findSessions();
214 public ClusterSessionService getSessionService() {
215 return sessionService;
218 public void setSessionService(ClusterSessionService sessionService) {
219 this.sessionService = sessionService;