KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > transport > http > AxisHttpSession


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.transport.http;
18
19 import org.apache.axis.session.Session;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpSession JavaDoc;
23 import java.util.Enumeration JavaDoc;
24
25 /**
26  * An HTTP/Servlet implementation of Axis sessions.
27  *
28  * @author Glen Daniels (gdaniels@apache.org)
29  */

30 public class AxisHttpSession implements Session
31 {
32     public static final String JavaDoc AXIS_SESSION_MARKER = "axis.isAxisSession";
33     
34     private HttpSession JavaDoc rep;
35     private HttpServletRequest JavaDoc req;
36     
37     public AxisHttpSession(HttpServletRequest JavaDoc realRequest)
38     {
39         req = realRequest;
40     }
41     
42     public AxisHttpSession(HttpSession JavaDoc realSession)
43     {
44         if (realSession != null)
45             setRep(realSession);
46     }
47     
48     /** Get the internal HttpSession.
49      */

50     public HttpSession JavaDoc getRep()
51     {
52         ensureSession();
53         return rep;
54     }
55     
56     /** Set our internal HttpSession to the passed
57      * servlet HttpSession. Not sure if we'll really
58      * need this method...
59      */

60     private void setRep(HttpSession JavaDoc realSession)
61     {
62         rep = realSession;
63         rep.setAttribute(AXIS_SESSION_MARKER, Boolean.TRUE);
64     }
65     
66     /** Get a property from the session
67      *
68      * @param key the name of the property desired.
69      */

70     public Object JavaDoc get(String JavaDoc key)
71     {
72         ensureSession();
73         return rep.getAttribute(key);
74     }
75     
76     /** Set a property in the session
77      *
78      * @param key the name of the property to set.
79      * @param value the value of the property.
80      */

81     public void set(String JavaDoc key, Object JavaDoc value)
82     {
83         ensureSession();
84         rep.setAttribute(key, value);
85     }
86     
87     /** Remove a property from the session
88      *
89      * @param key the name of the property desired.
90      */

91     public void remove(String JavaDoc key)
92     {
93         ensureSession();
94         rep.removeAttribute(key);
95     }
96
97     /**
98      * Get an enumeration of the keys in this session
99      */

100     public Enumeration JavaDoc getKeys() {
101         ensureSession();
102         return rep.getAttributeNames();
103     }
104
105     /** Set the session's time-to-live.
106      *
107      * This is implementation-specific, but basically should be the #
108      * of seconds of inactivity which will cause the session to time
109      * out and invalidate. "inactivity" is implementation-specific.
110      */

111     public void setTimeout(int timeout)
112     {
113         ensureSession();
114         rep.setMaxInactiveInterval(timeout);
115     }
116
117     /**
118      * Return the sessions' time-to-live.
119      *
120      * @return the timeout value for this session.
121      */

122     public int getTimeout() {
123         ensureSession();
124         return rep.getMaxInactiveInterval();
125     }
126
127     /**
128      * "Touch" the session (mark it recently used)
129      */

130     public void touch() {
131         // ???
132
}
133
134     /**
135      * invalidate the session
136      */

137     public void invalidate() {
138         rep.invalidate();
139     }
140     
141     protected void ensureSession() {
142         if (rep == null) {
143             setRep(req.getSession());
144         }
145     }
146
147     /**
148      * Get an Object suitable for synchronizing the session. This method
149      * exists because different session implementations might provide
150      * different ways of getting at shared data. For a simple hashtable-
151      * based session, this would just be the hashtable, but for sessions
152      * which use database connections, etc. it might be an object wrapping
153      * a table ID or somesuch.
154      */

155     public Object JavaDoc getLockObject() {
156         ensureSession();
157         return rep;
158     }
159 }
160
Popular Tags