KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > controller > session > SimplePersistentSession


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64 package com.jcorporate.expresso.core.controller.session;
65
66 import java.util.Enumeration JavaDoc;
67 import java.util.HashMap JavaDoc;
68 import java.util.Hashtable JavaDoc;
69 import java.util.Map JavaDoc;
70
71
72 /**
73  * This is an implementation of PersistentSession that is useful for command
74  * line usage. The code containing the controller should hand the controller
75  * an instantiated SimplePersistentSession so that the state handlers can use
76  * ControllerRequest.getSession() and simply use it without any cares in the
77  * world.
78  * <p>Example Usage: <br/>
79  * <code>
80  * <pre>
81  * SimplePersistentSession session = new SimplePersistentSession();
82  * Controller con = ConfigManager.getControllerFactory().getController(
83  * controllerName);
84  * ControllerRequest params = new ControllerRequest();
85  * params.setUid(3);
86  * params.setSession(mySession);
87  * <pre>
88  * </code>
89  * </p>
90  *
91  * @author Michael Nash
92  * @see com.jcorporate.expresso.core.controller.session.PersistentSession
93  * @since Expresso 4.0
94  */

95 public class SimplePersistentSession implements PersistentSession {
96     /**
97      * A storage place for attributes
98      */

99     private Hashtable JavaDoc attributes = new Hashtable JavaDoc();
100
101     /**
102      * A Storage place for persistent attributes
103      */

104     private Hashtable JavaDoc persistentAttributes = new Hashtable JavaDoc();
105
106     /**
107      * Creates new SimplePersistentSession
108      */

109     public SimplePersistentSession() {
110     }
111
112     /**
113      * Sets an attribute that is valid for the duration of the request.
114      *
115      * @param attribName The name of the object you wish to set.
116      * @param attribValue the object you want to set.
117      */

118     public void setAttribute(String JavaDoc attribName, Object JavaDoc attribValue) {
119         if (attribValue == null) {
120             attributes.remove(attribName);
121         } else {
122             attributes.put(attribName, attribValue);
123         }
124     }
125
126     /**
127      * Retrieves the object from the request context.
128      *
129      * @param attribName the name of the object to retrieve
130      * @return the object or null if it doesn't exist.
131      */

132     public Object JavaDoc getAttribute(String JavaDoc attribName) {
133         return attributes.get(attribName);
134     }
135
136     /**
137      * Retrieves all attribute names in the request context.
138      *
139      * @return java.util.Enumeration
140      */

141     public Enumeration JavaDoc getAttributeNames() {
142         return attributes.keys();
143     }
144
145     /**
146      * Does nothing. In a command line environment, cookies don't make
147      * sense.
148      *
149      * @param attribName the name of the attribute to
150      * @param attribValue the value of the attribute to set.
151      */

152     public void setClientAttribute(String JavaDoc attribName, String JavaDoc attribValue) {
153     }
154
155     /**
156      * Retrieves a value of a cookie set on the client's system. It also
157      * decrypts the value with the password you set up in your <code>expresso-config.xml</code>
158      * file.
159      *
160      * @param attribName the name of the cookie to retrieve
161      * @return java.lang.String the value of the cookie or null if the cookie doesn't exist.
162      */

163     public String JavaDoc getClientAttribute(String JavaDoc attribName) {
164         return null;
165     }
166
167     /**
168      * Retrieves the persistent attributes table. Useful if you wish to actually
169      * persisten these attributes across a command line 'session'
170      *
171      * @return java.util.HashMap
172      * @since Expresso 5.1
173      */

174     public Map JavaDoc getPersistentAttributes() {
175         return new HashMap JavaDoc(persistentAttributes);
176     }
177
178     /**
179      * Sets the persistent attributes that may have been saved across instances
180      * of command line 'session'
181      *
182      * @param newAttributes Map of attributes to set for the session.
183      */

184     public void setPersistentAttributes(Map JavaDoc newAttributes) {
185         persistentAttributes = new Hashtable JavaDoc(newAttributes);
186     }
187
188     /**
189      * Retrieves all attribute names from the session context.
190      *
191      * @return java.util.Enumeration
192      */

193     public Enumeration JavaDoc getPeristentAttributeNames() {
194         return persistentAttributes.keys();
195     }
196
197     /**
198      * Saves an attribute to the actual session, rather than simply the response.
199      * Use this for storing objects between requests, but only use it with care
200      * since the extra memory used may bog down systems under high load.
201      *
202      * @param attribName the name of the attribute to set
203      * @param attribValue a <code>Serializable</code> java object.
204      */

205     public void setPersistentAttribute(String JavaDoc attribName, Object JavaDoc attribValue) {
206         if (attribValue == null) {
207             attributes.remove(attribName);
208         } else {
209             persistentAttributes.put(attribName, attribValue);
210         }
211     }
212
213     /**
214      * Retrieves the object from the session context.
215      *
216      * @param attribName the name of the object to retrieve
217      * @return the object or null if it doesn't exist.
218      */

219     public Object JavaDoc getPersistentAttribute(String JavaDoc attribName) {
220         return persistentAttributes.get(attribName);
221     }
222
223     /**
224      * Clear out the session. Invalidates the entire session.
225      */

226     public void invalidate() {
227         attributes = new Hashtable JavaDoc();
228         persistentAttributes = new Hashtable JavaDoc();
229     }
230
231     /**
232      * Clears an attribute from the request context
233      *
234      * @param attribName the name of the attribute to remove.
235      */

236     public void removeAttribute(String JavaDoc attribName) {
237         attributes.remove(attribName);
238     }
239
240     /**
241      * Clears an attribute from the session context
242      *
243      * @param attribName the name of the attribute to remove.
244      */

245     public void removePersistentAttribute(String JavaDoc attribName) {
246         persistentAttributes.remove(attribName);
247     }
248 }
249
Popular Tags