KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > util > SessionMap


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.util;
14
15 import java.util.AbstractMap JavaDoc;
16 import java.util.AbstractSet JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import javax.servlet.http.HttpSession JavaDoc;
22
23
24 /**
25  * This class makes an <code>HttpSession</code> look like a <code>Map</code>. FIXME: Write a
26  * testcase for this class!
27  *
28  * @author Eric Galluzzo
29  */

30 public class SessionMap extends AbstractMap JavaDoc
31 {
32     protected HttpSession JavaDoc fieldSession;
33
34     public SessionMap(HttpSession JavaDoc inSession)
35     {
36         fieldSession = inSession;
37     }
38
39     /* (non-Javadoc)
40      * @see Map#isEmpty()
41      */

42     public boolean isEmpty()
43     {
44         return getSession().getAttributeNames().hasMoreElements();
45     }
46
47     /**
48      * DOCUMENT ME!
49      *
50      * @return
51      */

52     public HttpSession JavaDoc getSession()
53     {
54         return fieldSession;
55     }
56
57     /* (non-Javadoc)
58      * @see Map#containsKey(Object)
59      */

60     public boolean containsKey(Object JavaDoc key)
61     {
62         return (getSession().getAttribute((String JavaDoc) key) != null);
63     }
64
65     /* (non-Javadoc)
66      * @see Map#entrySet()
67      */

68     public Set JavaDoc entrySet()
69     {
70         return new EntrySet();
71     }
72
73     /* (non-Javadoc)
74      * @see Map#get(Object)
75      */

76     public Object JavaDoc get(Object JavaDoc key)
77     {
78         return getSession().getAttribute((String JavaDoc) key);
79     }
80
81     /* (non-Javadoc)
82      * @see Map#put(Object, Object)
83      */

84     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
85     {
86         Object JavaDoc oldValue = getSession().getAttribute((String JavaDoc) key);
87         getSession().setAttribute((String JavaDoc) key, value);
88
89         return oldValue;
90     }
91
92     /* (non-Javadoc)
93      * @see Map#remove(Object)
94      */

95     public Object JavaDoc remove(Object JavaDoc key)
96     {
97         Object JavaDoc oldValue = getSession().getAttribute((String JavaDoc) key);
98
99         if (oldValue != null)
100         {
101             getSession().removeAttribute((String JavaDoc) key);
102         }
103
104         return oldValue;
105     }
106
107     /* (non-Javadoc)
108      * @see Map#size()
109      */

110     public int size()
111     {
112         int size = 0;
113
114         for (Enumeration JavaDoc e = getSession().getAttributeNames(); e.hasMoreElements();)
115         {
116             e.nextElement();
117             size++;
118         }
119
120         return size;
121     }
122
123     /**
124      * This class implements an entry set as required by <code>{@link SessionMap#emptySet}</code>.
125      */

126     protected class EntrySet extends AbstractSet JavaDoc
127     {
128         public EntrySet()
129         {
130         }
131
132         /**
133          * DOCUMENT ME!
134          *
135          * @return
136          */

137         public boolean isEmpty()
138         {
139             return SessionMap.this.isEmpty();
140         }
141
142         /**
143          * DOCUMENT ME!
144          *
145          * @return
146          */

147         public Iterator JavaDoc iterator()
148         {
149             return new EntryIterator();
150         }
151
152         /* (non-Javadoc)
153          * @see Collection#remove(Object)
154          */

155         public boolean remove(Object JavaDoc o)
156         {
157             return (SessionMap.this.remove(o) != null);
158         }
159
160         /**
161          * DOCUMENT ME!
162          *
163          * @return
164          */

165         public int size()
166         {
167             return SessionMap.this.size();
168         }
169
170         protected class EntryIterator implements Iterator JavaDoc
171         {
172             protected Enumeration JavaDoc fieldEnumeration = getSession().getAttributeNames();
173             protected String JavaDoc fieldCurrentName;
174
175             /**
176              * DOCUMENT ME!
177              *
178              * @return
179              */

180             public boolean hasNext()
181             {
182                 return fieldEnumeration.hasMoreElements();
183             }
184
185             /**
186              * DOCUMENT ME!
187              *
188              * @return
189              */

190             public Object JavaDoc next()
191             {
192                 fieldCurrentName = (String JavaDoc) fieldEnumeration.nextElement();
193
194                 if (fieldCurrentName == null)
195                 {
196                     return null;
197                 }
198                 else
199                 {
200                     return new com.openedit.util.SimpleEntry(
201                         fieldCurrentName, getSession().getAttribute(fieldCurrentName));
202                 }
203             }
204
205             /**
206                          *
207                          */

208             public void remove()
209             {
210                 getSession().removeAttribute(fieldCurrentName);
211             }
212         }
213     }
214 }
215
Popular Tags