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