001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/security/session/SessionID.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.sql.Timestamp;
039    import java.util.Date;
040    import java.util.GregorianCalendar;
041    
042    import org.deegree.framework.util.IDGenerator;
043    
044    /**
045     *
046     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
047     * @author last edited by: $Author: mschneider $
048     *
049     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
050     */
051    public class SessionID {
052    
053        private String id = null;
054    
055        private Date created = null;
056    
057        private Date expiration = null;
058    
059        private int duration = 0;
060    
061        private boolean closed = false;
062    
063        /**
064         * The constructor ensures that a sessionID that is unique within the JVM will be created. If
065         * the sessionID must be unique across two or more JVMs use the construtor
066         * {@link #SessionID(String, int)} and ensure that the passed ID is valid.
067         *
068         * @param duration
069         *            duration in millis the ID is valid. -1 indicates that a sessionID never expires.
070         */
071        public SessionID( int duration ) {
072            this.id = "ID" + IDGenerator.getInstance().generateUniqueID() + "-" + Math.random();
073            this.duration = duration;
074            created = ( new GregorianCalendar() ).getTime();
075            expiration = new Timestamp( created.getTime() + duration );
076        }
077    
078        /**
079         * @param id
080         *            encapsulated ID
081         * @param duration
082         *            duration in millis the ID is valid. -1 indicates that a sessionID never expires.
083         */
084        public SessionID( String id, int duration ) {
085            this.id = id;
086            this.duration = duration;
087            created = ( new GregorianCalendar() ).getTime();
088            expiration = new Timestamp( created.getTime() + duration );
089        }
090    
091        /**
092         * @return Returns the created.
093         *
094         */
095        public Date getCreated() {
096            return created;
097        }
098    
099        /**
100         * if sessionID never expires this method returns <tt>new Timestamp(0)</tt>
101         *
102         * @return Returns the expiration.
103         *
104         */
105        public Date getExpiration() {
106            if ( duration < 0 )
107                return new Timestamp( 0 );
108            return expiration;
109        }
110    
111        /**
112         * this method throws an exception if the sessinID has been killed or is alive anymore
113         *
114         * @return Returns the id.
115         * @throws SessionStatusException
116         *
117         */
118        public String getId()
119                                throws SessionStatusException {
120            if ( closed ) {
121                throw new SessionStatusException( "session has been killed" );
122            }
123            if ( !isAlive() ) {
124                throw new SessionExpiredException( "session has been expired" );
125            }
126            return id;
127        }
128    
129        /**
130         * returns true if the expiration date of the <tt>SessionID</tt> is after the current
131         * timestamp
132         *
133         * @return <code>true</code> if the expiration date of the <tt>SessionID</tt> is after the
134         *         current timestamp
135         */
136        public boolean isAlive() {
137            if ( closed )
138                return false;
139            if ( duration < 0 )
140                return true;
141            return expiration.after( new Timestamp( System.currentTimeMillis() ) );
142        }
143    
144        /**
145         * resets the expiration date to the current timestamp plus duration passed to the constructor
146         * when creating the <tt>SessionID</tt> this method throws an exception if the sessinID has
147         * been killed or is alive anymore
148         *
149         * @throws SessionStatusException
150         */
151        public void reset()
152                                throws SessionStatusException {
153            if ( closed ) {
154                throw new SessionStatusException( "session has been killed" );
155            }
156            if ( !isAlive() ) {
157                throw new SessionExpiredException( "session has been expired" );
158            }
159            if ( duration < 0 )
160                return;
161            expiration = new Timestamp( System.currentTimeMillis() + duration );
162        }
163    
164        /**
165         * kills a SessionID by marking it as invalid. A killed SessionID can't be reseted
166         */
167        public void close() {
168            closed = true;
169        }
170    }