View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.services.webpage;
18  
19  // java.util
20  import java.util.HashMap;
21  import java.util.Iterator;
22  
23  // javax.servlet
24  import javax.servlet.http.*;
25  
26  import org.apache.log4j.Logger;
27  
28  /***
29   *  Stores a map of sessions with other sites for the current portal session.
30   *  The map is keyed by the portal session's SessionID (from the servlet api). 
31   *  Binds to the servlet's session so that it can be cleaned up on session end.
32   *  This session keeps a map of all live sessions per site local session.
33   *
34   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
35   * @version $Id: SessionMap.java,v 1.3 2004/02/23 03:46:26 jford Exp $ 
36   */
37  
38  public class SessionMap extends HashMap implements HttpSessionBindingListener
39  {    
40  
41      // the name of the user for this session
42      private String sessionUser;
43      
44      // the servlet session id on this server
45      private String sessionID;
46      
47      // the active status of this session
48      private boolean active = true;
49  
50      // hit count
51      private int hitCount = 0;
52  
53      // cache hit count 
54      private int cacheCount = 0;
55  
56      static Logger log = Logger.getLogger(SessionMap.class);
57  
58      /***
59       * construct a Session Map
60       *
61       * @param sessionId the sessionID on this host
62       * @param sessionUser the user associated with the new session.
63       *
64       */
65      public SessionMap(String sessionID, String sessionUser)
66      {
67          this.sessionID = sessionID;
68          this.sessionUser = sessionUser;
69      }
70  
71  
72      /***
73       * Gets the user associated with this session.
74       *
75       * @return the user associated with this sessions.
76       */
77      public String getUser()
78      {
79          return sessionUser;
80      }
81  
82      /***
83       * Gets the Session ID associated with this session.
84       *
85       * @return the session ID associated with this sessions.
86       */
87      public String getSessionID()
88      {
89          return sessionID;
90      }
91        
92      /***
93       * Gets the Session State, for the servlet session.
94       *
95       * @return the a string describing the session state.
96       */
97      public String getSessionState()
98      {
99          return (active) ? "active" : "inactive";
100     }
101 
102     /***
103      * Gets the managed session count for this portal session.
104      *
105      * @return the managed session count for this session.
106      */
107     public int getSessionCount()
108     {
109         return this.size();
110     }
111 
112     /***
113      * Gets the hitcount for this session.
114      *
115      * @return the hitcount for this session.
116      */
117     public int getHitCount()
118     {
119         return hitCount;
120     }
121 
122     /***
123      * Increments the hitcount for this session.
124      *
125      */
126     public void incHitCount()
127     {
128         hitCount++;
129     }
130 
131     /***
132      * Gets the cache count for this session.
133      *
134      * @return the cache count for this session.
135      */
136     public int getCacheCount()
137     {
138         return cacheCount;
139     }
140 
141     /***
142      * Increments the hitcount for this session.
143      *
144      */
145     public void incCacheCount()
146     {
147         cacheCount++;
148     }
149 
150 
151     /***
152      * This method is a session binding event callback.
153      * It is called when the session is bound
154      *
155      */
156     public void valueBound(HttpSessionBindingEvent event)
157     {
158     }
159 
160     /***
161      * This method is a session unbinding event callback.
162      * It is called when the session is unbound.
163      * We need to clean up all the site sessions associated with this Session.
164      * The session is marked as 'not active' and is no longer useable.
165      * It session object is then removed from the agent's cache 
166      *
167      */    
168     public void valueUnbound(HttpSessionBindingEvent event)
169     {
170         log.info("~~~ SessionMap UNBOUND as " + event.getName() + " from " + event.getSession().getId() );
171 
172         // Now logout of all sessions
173         Iterator it = values().iterator();
174         while (it.hasNext())
175         {
176             SiteSession hps = (SiteSession)it.next();
177             try 
178             {
179                 hps.logout(null);
180 
181             } catch(Exception e)
182             {
183                 // continue logging out even if one fails
184                 log.error("Unbound-Logout of Session: " + e);                                
185             }            
186         }
187         active = false;
188         clear();
189     }
190 
191 
192  }