KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > util > FakeHttpSession


1 /*
2  * Copyright 2005 Joe Walker
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 package org.directwebremoting.util;
17
18 import java.util.Collections JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.http.HttpSession JavaDoc;
25
26 /**
27  * For the benefit of anyone that wants to create a fake HttpSession
28  * that doesn't do anything other than not be null.
29  * @author Joe Walker [joe at getahead dot ltd dot uk]
30  */

31 public class FakeHttpSession implements HttpSession JavaDoc
32 {
33     /**
34      * Setup the creation time
35      */

36     public FakeHttpSession()
37     {
38         creationTime = System.currentTimeMillis();
39     }
40
41     /**
42      * Setup the creation time
43      * @param id The new session id
44      */

45     public FakeHttpSession(String JavaDoc id)
46     {
47         this.id = id;
48         creationTime = System.currentTimeMillis();
49     }
50
51     /* (non-Javadoc)
52      * @see javax.servlet.http.HttpSession#getCreationTime()
53      */

54     public long getCreationTime()
55     {
56         return creationTime;
57     }
58
59     /* (non-Javadoc)
60      * @see javax.servlet.http.HttpSession#getId()
61      */

62     public String JavaDoc getId()
63     {
64         if (id == null)
65         {
66             log.warn("Inventing data in FakeHttpSession.getId() to remain plausible.");
67             id = "fake";
68         }
69
70         return id;
71     }
72
73     /* (non-Javadoc)
74      * @see javax.servlet.http.HttpSession#getLastAccessedTime()
75      */

76     public long getLastAccessedTime()
77     {
78         return creationTime;
79     }
80
81     /* (non-Javadoc)
82      * @see javax.servlet.http.HttpSession#getServletContext()
83      */

84     public ServletContext JavaDoc getServletContext()
85     {
86         return null;
87     }
88
89     /* (non-Javadoc)
90      * @see javax.servlet.http.HttpSession#setMaxInactiveInterval(int)
91      */

92     public void setMaxInactiveInterval(int maxInactiveInterval)
93     {
94         this.maxInactiveInterval = maxInactiveInterval;
95     }
96
97     /* (non-Javadoc)
98      * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
99      */

100     public int getMaxInactiveInterval()
101     {
102         return maxInactiveInterval;
103     }
104
105     /**
106      * @see javax.servlet.http.HttpSession#getSessionContext()
107      * @deprecated
108      */

109     public javax.servlet.http.HttpSessionContext JavaDoc getSessionContext()
110     {
111         return null;
112     }
113
114     /* (non-Javadoc)
115      * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String)
116      */

117     public Object JavaDoc getAttribute(String JavaDoc name)
118     {
119         return attributes.get(name);
120     }
121
122     /* (non-Javadoc)
123      * @see javax.servlet.http.HttpSession#getValue(java.lang.String)
124      */

125     public Object JavaDoc getValue(String JavaDoc name)
126     {
127         return attributes.get(name);
128     }
129
130     /* (non-Javadoc)
131      * @see javax.servlet.http.HttpSession#getAttributeNames()
132      */

133     public Enumeration JavaDoc getAttributeNames()
134     {
135         return Collections.enumeration(attributes.keySet());
136     }
137
138     /* (non-Javadoc)
139      * @see javax.servlet.http.HttpSession#getValueNames()
140      */

141     public String JavaDoc[] getValueNames()
142     {
143         return (String JavaDoc[]) attributes.keySet().toArray(new String JavaDoc[attributes.keySet().size()]);
144     }
145
146     /* (non-Javadoc)
147      * @see javax.servlet.http.HttpSession#setAttribute(java.lang.String, java.lang.Object)
148      */

149     public void setAttribute(String JavaDoc name, Object JavaDoc value)
150     {
151         attributes.put(name, value);
152     }
153
154     /* (non-Javadoc)
155      * @see javax.servlet.http.HttpSession#putValue(java.lang.String, java.lang.Object)
156      */

157     public void putValue(String JavaDoc name, Object JavaDoc value)
158     {
159         attributes.put(name, value);
160     }
161
162     /* (non-Javadoc)
163      * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
164      */

165     public void removeAttribute(String JavaDoc name)
166     {
167         attributes.remove(name);
168     }
169
170     /* (non-Javadoc)
171      * @see javax.servlet.http.HttpSession#removeValue(java.lang.String)
172      */

173     public void removeValue(String JavaDoc name)
174     {
175         attributes.remove(name);
176     }
177
178     /* (non-Javadoc)
179      * @see javax.servlet.http.HttpSession#invalidate()
180      */

181     public void invalidate()
182     {
183     }
184
185     /* (non-Javadoc)
186      * @see javax.servlet.http.HttpSession#isNew()
187      */

188     public boolean isNew()
189     {
190         return true;
191     }
192
193     /**
194      * The session id
195      */

196     private String JavaDoc id = null;
197
198     /**
199      * The list of attributes
200      */

201     private Map JavaDoc attributes = new HashMap JavaDoc();
202
203     /**
204      * When were we created
205      */

206     private long creationTime;
207
208     /**
209      * How long before we timeout?
210      */

211     private int maxInactiveInterval = 30 * 60 * 1000;
212
213     /**
214      * The log stream
215      */

216     private static final Logger log = Logger.getLogger(FakeHttpSession.class);
217 }
218
Popular Tags