KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > plankton > data > HttpSessionStateMap


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: HttpSessionStateMap.java,v 1.7 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.plankton.data;
21
22 import java.io.*;
23 import java.util.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27 /**
28  * The implementation provides a StateMap bridge to a HttpSession
29  * object. By this, we mean that this class allows you to treat an
30  * HttpSession as a StateMap. Unlike the HttpSession, this class
31  * will handle null keys, values.
32  */

33 public class HttpSessionStateMap implements StateMap {
34
35     //private vars
36
protected HttpSession session = null;
37     private String JavaDoc NULL = "~Null~";
38     
39     /**
40      * Public constructor.
41      *
42      * @param isession the underlying servlet session structure
43      */

44     public HttpSessionStateMap(HttpSession isession) {
45         session = isession;
46     }
47     
48     /**
49      * set a property in this StateMap
50      *
51      * @param key the key object
52      * @param val the value object
53      */

54     public void putState(Object JavaDoc key, Object JavaDoc val) {
55         if (key==null) key = NULL;
56         if (val==null) val = NULL;
57         session.setAttribute(key.toString(),val);
58     }
59     
60     /**
61      * get a property in this StateMap
62      *
63      * @param key the key object
64      * @return the value for the given key
65      */

66     public Object JavaDoc getState(Object JavaDoc key) {
67         if (key==null) key = NULL;
68         Object JavaDoc val = session.getAttribute(key.toString());
69         return ((val==null || val.equals(NULL)) ? null : val);
70     }
71     
72     /**
73      * remove a property in this StateMap. This function was expanded in csc_082103_1 to
74      * support the notion of wildcarding, allowing you to remove multiple keys in one fell
75      * swoop. Basically, if the key is a String, which ends with an '*', then any keys that
76      * start with that string will be removed (and in this case, the method returns a Map of
77      * key/val pairs that got removed, rather than a single object that got removed). This
78      * approach is not quite as flexible as using regular expressions, but that would make us
79      * dependent on jdk1.4 (so we won't go there for now). Note that this class backs the
80      * ObjectRepository data structures, so this functionality applies there as well.
81      *
82      * @param key the key object
83      * @return the object which was removed
84      */

85     public Object JavaDoc removeState(Object JavaDoc key) {
86 //csc_082103_1_start
87
try { //csc_011904_2
88
//remove all keys that match
89
if (key!=null && (key instanceof String JavaDoc) && ((String JavaDoc) key).endsWith("*")) {
90                 Map removed = new HashMap();
91                 String JavaDoc keystr = (String JavaDoc) key;
92                 String JavaDoc targetstr = keystr.substring(0,keystr.length()-1);
93                 List keys = getStateKeys();
94                 Iterator it = keys.iterator();
95                 while (it.hasNext()) {
96                     Object JavaDoc okey = it.next();
97                     if ((okey instanceof String JavaDoc) && ((String JavaDoc) okey).startsWith(targetstr)) {
98                         Object JavaDoc oval = session.getAttribute(okey.toString());
99                         removed.put(okey, oval);
100                         session.removeAttribute(okey.toString());
101                     }
102                 }
103                 return (removed.size()>0 ? removed : null);
104             } else {
105     //csc_082103_1_end
106
if (key==null) key = NULL;
107                 Object JavaDoc val = session.getAttribute(key.toString());
108                 session.removeAttribute(key.toString());
109         //csc_040203.1 return (val.equals(NULL) ? null : val);
110
return ((val==null || val.equals(NULL)) ? null : val); //csc_040203.1
111
}
112         
113         //this occasionally happens if the session expires as you are in the process of
114
//taking something out of it...in such a case, just catch the error and return null
115
} catch (NullPointerException JavaDoc e) { //csc_011904_2 - added
116
return null;
117         }
118     }
119     
120     /**
121      * get a List of the keys for this StateMap (implementation
122      * is an ArrayList)
123      *
124      * @return a List the keys for this StateMap
125      */

126     public List getStateKeys() {
127         Enumeration e = session.getAttributeNames();
128         List list = new ArrayList();
129         while (e.hasMoreElements()) {
130             list.add(e.nextElement());
131         }
132         return list;
133     }
134     
135     /**
136      * get a copy of the underlying Map (in this case, we
137      * map all the attributes of the HttpSession structure
138      * into a Map and return that)
139      *
140      * @return a copy of the underlying state Map
141      */

142     public Map getStateValues() {
143         Map map = new HashMap();
144         List keys = getStateKeys();
145         Iterator it = keys.iterator();
146         while (it.hasNext()) {
147             Object JavaDoc key = it.next();
148             Object JavaDoc val = session.getAttribute(key.toString());
149             map.put(key,val);
150         }
151         return map;
152     }
153
154     //csc_052803_2 - added
155
/**
156      * clear all state information
157      */

158     public void clearState() {
159         List keys = getStateKeys();
160         Iterator it = keys.iterator();
161         while (it.hasNext()) {
162             Object JavaDoc key = it.next();
163             removeState(key);
164         }
165     }
166
167     /**
168      * get a reference to the underlying HttpSession
169      *
170      * @return a reference to the underlying HttpSession
171      */

172     public HttpSession getSession() {
173         return session;
174     }
175 }
176
Popular Tags