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