001: /*
002: HttpdBase4J: An embeddable Java web server framework that supports HTTP, HTTPS,
003: templated content and serving content from inside a jar or archive.
004: Copyright (C) 2007 Donald Munro
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not,see http://www.gnu.org/licenses/lgpl.txt
018: */
019:
020: package net.homeip.donaldm.httpdbase4j;
021:
022: import java.util.HashMap;
023: import java.util.Map;
024: import java.util.concurrent.atomic.AtomicLong;
025:
026: /*
027: * A simple sessions manager. May be used to maintain state for simple
028: * web applications.
029: * @author Donald Munro
030: */
031: public class SimpleSessionManager
032: //===============================
033: {
034:
035: /*
036: * Unique session id used to identify a session
037: */
038: static private AtomicLong m_sessionSequence = new AtomicLong(0);
039:
040: /*
041: * Session Map. Maps a session id onto another Map that maps a
042: * variable name (String) onto a value (Object).
043: */
044: static protected Map<Long, Map<String, Object>> m_sessionMap = null;
045:
046: private static final class SingletonHolder {
047: static final SimpleSessionManager singleton = new SimpleSessionManager();
048: }
049:
050: private SimpleSessionManager() {
051: }
052:
053: /**
054: * Return the single SimpleSessionManager instance.
055: * @return The singleton SimpleSessionManager instance
056: */
057: static public SimpleSessionManager getSimpleSessionManager()
058: //----------------------------------------------------------
059: {
060: return SingletonHolder.singleton;
061: }
062:
063: /**
064: *
065: * @return The next available session id
066: */
067: static public long getNextSessionId()
068: //-----------------------------------
069: {
070: return m_sessionSequence.getAndIncrement();
071: }
072:
073: /**
074: * Set a session variable.
075: * @param sessionId The session id for the session
076: * @param varName The name of the session variable
077: * @param value The variable value
078: */
079: static public void setSessionVariable(long sessionId,
080: String varName, Object value)
081: //---------------------------------------------------------------------
082: {
083: if (m_sessionMap == null)
084: m_sessionMap = new HashMap<Long, Map<String, Object>>();
085: Map<String, Object> m = m_sessionMap.get(sessionId);
086: if (m == null) {
087: m = new HashMap<String, Object>();
088: m_sessionMap.put(sessionId, m);
089: }
090: m.put(varName, value);
091: }
092:
093: /**
094: * Get a session variable.
095: * @param sessionId The session id for the session
096: * @param varName The name of the session variable
097: * @return The value of the session variable <i>varName</i> or null
098: * if no such session or no such variable
099: */
100: static public Object getSessionVariable(long sessionId,
101: String varName)
102: //---------------------------------------------------------------------
103: {
104: if (m_sessionMap == null)
105: return null;
106: Map<String, Object> m = m_sessionMap.get(sessionId);
107: if (m == null)
108: return null;
109: return m.get(varName);
110: }
111:
112: /**
113: * Delete a session variable.
114: * @param sessionId The session id for the session
115: * @param varName The name of the session variable
116: * @return The value of the session variable <i>varName</i> or null
117: * if no such session or no such variable
118: */
119: static public Object removeSessionVariable(long sessionId,
120: String varName)
121: //-----------------------------------------------------------------------
122: {
123: if (m_sessionMap == null)
124: return null;
125: Map<String, Object> m = m_sessionMap.get(sessionId);
126: if (m == null)
127: return null;
128: return m.remove(varName);
129: }
130:
131: /**
132: * Delete a session.
133: * @param sessionId The session id for the session
134: */
135: static public void clearSession(long sessionId)
136: //---------------------------------------------
137: {
138: if (m_sessionMap == null)
139: return;
140: Map<String, Object> m = m_sessionMap.remove(sessionId);
141: m.clear();
142: m = null;
143: }
144: }
|