Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/tripi/asp/Session.java


1   /**
2    * ArrowHead ASP Server 
3    * This is a source file for the ArrowHead ASP Server - an 100% Java
4    * VBScript interpreter and ASP server.
5    *
6    * For more information, see http://www.tripi.com/arrowhead
7    *
8    * Copyright (C) 2002  Terence Haddock
9    *
10   * This program is free software; you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as published by
12   * the Free Software Foundation; either version 2 of the License, or
13   * (at your option) any later version.
14   *
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   *
20   * You should have received a copy of the GNU General Public License
21   * along with this program; if not, write to the Free Software
22   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23   *
24   */
25  package com.tripi.asp;
26  
27  import javax.servlet.http.HttpSession;
28  import javax.servlet.http.HttpServletRequest;
29  
30  import java.util.Enumeration;
31  import java.io.Serializable;
32  import org.apache.log4j.Category;
33  
34  /**
35   * Session class, implements the ASP "Session" object.
36   * <p>Status:
37   * <ul>
38   * <li><b>CodePage</b> - Not implemented
39   * <li><b>LCID</b> - Not implemented
40   * <li><b>SessionID</b> - Not implemented
41   * <li><b>Timeout</b> - Not implemented
42   * <li><b>Contents</b> - Implemented <b>TODO</b> Have to implement ASP syntax
43   * <li><b>Contents.Remove</b> - Not implemented
44   * <li><b>Contents.RemoveAll</b> - Not implemented
45   * <li><b>StaticObjects</b> - Not implemented
46   * <li><b>Abandon</b> - Implmented
47   * </ul>
48   *
49   * @author Terence Haddock
50   * @version 0.9
51   */
52  public class Session implements SimpleMap
53  {
54      /** Debugging category */
55      private final static Category DBG = Category.getInstance(Session.class);
56  
57      /** HTTP Session */
58      HttpSession httpSession;
59  
60      /** HTTP Request object */
61      HttpServletRequest request;
62  
63      /** The actual collection contents */
64      AspCollection collection = null;
65  
66      /** Local context, Session_OnStart and Session_OnEnd are executed under
67          this context */
68      AspContext localContext;
69  
70      /** Reference to the collection object */
71      public SimpleReference Contents = new ContentsReference();
72  
73      /** Session timeout value */
74      public int timeout = 60;
75  
76      /** Session LCID value */
77      public int LCID = 0;
78  
79      /**
80       * Constructor.
81       * @param request HTTP Request object
82       * @param localContext Local context object, used in call to Session_On*
83       */
84      public Session(HttpServletRequest request, AspContext localContext)
85      {
86          this.request = request;
87          this.localContext = localContext;
88      }
89  
90      /**
91       * Special, protected constructor for use with Session_OnStart and
92       * Session_OnEnd.
93       * @param session pre-existing session
94       */
95      protected Session(HttpSession session, AspCollection aspCollection)
96      {
97          this.httpSession = session;
98          this.collection = aspCollection;
99          this.request = null;
100         this.localContext = null;
101     }
102 
103     /**
104      * Obtains a value in the session object, implementing the SimpleMap.get
105      * function. Key will be converted to string and is case insensitive.
106      * @param key Key of object to obtain, will be converted to string.
107      * @return value of object with the given key.
108      * @throws AspException on error
109      * @see SimpleMap#get
110      */
111     public Object get(Object key) throws AspException
112     {
113         if (collection == null) getSession();
114         return collection.get(key);
115     }
116 
117     /**
118      * Stores a value in the session. Key will be converted to string
119      * and is case insensitive.
120      * @param key Key of object to store, will be converted to string.
121      * @param value Value to store with the given key.
122      * @throws AspException on error
123      * @see SimpleMap#put
124      */
125     public void put(Object key, Object value) throws AspException
126     {
127         if (collection == null) getSession();
128         collection.put(key, value);
129     }
130 
131     /**
132      * Obtains the keys of the map interface.
133      * @return enumeration of all keys stores in this session.
134      * @throws AspException if an error occurs
135      * @see SimpleMap#getKeys
136      */
137     public Enumeration getKeys() throws AspException
138     {
139         if (collection == null) getSession();
140         return collection.getKeys();
141     }
142 
143     /**
144      * ASP function, abandons the current session.
145      */
146     public void abandon() throws AspException
147     {
148         if (httpSession == null) getSession();
149         httpSession.invalidate();
150     }
151 
152     /**
153      * ASP function, obtains the session identifier
154      * @return string representation of the session identifier.
155      */
156     public String SessionID() throws AspException
157     {
158         if (httpSession == null) getSession();
159         return httpSession.getId();
160     }
161 
162     /**
163      * Internal function to obtain the session if it already exists.
164      */
165     protected void getSessionIfExists() throws AspException
166     {
167         httpSession = request.getSession(false);
168         if (httpSession != null) getSession();
169     }
170 
171     /**
172      * Internal function which obtains the current, active session.
173      */
174     private void getSession() throws AspException
175     {
176         httpSession = request.getSession(true);
177         if (DBG.isDebugEnabled())
178         {
179             if (httpSession.isNew()) DBG.debug("New session");
180         } 
181         collection = AspSessionHandler.getContents(httpSession);
182         if (collection == null) throw new AspException("Internal Error, did you add the listener tags to your web.xml file?");
183     }
184 
185     /**
186      * This class handles the Contents variable, which is not defined
187      * until it is first accessed.
188      */
189     class ContentsReference implements SimpleReference
190     {
191         /**
192          * Obtain the value this reference represents
193          */
194         public Object getValue() throws AspException
195         {
196             if (collection == null) getSession();
197             return collection;
198         }
199 
200         /**
201          * Sets the value this reference represents.
202          */
203         public void setValue(Object obj) throws AspException
204         {
205             throw new AspReadOnlyException("Contents variable cannot be set");
206         }
207     }
208 }