Source code: com/opencms/core/CmsSession.java
1 /*
2 * File : $Source: /usr/local/cvs/opencms/src/com/opencms/core/CmsSession.java,v $
3 * Date : $Date: 2003/01/20 17:57:49 $
4 * Version: $Revision: 1.23 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * For further information about OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */
28
29 package com.opencms.core;
30
31 import com.opencms.boot.I_CmsLogChannels;
32
33 import java.util.Enumeration;
34 import java.util.Hashtable;
35
36 import javax.servlet.http.HttpSession;
37
38 /**
39 * Implements the I_CmsSession interface and is required by the OpenCms session
40 * handling mechanism.<p>
41 *
42 * This <code>CmsSession</code> object should be used instead of the
43 * <code>HttpSession</code>.
44 *
45 * @author Michael Emmerich
46 *
47 * @version $Revision: 1.23 $ $Date: 2003/01/20 17:57:49 $
48 */
49 public class CmsSession implements I_CmsSession,I_CmsConstants {
50
51 /**
52 * The original HttpSession
53 */
54 private HttpSession m_session;
55
56 /**
57 * The sessiondata.
58 */
59 private Hashtable m_sessionData;
60
61 /**
62 * Constructs a new CmsSession based on a HttpSession.
63 *
64 * @param originalSession the original session to use.
65 */
66 public CmsSession(HttpSession originalSession) {
67 m_session = originalSession;
68 m_sessionData = (Hashtable)m_session.getAttribute(C_SESSION_DATA);
69
70 // if there is no session-data, create a new one.
71 if(m_sessionData == null) {
72 m_sessionData = new Hashtable();
73 m_session.setAttribute(C_SESSION_DATA, m_sessionData);
74 }
75 }
76
77 /**
78 * Gets a value from the session.
79 *
80 * @param name the key for the value.
81 * @return the object associated with this key.
82 */
83 public Object getValue(String name) {
84 return m_sessionData.get(name);
85 }
86
87 /**
88 * Gets the names of all values stored in the session.
89 *
90 * @return String array containing all value names.
91 */
92 public String[] getValueNames() {
93 String[] name = new String[m_sessionData.size()];
94 Enumeration enu = m_sessionData.keys();
95 for(int i = 0;i < m_sessionData.size();i++) {
96 name[i] = (String)enu.nextElement();
97 }
98 return name;
99 }
100
101 /**
102 * Puts a value into the session.
103 *
104 * @param name the key for the value.
105 * @param value an object to be stored in the session.
106 */
107 public void putValue(String name, Object value) {
108 m_sessionData.put(name, value);
109
110 try {
111 // indicate, that the session should be stored after the request.
112 m_session.setAttribute(C_SESSION_IS_DIRTY, new Boolean(true));
113 } catch (Exception exc) {
114 if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
115 A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_INFO, "[CmsSession] Error marking session as dirty " + exc.getClass().getName() + " " + exc.getMessage());
116 }
117 }
118 }
119
120 /**
121 * Removes a value from the session.
122 *
123 * @param name the key for the value to be removed.
124 */
125 public void removeValue(String name) {
126 m_sessionData.remove(name);
127
128 // indicate, that the session should be stored after the request.
129 m_session.setAttribute(C_SESSION_IS_DIRTY, new Boolean(true));
130 }
131
132 /**
133 * Invalidates the session.
134 */
135 public void invalidate() {
136 if (m_session != null) m_session.invalidate();
137 // if there is session-data, invalidate it as well
138 if(m_sessionData != null) {
139 m_sessionData = new Hashtable();
140 }
141 }
142 }