KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 package com.lutris.appserver.server.sessionContainerAdapter;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.Date JavaDoc;
6
7 import javax.servlet.http.HttpSession JavaDoc;
8 import javax.servlet.http.HttpSessionActivationListener JavaDoc;
9 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
10 import javax.servlet.http.HttpSessionEvent JavaDoc;
11 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
12
13
14 import com.lutris.appserver.server.session.SessionData;
15 import com.lutris.appserver.server.session.SessionException;
16 import com.lutris.appserver.server.session.SessionManager;
17 import com.lutris.appserver.server.user.User;
18 /**
19  * <p>Description: </p>
20  * Session object, used to keep the session data
21  * @version 1.0
22  */

23 public class ContainerAdapterSession
24     implements com.lutris.appserver.server.session.Session,HttpSessionActivationListener JavaDoc,HttpSessionBindingListener JavaDoc, Serializable JavaDoc {
25
26  /**
27    * user object
28    */

29   private User user;
30   /**
31    * Session dat, must be serializable since the servlet container really manages
32    * the sessions, and Tomcat's standard manager implementation needs serializable objects
33    */

34   private SerializableSessionData sessionData;
35   /**
36    * SessionManager that is used to create this Session object
37    */

38   private SessionManager sessionManager;
39  
40   private HttpSession JavaDoc httpSession;
41  
42   /**
43    * Default constructor
44    */

45   public ContainerAdapterSession () {
46   }
47  
48   /**
49    *
50    * @param sessionManager SessionManager that is used to create this Session object
51    * @param sessionKey The identifiction of the session
52    */

53   public ContainerAdapterSession (ContainerAdapterSessionManager sessionManager,
54       HttpSession JavaDoc httpSession) {
55  
56     this.sessionManager = sessionManager;
57     this.httpSession = httpSession;
58     sessionData = new SerializableSessionData();
59     user = null;
60   }
61
62   /**
63    * @return the User object;
64    */

65   public User getUser () {
66     return user;
67   }
68
69   /**
70    * Sets the user
71    * @param user - the user object of the session
72    * @throws SessionException
73    */

74   public void setUser (User user) throws com.lutris.appserver.server.session.SessionException {
75     this.user = user;
76   }
77
78   /**
79    * clears the user object
80    * @throws SessionException
81    */

82   public void clearUser () throws com.lutris.appserver.server.session.SessionException {
83     user = null;
84   }
85
86   /**
87    *
88    * @return the session identification
89    */

90   public String JavaDoc getSessionKey () {
91    if(httpSession!=null)
92     return httpSession.getId();
93    else
94     return null;
95   }
96
97    /**
98    *
99    * @return the session creation time
100    */

101   public long getTimeCreated()
102    {
103     if(httpSession!=null)
104      return httpSession.getCreationTime();
105     else
106      return -1;
107    }
108   /**
109    * Obtain the time of last use for this object. The time is in
110    * milliseconds since Midnight, Jan 1, 1970 (epoch).
111    *
112    * @return The time of last use since epoch for this object.
113    */

114   
115   public long getTimeLastUsed() {
116     if(httpSession!=null)
117       return httpSession.getLastAccessedTime();
118     else
119       return -1;
120   }
121    
122   protected void setTimeCreated(long cTime)
123    {
124    }
125      
126   
127   /**
128    * Obtain the maximum idle time for this object. Zero (or negative)
129    * indicates that sessions may be idle indefinetly.
130    *
131    * @return The maximum number of milliseconds this session
132    * may be idle.
133    */

134   public long getMaxIdleTime() {
135     if(httpSession!=null)
136       return httpSession.getMaxInactiveInterval();
137     else
138       return -1;
139   }
140
141   /**
142    * Set the maximum idle time for this object. Set this to zero
143    * (or negative) to disable idle checking.
144    *
145    * @param maxIdleTime The maximum number of milliseconds this
146    * session may be idle, or zero (or negative) to allow sessions to be idle
147    * indefinetly.
148    */

149   public void setMaxIdleTime(int maxIdleTime) {
150     if(httpSession!=null)
151       httpSession.setMaxInactiveInterval(maxIdleTime);
152   }
153   
154   
155   /**
156    *
157    * @return the <code> SessionManager </code>used to create this session
158    */

159   public SessionManager getSessionManager () {
160     return sessionManager;
161   }
162
163  public void setSessionManager (SessionManager sessionManager) {
164    this.sessionManager=sessionManager;
165   }
166   /**
167    *
168    * @return the <code> SessionData </code>
169    */

170   public SessionData getSessionData () {
171     return sessionData;
172   }
173
174   /**
175    * puts the <code> SessionData </code> into this session
176    * @param sessionData
177    */

178   public void setSessionData (SessionData sessionData) {
179     this.sessionData = (SerializableSessionData)sessionData;
180   }
181
182  
183   public boolean isNew () {
184     if(httpSession!=null)
185      return (httpSession.getCreationTime() == httpSession.getLastAccessedTime());
186     else
187      return true;
188   }
189
190   public String JavaDoc toString () {
191   
192     StringBuffer JavaDoc result=new StringBuffer JavaDoc();
193     Date JavaDoc ct = new Date JavaDoc(getTimeCreated());
194     result.append("CreationTime:");
195     result.append(" ");
196     result.append(ct.toString());
197   
198    if(getUser()!=null)
199    {
200     result.append("\n");
201     result.append("User:");
202     result.append(" ");
203     result.append(getUser().getName());
204     
205    }
206    String JavaDoc data=getSessionData().toString();
207    if(data!=null)
208    {
209     result.append("\n");
210     result.append("SessionData:");
211     result.append(" ");
212     result.append(data);
213    }
214     return result.toString();
215  
216   }
217  
218   public HttpSession JavaDoc getHttpSession () {
219      return httpSession;
220   }
221   
222   public void valueBound(HttpSessionBindingEvent JavaDoc event)
223   {
224   }
225   
226   public void valueUnbound(HttpSessionBindingEvent JavaDoc event)
227   {
228     if("session".equals(event.getName()))
229      {
230       sessionManager = null;
231       httpSession = null;
232       user = null;
233       sessionData = null;
234      }
235   }
236  
237    public void sessionWillPassivate(HttpSessionEvent JavaDoc event)
238    {
239     sessionManager = null;
240     httpSession = null;
241     }
242
243    public void sessionDidActivate(HttpSessionEvent JavaDoc event)
244    {
245     httpSession = event.getSession();
246    }
247    
248 }
249
Popular Tags