001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/security/session/Session.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2001-2008 by:
006 EXSE, Department of Geography, University of Bonn
007 http://www.giub.uni-bonn.de/deegree/
008 lat/lon GmbH
009 http://www.lat-lon.de
010
011 This library is free software; you can redistribute it and/or
012 modify it under the terms of the GNU Lesser General Public
013 License as published by the Free Software Foundation; either
014 version 2.1 of the License, or (at your option) any later version.
015
016 This library is distributed in the hope that it will be useful,
017 but WITHOUT ANY WARRANTY; without even the implied warranty of
018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 Lesser General Public License for more details.
020
021 You should have received a copy of the GNU Lesser General Public
022 License along with this library; if not, write to the Free Software
023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024
025 Contact:
026
027 Andreas Poth
028 lat/lon GmbH
029 Aennchenstr. 19
030 53115 Bonn
031 Germany
032 E-Mail: poth@lat-lon.de
033
034 Prof. Dr. Klaus Greve
035 Department of Geography
036 University of Bonn
037 Meckenheimer Allee 166
038 53115 Bonn
039 Germany
040 E-Mail: greve@giub.uni-bonn.de
041
042 ---------------------------------------------------------------------------*/
043 package org.deegree.security.session;
044
045 import java.util.Collections;
046 import java.util.HashMap;
047 import java.util.Map;
048
049 /**
050 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
051 * @author last edited by: $Author: apoth $
052 *
053 * @version $Revision: 9346 $, $Date: 2007-12-27 17:39:07 +0100 (Do, 27 Dez 2007) $
054 */
055
056 public class Session {
057
058 private SessionID sessionID = null;
059
060 private String user = null;
061
062 private Map<Object,Object> attributes = Collections.synchronizedMap( new HashMap<Object,Object>() );
063
064 /**
065 * creates a session that never expires for an anonymous user
066 *
067 * @param user
068 * user the session is assigned to
069 */
070 public Session() {
071 this.sessionID = new SessionID( -1 );
072 }
073
074 /**
075 * creates a session that never expires
076 *
077 * @param user
078 * user the session is assigned to
079 */
080 public Session( String user ) {
081 this.sessionID = new SessionID( -1 );
082 this.user = user;
083 }
084
085 /**
086 * creates a session with a specific lifetime for an anonymous user. the expiration date will be
087 * updated each time a user accesses his session
088 *
089 * @param duration
090 */
091 public Session( int duration ) {
092 this( null, duration );
093 }
094
095 /**
096 * creates a session with a specific lifetime. the expiration date will be updated each time a
097 * uses accesses his session
098 *
099 * @param duration
100 * @param user
101 */
102 public Session( String user, int duration ) {
103 this.sessionID = new SessionID( duration );
104 this.user = user;
105 }
106
107 /**
108 * creates a session with a specific SessionID for an anonymous user. the expiration date will
109 * be updated each time a uses accesses his session
110 *
111 * @param sessionID
112 */
113 public Session( SessionID sessionID ) {
114 this( null, sessionID );
115 }
116
117 /**
118 * creates a session with a specific SessionID. the expiration date will be updated each time a
119 * uses accesses his session
120 *
121 * @param sessionID
122 * @param user
123 */
124 public Session( String user, SessionID sessionID ) {
125 super();
126 this.sessionID = sessionID;
127 this.user = user;
128 }
129
130 /**
131 * returns the name user the user who owns the session. returns null if its a session for an
132 * anonymous user
133 *
134 * @return the name user the user who owns the session. returns <code>null</code> if its a
135 * session for an anonymous user
136 *
137 */
138 public String getUser() {
139 return user;
140 }
141
142 /**
143 * adds an attribute to the session. calling this method will reset the expiration date of the
144 * encapsulated sessionID<br>
145 * this method throws an exception if the sessinID has been killed or is alive anymore
146 *
147 * @param key
148 * @param value
149 * @throws SessionStatusException
150 */
151 public void addAttribute( Object key, Object value )
152 throws SessionStatusException {
153 sessionID.reset();
154 attributes.put( key, value );
155 }
156
157 /**
158 * returns the values of the attribute identified by the passed key. calling this method will
159 * reset the expiration date of the encapsulated sessionID<br>
160 * this method throws an exception if the sessinID has been killed or is alive anymore
161 *
162 * @param key
163 * @return the values of the attribute identified by the passed key. calling this method will
164 * reset the expiration date of the encapsulated sessionID
165 * @throws SessionStatusException
166 */
167 public Object getAttribute( Object key )
168 throws SessionStatusException {
169 sessionID.reset();
170 return attributes.get( key );
171 }
172
173 /**
174 * removes the attribute identified by the passed key from the session. calling this method will
175 * reset the expiration date of the encapsulated sessionID<br>
176 * this method throws an exception if the sessinID has been killed or is alive anymore
177 *
178 * @param key
179 * @return
180 * @throws SessionStatusException
181 */
182 public Object removeAttribute( Object key )
183 throws SessionStatusException {
184 sessionID.reset();
185 return attributes.remove( key );
186 }
187
188 /**
189 * returns true if the session is still alive or false if the expiration date of the sessionID
190 * has been reached
191 *
192 * @return <code>true</code> if the session is still alive or <code>false</code> if the
193 * expiration date of the sessionID has been reached
194 */
195 public boolean isAlive() {
196 return sessionID.isAlive();
197 }
198
199 /**
200 * returns the sessionID encapsulated in this session.
201 *
202 * @return the sessionID encapsulated in this session.
203 *
204 */
205 public SessionID getSessionID() {
206 return sessionID;
207 }
208
209 /**
210 * kills a Session by marking the encapsulated SessionID as invalid. A killed SessionID can't be
211 * reseted
212 */
213 public void close() {
214 sessionID.close();
215 }
216
217 /**
218 * resets the expiration date of the session
219 *
220 * @throws SessionStatusException
221 *
222 */
223 public void reset()
224 throws SessionStatusException {
225 sessionID.reset();
226 }
227 }