KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > j2ee > session > StateAdaptor


1 // ========================================================================
2
// $Id: StateAdaptor.java,v 1.4 2004/05/09 20:30:47 gregwilkins Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.j2ee.session;
17
18 //----------------------------------------
19

20 import java.rmi.RemoteException JavaDoc;
21 import java.util.Enumeration JavaDoc;
22
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.http.HttpSessionContext JavaDoc;
25
26 import org.jfox.ioc.logger.Logger;
27
28 //----------------------------------------
29
// this class is responsible for presenting a State container to a
30
// servlet as a HttpSession - since this interface is not an ideal one
31
// to use for the container interceptors. It constrains all the inputs
32
// to this API as specified...
33

34 // Since this is the front end of the Container, maybe it should be so
35
// called ? or maybe I should just call it [Http]Session?
36

37 // we should cache our id locally...
38

39 public class StateAdaptor
40   implements org.mortbay.jetty.servlet.SessionManager.Session
41 {
42   protected static final Logger _log=Logger.getLogger(StateAdaptor.class);
43   Manager _manager;
44   State _state=null;
45   boolean _new=true;
46
47   // we cache these for speed...
48
final String JavaDoc _id;
49
50   StateAdaptor(String JavaDoc id, Manager manager, int maxInactiveInterval, long lastAccessedTime)
51   {
52     _id=id;
53     _manager=manager;
54   }
55
56   void
57     setState(State state)
58   {
59     _state=state;
60   }
61
62   State
63     getState()
64   {
65     return _state;
66   }
67
68   // hmmmm...
69
// why does Greg call this?
70
// could it be compressed into a subsequent call ?
71
public boolean
72     isValid()
73   {
74     if (_state==null)
75       return false;
76
77     StateInterceptor si=(StateInterceptor)_state;
78      si.setManager(_manager);
79      si.setSession(this);
80
81     try
82     {
83       _state.getLastAccessedTime(); // this should cause an interceptor/the session to check
84
return true;
85     }
86     catch (IllegalStateException JavaDoc ignore)
87     {
88       return false;
89     }
90     catch (Exception JavaDoc e)
91     {
92       _log.error("problem contacting HttpSession", e);
93       return false;
94     }
95   }
96
97   // HttpSession API
98

99   public long
100     getCreationTime()
101     throws IllegalStateException JavaDoc
102   {
103     checkState();
104
105     try
106     {
107       return _state.getCreationTime();
108     }
109     catch (RemoteException JavaDoc e)
110     {
111       _log.error("could not get CreationTime", e);
112       throw new IllegalStateException JavaDoc("problem with distribution layer");
113     }
114   }
115
116   public String JavaDoc
117     getId()
118     throws IllegalStateException JavaDoc
119   {
120     checkState();
121
122     // locally cached and invariant
123
return _id;
124   }
125
126   public long
127     getLastAccessedTime()
128     throws IllegalStateException JavaDoc
129   {
130     checkState();
131
132     try
133     {
134       return _state.getLastAccessedTime();
135     }
136     catch (RemoteException JavaDoc e)
137     {
138       _log.error("could not get LastAccessedTime", e);
139       throw new IllegalStateException JavaDoc("problem with distribution layer");
140     }
141   }
142
143   // clarify with Tomcat, whether this is on a per Session or SessionManager basis...
144
public void
145     setMaxInactiveInterval(int interval)
146   {
147     checkState();
148
149     try
150     {
151       _state.setMaxInactiveInterval(interval);
152     }
153     catch (RemoteException JavaDoc e)
154     {
155       _log.error("could not set MaxInactiveInterval", e);
156     }
157   }
158
159   public int
160     getMaxInactiveInterval()
161   {
162     checkState();
163
164     try
165     {
166       return _state.getMaxInactiveInterval();
167     }
168     catch (RemoteException JavaDoc e)
169     {
170       // Can I throw an exception of some type here - instead of
171
// returning rubbish ? - TODO
172
_log.error("could not get MaxInactiveInterval", e);
173       return 0;
174     }
175   }
176
177   public Object JavaDoc
178     getAttribute(String JavaDoc name)
179     throws IllegalStateException JavaDoc
180   {
181     checkState();
182
183     try
184     {
185       return _state.getAttribute(name);
186     }
187     catch (RemoteException JavaDoc e)
188     {
189       _log.error("could not get Attribute", e);
190       throw new IllegalStateException JavaDoc("problem with distribution layer");
191     }
192   }
193
194   public Object JavaDoc
195     getValue(String JavaDoc name)
196     throws IllegalStateException JavaDoc
197   {
198     checkState();
199
200     try
201     {
202       return _state.getAttribute(name);
203     }
204     catch (RemoteException JavaDoc e)
205     {
206       _log.error("could not get Value", e);
207       throw new IllegalStateException JavaDoc("problem with distribution layer");
208     }
209   }
210
211   public Enumeration JavaDoc
212     getAttributeNames()
213     throws IllegalStateException JavaDoc
214   {
215     checkState();
216
217     try
218     {
219       return _state.getAttributeNameEnumeration();
220     }
221     catch (RemoteException JavaDoc e)
222     {
223       _log.error("could not get AttributeNames", e);
224       throw new IllegalStateException JavaDoc("problem with distribution layer");
225     }
226   }
227
228   public String JavaDoc[]
229     getValueNames()
230     throws IllegalStateException JavaDoc
231   {
232     checkState();
233
234     try
235     {
236       return _state.getAttributeNameStringArray();
237     }
238     catch (RemoteException JavaDoc e)
239     {
240       _log.error("could not get ValueNames", e);
241       throw new IllegalStateException JavaDoc("problem with distribution layer");
242     }
243   }
244
245   public void
246     setAttribute(String JavaDoc name, Object JavaDoc value)
247     throws IllegalStateException JavaDoc
248   {
249     checkState();
250
251     try
252     {
253       if (value==null)
254     _state.removeAttribute(name, false);
255       else
256       {
257     if (name==null)
258       throw new IllegalArgumentException JavaDoc("invalid attribute name: "+name);
259
260     _state.setAttribute(name, value, false);
261       }
262     }
263     catch (RemoteException JavaDoc e)
264     {
265       _log.error("could not set Attribute", e);
266       throw new IllegalStateException JavaDoc("problem with distribution layer");
267     }
268   }
269
270   public void
271     putValue(String JavaDoc name, Object JavaDoc value)
272     throws IllegalStateException JavaDoc
273   {
274     checkState();
275
276     if (name==null)
277       throw new IllegalArgumentException JavaDoc("invalid attribute name: "+name);
278
279     if (value==null)
280       throw new IllegalArgumentException JavaDoc("invalid attribute value: "+value);
281
282     try
283     {
284       _state.setAttribute(name, value, false);
285     }
286     catch (RemoteException JavaDoc e)
287     {
288       _log.error("could not put Value", e);
289       throw new IllegalStateException JavaDoc("problem with distribution layer");
290     }
291   }
292
293   public void
294     removeAttribute(String JavaDoc name)
295     throws IllegalStateException JavaDoc
296   {
297     checkState();
298
299     try
300     {
301       _state.removeAttribute(name, false);
302     }
303     catch (RemoteException JavaDoc e)
304     {
305       _log.error("could not remove Attribute", e);
306       throw new IllegalStateException JavaDoc("problem with distribution layer");
307     }
308   }
309
310   public void
311     removeValue(String JavaDoc name)
312     throws IllegalStateException JavaDoc
313   {
314     checkState();
315
316     try
317     {
318       _state.removeAttribute(name, false);
319     }
320     catch (RemoteException JavaDoc e)
321     {
322       _log.error("could not remove Value", e);
323       throw new IllegalStateException JavaDoc("problem with distribution layer");
324     }
325   }
326
327   public void
328     invalidate()
329     throws IllegalStateException JavaDoc
330   {
331     if (_log.isTraceEnabled()) _log.trace("user invalidated session: "+getId());
332     _manager.destroySession(this);
333   }
334
335   /**
336    *
337    * Returns <code>true</code> if the client does not yet know about the
338    * session or if the client chooses not to join the session. For
339    * example, if the server used only cookie-based sessions, and
340    * the client had disabled the use of cookies, then a session would
341    * be new on each request.
342    *
343    * @return <code>true</code> if the
344    * server has created a session,
345    * but the client has not yet joined
346    *
347    * @exception IllegalStateException if this method is called on an
348    * already invalidated session
349    *
350    */

351
352   public boolean
353     isNew()
354     throws IllegalStateException JavaDoc
355   {
356     return _new;
357   }
358
359   public ServletContext JavaDoc
360     getServletContext()
361   {
362     return _manager.getServletContext();
363   }
364
365   public HttpSessionContext JavaDoc
366     getSessionContext()
367   {
368     return _manager.getSessionContext();
369   }
370
371   // this one's for Greg...
372
public void
373     access()
374   {
375     long time=System.currentTimeMillis(); // we could get this from Manager - less accurate
376
setLastAccessedTime(time);
377
378     _new=false; // synchronise - TODO
379
}
380
381   public void
382     setLastAccessedTime(long time)
383     throws IllegalStateException JavaDoc
384   {
385     checkState();
386
387     try
388     {
389       _state.setLastAccessedTime(time);
390     }
391     catch (RemoteException JavaDoc e)
392     {
393       _log.error("could not set LastAccessedTime", e);
394       throw new IllegalStateException JavaDoc("problem with distribution layer");
395     }
396   }
397
398   protected void
399     checkState()
400     throws IllegalStateException JavaDoc
401   {
402     if (_state==null)
403       throw new IllegalStateException JavaDoc("invalid session");
404
405     // this is a hack to get new interceptor stack to work... - TODO
406
StateInterceptor si=(StateInterceptor)_state;
407      si.setManager(_manager);
408      si.setSession(this);
409   }
410
411   public String JavaDoc
412     toString()
413   {
414     return "<"+getClass()+"->"+_state+">";
415   }
416
417   // I'm still not convinced that this is the correct place for this
418
// method- but I can;t think of a better way - maybe in the next
419
// iteration...
420

421 // MigrationInterceptor _mi=null;
422
//
423
// public void
424
// registerMigrationListener(MigrationInterceptor mi)
425
// {
426
// _mi=mi;
427
// }
428
//
429
public void
430      migrate()
431    {
432      // if (_mi!=null)
433
// _mi.migrate(); // yeugh - TODO
434
}
435 }
436
Popular Tags