001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 26/08/2006 21:55:33
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.context.standard;
044:
045: import java.util.Enumeration;
046: import java.util.Hashtable;
047: import java.util.Random;
048:
049: import net.jforum.context.SessionContext;
050:
051: /**
052: * Session Context non-dependent of HTTP requests
053: * @author Rafael Steil
054: * @version $Id: StandardSessionContext.java,v 1.2 2006/09/04 01:00:07 rafaelsteil Exp $
055: */
056: public class StandardSessionContext implements SessionContext {
057: private static Random random = new Random();
058: public static String SESSION_ID = "__sessionId";
059: private Hashtable data;
060:
061: public StandardSessionContext() {
062: this .data = new Hashtable();
063: this .createSessionId();
064: }
065:
066: private void createSessionId() {
067: this .data.put(SESSION_ID, Integer.toString(random
068: .nextInt(99999999)));
069: }
070:
071: /**
072: * @see net.jforum.context.SessionContext#getAttribute(java.lang.String)
073: */
074: public Object getAttribute(String name) {
075: return this .data.get(name);
076: }
077:
078: /**
079: * @see net.jforum.context.SessionContext#getAttributeNames()
080: */
081: public Enumeration getAttributeNames() {
082: return this .data.elements();
083: }
084:
085: /**
086: * @see net.jforum.context.SessionContext#getId()
087: */
088: public String getId() {
089: return (String) this .getAttribute(SESSION_ID);
090: }
091:
092: /**
093: * @see net.jforum.context.SessionContext#invalidate()
094: */
095: public void invalidate() {
096: this .data.clear();
097: this .createSessionId();
098: }
099:
100: /**
101: * @see net.jforum.context.SessionContext#removeAttribute(java.lang.String)
102: */
103: public void removeAttribute(String name) {
104: this .data.remove(name);
105: }
106:
107: /**
108: * @see net.jforum.context.SessionContext#setAttribute(java.lang.String, java.lang.Object)
109: */
110: public void setAttribute(String name, Object value) {
111: if (this.data.containsKey(name)) {
112: this.data.remove(name);
113: }
114:
115: this.data.put(name, value);
116: }
117: }
|