001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064: package com.jcorporate.expresso.core.controller.session;
065:
066: import java.util.Enumeration;
067: import java.util.HashMap;
068: import java.util.Hashtable;
069: import java.util.Map;
070:
071: /**
072: * This is an implementation of PersistentSession that is useful for command
073: * line usage. The code containing the controller should hand the controller
074: * an instantiated SimplePersistentSession so that the state handlers can use
075: * ControllerRequest.getSession() and simply use it without any cares in the
076: * world.
077: * <p>Example Usage: <br/>
078: * <code>
079: * <pre>
080: * SimplePersistentSession session = new SimplePersistentSession();
081: * Controller con = ConfigManager.getControllerFactory().getController(
082: * controllerName);
083: * ControllerRequest params = new ControllerRequest();
084: * params.setUid(3);
085: * params.setSession(mySession);
086: * <pre>
087: * </code>
088: * </p>
089: *
090: * @author Michael Nash
091: * @see com.jcorporate.expresso.core.controller.session.PersistentSession
092: * @since Expresso 4.0
093: */
094: public class SimplePersistentSession implements PersistentSession {
095: /**
096: * A storage place for attributes
097: */
098: private Hashtable attributes = new Hashtable();
099:
100: /**
101: * A Storage place for persistent attributes
102: */
103: private Hashtable persistentAttributes = new Hashtable();
104:
105: /**
106: * Creates new SimplePersistentSession
107: */
108: public SimplePersistentSession() {
109: }
110:
111: /**
112: * Sets an attribute that is valid for the duration of the request.
113: *
114: * @param attribName The name of the object you wish to set.
115: * @param attribValue the object you want to set.
116: */
117: public void setAttribute(String attribName, Object attribValue) {
118: if (attribValue == null) {
119: attributes.remove(attribName);
120: } else {
121: attributes.put(attribName, attribValue);
122: }
123: }
124:
125: /**
126: * Retrieves the object from the request context.
127: *
128: * @param attribName the name of the object to retrieve
129: * @return the object or null if it doesn't exist.
130: */
131: public Object getAttribute(String attribName) {
132: return attributes.get(attribName);
133: }
134:
135: /**
136: * Retrieves all attribute names in the request context.
137: *
138: * @return java.util.Enumeration
139: */
140: public Enumeration getAttributeNames() {
141: return attributes.keys();
142: }
143:
144: /**
145: * Does nothing. In a command line environment, cookies don't make
146: * sense.
147: *
148: * @param attribName the name of the attribute to
149: * @param attribValue the value of the attribute to set.
150: */
151: public void setClientAttribute(String attribName, String attribValue) {
152: }
153:
154: /**
155: * Retrieves a value of a cookie set on the client's system. It also
156: * decrypts the value with the password you set up in your <code>expresso-config.xml</code>
157: * file.
158: *
159: * @param attribName the name of the cookie to retrieve
160: * @return java.lang.String the value of the cookie or null if the cookie doesn't exist.
161: */
162: public String getClientAttribute(String attribName) {
163: return null;
164: }
165:
166: /**
167: * Retrieves the persistent attributes table. Useful if you wish to actually
168: * persisten these attributes across a command line 'session'
169: *
170: * @return java.util.HashMap
171: * @since Expresso 5.1
172: */
173: public Map getPersistentAttributes() {
174: return new HashMap(persistentAttributes);
175: }
176:
177: /**
178: * Sets the persistent attributes that may have been saved across instances
179: * of command line 'session'
180: *
181: * @param newAttributes Map of attributes to set for the session.
182: */
183: public void setPersistentAttributes(Map newAttributes) {
184: persistentAttributes = new Hashtable(newAttributes);
185: }
186:
187: /**
188: * Retrieves all attribute names from the session context.
189: *
190: * @return java.util.Enumeration
191: */
192: public Enumeration getPeristentAttributeNames() {
193: return persistentAttributes.keys();
194: }
195:
196: /**
197: * Saves an attribute to the actual session, rather than simply the response.
198: * Use this for storing objects between requests, but only use it with care
199: * since the extra memory used may bog down systems under high load.
200: *
201: * @param attribName the name of the attribute to set
202: * @param attribValue a <code>Serializable</code> java object.
203: */
204: public void setPersistentAttribute(String attribName,
205: Object 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 getPersistentAttribute(String 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();
228: persistentAttributes = new Hashtable();
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 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 attribName) {
246: persistentAttributes.remove(attribName);
247: }
248: }
|