Make sure invokeOperation is set once
[controller.git] / opendaylight / adsal / samples / clustersession / src / main / java / org / opendaylight / controller / clustersession / ClusterSessionManager.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  */
5 package org.opendaylight.controller.clustersession;
6
7 import java.io.IOException;
8 import java.util.HashMap;
9
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;
19 /**
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
23  *
24  */
25 public class ClusterSessionManager extends ManagerBase{
26   /**
27    * Has this component been _started yet?
28    */
29   protected boolean started = false;
30
31   protected ClusterSessionService sessionService;
32
33   private static final Logger LOGGER = LoggerFactory.getLogger(ClusterSessionManager.class);
34   /**
35    * The descriptive information about this implementation.
36    */
37   protected static final String INFO = "ClusterSessionManager/1.0";
38
39   /**
40    * The descriptive name of this Manager implementation (for logging).
41    */
42   protected static final String NAME = "ClusterSessionManager";
43
44   public ClusterSessionManager(){
45     sessionService = new ClusterSessionServiceImpl(this);
46   }
47
48   /**
49    * Return descriptive information about this Manager implementation and
50    * the corresponding version number, in the format
51    * <code>&lt;description&gt;/&lt;version&gt;</code>.
52    */
53   @Override
54   public String getInfo(){
55     return INFO;
56   }
57
58   /**
59    * Return the descriptive short name of this Manager implementation.
60    */
61   @Override
62   public String getName(){
63     return NAME;
64   }
65
66   /**
67    * {@inheritDoc}
68    */
69   @Override
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.
73   }
74
75   /**
76    * {@inheritDoc}
77    */
78   @Override
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.
82   }
83
84   /**
85    * Start this component and implement the requirements
86    * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
87    *
88    * @exception LifecycleException if this component detects a fatal error
89    *  that prevents this component from being used
90    */
91   @Override
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);
101   }
102
103   /**
104    * Stop this component and implement the requirements
105    * of {@link org.apache.catalina.util.LifecycleBase#stopInternal()}.
106    *
107    * @exception LifecycleException if this component detects a fatal error
108    *  that prevents this component from being used
109    */
110   @Override
111   protected synchronized void stopInternal() throws LifecycleException {
112     setState(LifecycleState.STOPPING);
113
114     // Expire all active sessions
115     Session sessions[] = findSessions();
116     for (int i = 0; i < sessions.length; i++) {
117       Session session = sessions[i];
118       try {
119         if (session.isValid()) {
120           session.expire();
121         }
122       } catch (Exception e) {
123         LOGGER.warn(e.toString());
124       } finally {
125         // Measure against memory leaking if references to the session
126         // object are kept in a shared field somewhere
127         session.recycle();
128       }
129     }
130     // Require a new random number generator if we are restarted
131     super.stopInternal();
132     sessionService.stopInternal();
133   }
134
135   /**
136    * {@inheritDoc}
137    */
138   @Override
139   public void expireSession(final String sessionId){
140     LOGGER.debug("SESSION EXPIRE : ", sessionId);
141     sessionService.expireSession(sessionId);
142   }
143
144   /**
145    * {@inheritDoc}
146    */
147   @Override
148   public void remove(final Session session){
149     LOGGER.debug("SESSION REMOVE : ", session.getId());
150     sessionService.removeSession(session.getId());
151   }
152
153   /**
154    * {@inheritDoc}
155    */
156   @Override
157   public void remove(Session session, boolean update) {
158     sessionService.removeSession(session.getId());
159   }
160
161   /**
162    * {@inheritDoc}
163    */
164   @Override
165   public Session findSession(final String id) throws IOException{
166     return sessionService.findSession(id);
167   }
168
169   /**
170    * {@inheritDoc}
171    */
172   @Override
173   public Session createSession(final String sessionId){
174     LOGGER.debug("SESSION CREATE : ", sessionId);
175     if(sessionId != null){
176       Session session = sessionService.findSession(sessionId);
177       if(session != null){
178         return session;
179       }
180     }
181     return sessionService.createSession(sessionId);
182   }
183
184   /**
185    * {@inheritDoc}
186    */
187   @Override
188   public Session createEmptySession(){
189     return sessionService.createEmptySession();
190   }
191   /**
192    * {@inheritDoc}
193    */
194   @Override
195   public void add(Session session){
196     LOGGER.debug("SESSION ADD : ", session.getId());
197     sessionService.addSession((ClusterSession)session);
198   }
199   /**
200    * {@inheritDoc}
201    */
202   @Override
203   public HashMap<String, String> getSession(String sessionId){
204     return sessionService.getSession(sessionId);
205   }
206   /**
207    * {@inheritDoc}
208    */
209   @Override
210   public Session[] findSessions() {
211     return sessionService.findSessions();
212   }
213
214   public ClusterSessionService getSessionService() {
215     return sessionService;
216   }
217
218   public void setSessionService(ClusterSessionService sessionService) {
219     this.sessionService = sessionService;
220   }
221
222 }