KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > sessionContainerAdapter > ContainerAdapterSessionManager


1
2 package com.lutris.appserver.server.sessionContainerAdapter;
3
4 import java.util.Date JavaDoc;
5 import java.util.Enumeration JavaDoc;
6
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpSession JavaDoc;
9
10 import com.lutris.appserver.server.StandardApplication;
11 import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
12 import com.lutris.appserver.server.session.Session;
13 import com.lutris.appserver.server.session.SessionException;
14 import com.lutris.appserver.server.user.User;
15
16
17 /**
18  * <p>Description: </p>
19  * Simple session manager to be used with servlet container capable of managing their sessions.
20  * Uses HttpSession to keep the session data.
21  * The sessions are completely managed by the session container.
22  * To use this class as a session manager, enter it as a value of the
23  * SessionManager.Class in the config file of the application - e.g.
24  *
25  * SessionManager.Class = com.lutris.appserver.server.sessionContainerAdapter.ContainerAdapterSessionManager
26  *
27  * @version 1.0
28  */

29 public class ContainerAdapterSessionManager
30     implements com.lutris.appserver.server.session.SessionManager, java.io.Serializable JavaDoc {
31   /**
32    * Indicates url encoding status. Assumes that
33    * comms.response.writeHTML(HTMLDocument doc) is used to commit the
34    * response to the client. The default is to Auto.<br>
35    * <code>Never</code> indicates urls are never encoded with session keys.
36    * This indicates that session cookies have to be used or no session state
37    * can be maintained.<br>
38    * <code>Always</code> indicates that urls are always encoded with session
39    * keys. Session cookies are never used.<br>
40    * <code>Auto</code> indicates that session cookies will be if available.
41    * If not, urls will automatically be encoded.<br>
42    * @see #getEncodeUrlState
43    */

44   protected static String JavaDoc defaultEncodeUrlState = "Auto";
45   /**
46    * The name of the config variable for the url encoding state
47    */

48   public static final String JavaDoc CFG_ENCODE_URL_STATE = "SessionEncodeUrlState";
49   /**
50    * The url encoding state. Either <code>Never</code>
51    * <code>Always</code> <code>Auto</code>.
52    */

53   protected String JavaDoc encodeUrlState;
54   /**
55    * Name of the session object in the HttpSession
56    */

57   protected String JavaDoc SESSION = "session";
58
59  /**
60    * Default maximum session idle time, in seconds. A derived
61    * <CODE>SessionManager</CODE> may override this value to use the default
62    * expiration logic or override <CODE>isSessionExpired</CODE>
63    * to define custom expiration logic.
64    * A value less-than or equal to zero disables idle checking.
65    * Default value is 30 minutes.
66    *
67    * @see #getMaxSessionIdleTime
68    */

69    protected static int defaultMaxSessionIdleTime = 30*60;
70  /**
71    * Maximum session idle time, in seconds.
72    * A value less-than or equal to zero disables idle checking.
73    *
74    * @see #getMaxSessionIdleTime
75    */

76
77    protected int maxSessionIdleTime;
78   // protected boolean isMemoryPersistence=false;
79
/**
80    *
81    * @param application application that uses this session manager
82    * @param config the application config object
83    * @param logger logger, ignored, since some common loggers are not serilizable
84    * @throws SessionException
85    */

86   public ContainerAdapterSessionManager (com.lutris.appserver.server.Application application,
87       com.lutris.util.Config config, com.lutris.logging.LogChannel logger) throws com.lutris.appserver.server.session.SessionException
88   {
89
90     /**/
91     try {
92       // Url encoding state
93
if (config.containsKey(CFG_ENCODE_URL_STATE)) {
94         encodeUrlState = config.getString(CFG_ENCODE_URL_STATE);
95       }
96       else if (config.containsKey("EncodeUrlState")) {
97         // backwards compatability
98
encodeUrlState = config.getString("EncodeUrlState");
99       }
100       else {
101         encodeUrlState = defaultEncodeUrlState;
102       }
103     if (config.containsKey("MaxIdleTime")) {
104       // backwards compatability
105
maxSessionIdleTime = config.getInt("MaxIdleTime")*60;
106     }
107     else {
108       maxSessionIdleTime = defaultMaxSessionIdleTime;
109     }
110   // if (config.containsKey("MemoryPersistence")) {
111
// backwards compatability
112
// isMemoryPersistence = config.getBoolean("MemoryPersistence");
113
// }
114

115     } catch (com.lutris.util.ConfigException e) {
116       throw new com.lutris.appserver.server.session.SessionException(e);
117     }
118      ((StandardApplication)application).setCookieForNewSession(false);
119   }
120
121   /**
122    * Creates a new session
123    * @return Seession
124    * @throws SessionException
125    */

126   public Session JavaDoc createSession () throws com.lutris.appserver.server.session.SessionException {
127     return new ContainerAdapterSession(this, null);
128   }
129  
130   /**
131    * Not implemented - backward compatibility with the API specification
132    * @param ipport
133    * @return
134    * @throws SessionException
135    */

136   public Session JavaDoc createSession (String JavaDoc ipport) throws com.lutris.appserver.server.session.SessionException {
137
138     /**No need to implement this method - used only for Director*/
139     throw new java.lang.UnsupportedOperationException JavaDoc("Method createSession() not implemented.");
140   }
141
142   /**
143    * Extracts the SessionId from the httpSession and creates a newSession object with that Id
144    * @param comms HttpPresentationComms that containes the HttpServletRequest
145    * @return new Session
146    * @throws SessionException
147    */

148   public Session JavaDoc createSession (HttpPresentationComms comms) throws com.lutris.appserver.server.session.SessionException {
149     
150     HttpServletRequest JavaDoc servletRequest = comms.request.getHttpServletRequest();
151     HttpSession JavaDoc httpSession = servletRequest.getSession();
152     httpSession.setMaxInactiveInterval(maxSessionIdleTime);
153     comms.session = new ContainerAdapterSession(this, httpSession);
154     httpSession.setAttribute(SESSION, comms.session);
155     return comms.session;
156   }
157
158   /**
159    * Not implemented, since this session manager doesn't really control the sessions -
160    * they are managed by the servlet container
161    * @param parm1
162    * @throws SessionException
163    */

164   public void deleteSession (Session JavaDoc parm1) throws com.lutris.appserver.server.session.SessionException {
165
166     /**Not implemented*/
167     throw new java.lang.UnsupportedOperationException JavaDoc("Method deleteSession() not implemented.");
168   }
169
170   /**
171    * Not implemented, since this session manager doesn't really control the sessions -
172    * they are managed by the servlet container
173    * @param parm1
174    * @throws SessionException
175    */

176   public void deleteSession (String JavaDoc parm1) throws com.lutris.appserver.server.session.SessionException {
177
178     /**Not implementedd*/
179     throw new java.lang.UnsupportedOperationException JavaDoc("Method deleteSession() not implemented.");
180   }
181
182   /**
183    *
184    * @param sessionId
185    * @return always true
186    * @throws SessionException
187    */

188   public boolean sessionExists (String JavaDoc sessionId) throws com.lutris.appserver.server.session.SessionException {
189     return true;
190   }
191
192   /**
193    * Not implemented, since the sessions are kept at the container side
194    * @param sessionId
195    * @return
196    * @throws SessionException
197    */

198   public Session JavaDoc getSession (String JavaDoc sessionId) throws com.lutris.appserver.server.session.SessionException {
199     //System.out.println("Req. sess. " + sessionId);
200
throw new java.lang.UnsupportedOperationException JavaDoc("Method getSession (String sessionId) not implemented.");
201   }
202   /**
203    * returns the session object contained in the HttpServletRequest
204    * @param sessionId - ignored, kept for the backward compatibility
205    * @param servletRequest HttpServletRequest from which the session is extracted
206    * @return the Session object
207    * @throws SessionException
208    */

209   public Session JavaDoc getSession (String JavaDoc sessionId, HttpServletRequest JavaDoc servletRequest) throws com.lutris.appserver.server.session.SessionException {
210
211     HttpSession JavaDoc httpSession = servletRequest.getSession();
212     
213     if (httpSession != null) {
214         Session JavaDoc session = null;
215          
216         try{
217             session= (ContainerAdapterSession)httpSession.getAttribute("session");
218          }catch(IllegalStateException JavaDoc ise){
219             return null;
220          }
221         
222          if(session!=null)
223           {
224              if(session.getSessionManager ()==null)
225               {
226                ((ContainerAdapterSession)session).setSessionManager (this);
227               }
228              
229             return session;
230           }
231          else
232           {
233             return null;
234           }
235       }
236       else {
237         return null;
238       }
239   }
240
241   /**
242    * Returns teh session object corresponding to the HttpPresentationComms
243    * @param sessionId ignored, kept for the backward compatibility
244    * @param comms HttpPresentationComms object that contains HttpServletRequest from which the session is extracted
245    * @return the Session object
246    * @throws SessionException
247    */

248   public Session JavaDoc getSession (String JavaDoc sessionId, HttpPresentationComms comms) throws com.lutris.appserver.server.session.SessionException {
249     HttpServletRequest JavaDoc servletRequest = comms.request.getHttpServletRequest();
250     HttpSession JavaDoc httpSession = servletRequest.getSession();
251    
252     if (httpSession != null) {
253       
254         Session JavaDoc session = null;
255         
256       try{
257         session= (ContainerAdapterSession)httpSession.getAttribute("session");
258         }catch(IllegalStateException JavaDoc ise){
259             return null;
260         }
261         if(session!=null)
262          {
263             if(session.getSessionManager ()==null)
264              {
265               ((ContainerAdapterSession)session).setSessionManager (this);
266              }
267            
268            return session;
269          }
270         else
271          {
272            return null;
273          }
274     }
275     else {
276       return null;
277     }
278 }
279
280   /**
281    * Returns teh session object corresponding to the HttpPresentationComms
282    * @param parm1 ignored
283    * @param sessionId ignored
284    * @param comms HttpPresentationComms object that contains HttpServletRequest from which the session is extracted
285    * @return the Session object
286    * @throws SessionException
287    */

288   public Session JavaDoc getSession (Thread JavaDoc parm1, String JavaDoc sessionId, HttpPresentationComms comms) throws com.lutris.appserver.server.session.SessionException {
289     return getSession(sessionId, comms);
290   }
291
292   /**
293    * Not implemented, needed for the compatibility with the API specification
294    * @param parm1
295    * @param sessionId
296    * @return
297    * @throws SessionException
298    */

299   public Session JavaDoc getSession (Thread JavaDoc parm1, String JavaDoc sessionId) throws com.lutris.appserver.server.session.SessionException {
300
301     /**Not implemented*/
302     throw new java.lang.UnsupportedOperationException JavaDoc("Method getSessionKeys() not implemented.");
303   }
304
305   /**
306    * Not implemented, this information is kept at the servlet container side
307    * @param parm1
308    * @return
309    * @throws SessionException
310    */

311   public Enumeration JavaDoc getSessionKeys (User parm1) throws com.lutris.appserver.server.session.SessionException {
312
313     /**Not implemented*/
314     throw new java.lang.UnsupportedOperationException JavaDoc("Method getSessionKeys() not implemented.");
315   }
316
317   /**
318    * Not implemented, since this session manager doesn't really control the sessions -
319    * they are managed by the servlet container
320    * @param parm1
321    * @param parm2
322    * @throws SessionException
323    */

324   public void passivateSession (Thread JavaDoc parm1, String JavaDoc parm2) throws com.lutris.appserver.server.session.SessionException {
325     //throw new java.lang.UnsupportedOperationException("Method passivateSession() not implemented.");
326
return;
327   }
328
329   /**
330    * Not implemented, since this session manager doesn't really control the sessions -
331    * they are managed by the servlet container
332    * @return
333    * @throws SessionException
334    */

335   public int activeSessionCount () throws com.lutris.appserver.server.session.SessionException {
336
337     /**Not implemented*/
338     throw new java.lang.UnsupportedOperationException JavaDoc("Method activeSessionCount() not implemented.");
339   }
340
341   /**
342    * Not implemented, this information is kept at the servlet container side
343    * @return
344    */

345   public int maxSessionCount () {
346
347     /**Not implemented*/
348     throw new java.lang.UnsupportedOperationException JavaDoc("Method maxSessionCount() not implemented.");
349   }
350
351   /**
352    * Not implemented
353    * @return
354    */

355   public Date JavaDoc maxSessionCountDate () {
356
357     /**Not implemented*/
358     throw new java.lang.UnsupportedOperationException JavaDoc("Method maxSessionCountDate() not implemented.");
359   }
360
361   /**
362    * Not implemented, since this session manager doesn't really control the sessions -
363    * they are managed by the servlet container
364    * @throws SessionException
365    */

366   public void resetMaxSessionCount () throws com.lutris.appserver.server.session.SessionException {
367
368     /**Not implemented*/
369     throw new java.lang.UnsupportedOperationException JavaDoc("Method resetMaxSessionCount() not implemented.");
370   }
371
372   /**
373    * Not implemented, this information is kept at the servlet container side
374    * @return
375    * @throws SessionException
376    */

377   public Enumeration JavaDoc getSessionKeys () throws com.lutris.appserver.server.session.SessionException {
378
379     /**Not implemented*/
380     throw new java.lang.UnsupportedOperationException JavaDoc("Method getSessionKeys() not implemented.");
381   }
382
383   /**
384    * NOP
385    */

386   public void shutdown () {}
387
388   /**
389    *
390    * @return the value indicating the url encoding status
391    */

392   public String JavaDoc getEncodeUrlState () {
393     return encodeUrlState;
394   }
395 }
396
397
398
399
Popular Tags