001    /*
002      Copyright (C) 2001-2002 Renaud Pawlak <renaud@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.aspects.gui.web;
019    
020    import java.util.Stack;
021    
022    /**
023     * This class defines a session for thin client servers.
024     *
025     * @see Request 
026     */
027    
028    public class Session implements java.io.Serializable {
029    
030       /** The requests stack for this session. */
031       transient protected Stack requests = new Stack();
032    
033       /** This session's ID. */
034       protected String sid;
035    
036       /**
037        * The constructor for a session with a given ID. */
038     
039       public Session(String sid) {
040          this.sid = sid;
041       }
042    
043       /**
044        * Returns the session's ID.
045        *
046        * @return the ID */
047    
048       public String getId() {
049          return sid;
050       }  
051    
052       /**
053        * Returns the stack of the requests for this session.
054        *
055        * <p><code>getRequests().peek()</code> is the request that is
056        * currently treated for this session.
057        *
058        * @return the requests stack */
059    
060       public Stack getRequests() {
061          return requests;
062       }
063    
064       /**
065        * Returns the number of active requests on the requests stack for
066        * this session.
067        * 
068        * @return requests stack count */
069    
070       public int getRequestCount() {
071          return requests.size();
072       }
073    
074       /**
075        * Creates a new request for this session (pushes it on the
076        * requests stack). The newly created request becomes the current
077        * one of the session.
078        *
079        * @param request the request to push
080        * @see #getCurrentRequest() 
081        * @see #endCurrentRequest() */
082    
083       public void newRequest(Request request) {
084          getRequests().push(request);
085       }
086    
087       /**
088        * Returns the current request of this session (same as
089        * <code>getRequests().peek()</code>).
090        *
091        * @return the current request */
092    
093       public Request getCurrentRequest() {
094          return (Request)getRequests().peek();
095       }
096    
097       /**
098        * Returns the previous request of this session (ie the one that
099        * was achieved before the current one).
100        *
101        * @return the previous request, null if no previous request is
102        * available */
103    
104       public Request getPreviousRequest() {
105          Request prevRequest = null;
106          if (requests.size() > 1) {
107             prevRequest = (Request)requests.get(requests.size()-2);
108          }
109          return prevRequest;
110       }
111    
112       /**
113        * Ends the current request (same as
114        * <code>getRequests().pop()</code>).
115        *
116        * @return the request that has just been ended */
117    
118       public Request endCurrentRequest() {
119          return (Request)getRequests().pop();
120       }   
121    
122       /**
123        * Gets a humain-readable string representation of the session. 
124        * @return a string */
125    
126       public String toString() {
127          return "session " + sid + ", requests stack = " + requests;
128       }
129    
130    }