KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > state > cocoon > SessionScope


1 package org.sapia.soto.state.cocoon;
2
3 import org.apache.cocoon.environment.Request;
4 import org.apache.cocoon.environment.Session;
5
6 import org.sapia.soto.state.Scope;
7
8
9 /**
10  * Implements the <code>Scope</code> interface over a Cocoon <code>Session</code>.
11  *
12  * @author Yanick Duchesne
13  * <dl>
14  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public class SessionScope implements Scope {
20   private Request _req;
21   private Session _sess;
22
23   public SessionScope(Request req) {
24     _req = req;
25   }
26
27   /**
28    * @see org.sapia.soto.state.Scope#putVal(java.lang.Object, java.lang.Object)
29    */

30   public void putVal(Object JavaDoc key, Object JavaDoc value) {
31     checkSession();
32
33     if (_sess != null) {
34       _sess.setAttribute(key.toString(), value);
35     }
36   }
37
38   /**
39    * @see org.sapia.soto.state.Scope#getVal(java.lang.Object)
40    */

41   public Object JavaDoc getVal(Object JavaDoc key) {
42     checkSession();
43
44     if (_sess == null) {
45       return null;
46     }
47
48     return _sess.getAttribute(key.toString());
49   }
50
51   /**
52    * @return the <code>Session</code> held within this instance, or
53    * <code>null</code> if no session was created.
54    *
55    * @see #createSession()
56    */

57   public Session getSession() {
58     return _sess;
59   }
60
61   /**
62    * @return creates a <code>Session</code> and returns it, or returns
63    * the already existing session, if this applies.
64    */

65   public Session createSession() {
66     return _sess = _req.getSession(true);
67   }
68
69   private void checkSession() {
70     if (_sess == null) {
71       _sess = _req.getSession(true);
72     }
73   }
74 }
75
Popular Tags