KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > valves > PersistentValve


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.valves;
20
21
22 import java.io.IOException JavaDoc;
23
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.apache.catalina.Context;
28 import org.apache.catalina.Manager;
29 import org.apache.catalina.Session;
30 import org.apache.catalina.Store;
31 import org.apache.catalina.connector.Request;
32 import org.apache.catalina.connector.Response;
33 import org.apache.catalina.core.StandardHost;
34 import org.apache.catalina.session.PersistentManager;
35 import org.apache.catalina.util.StringManager;
36
37
38 /**
39  * Valve that implements the default basic behavior for the
40  * <code>StandardHost</code> container implementation.
41  * <p>
42  * <b>USAGE CONSTRAINT</b>: To work correctly it requires a PersistentManager.
43  *
44  * @author Jean-Frederic Clere
45  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
46  */

47
48 public class PersistentValve
49     extends ValveBase {
50
51
52     // ----------------------------------------------------- Instance Variables
53

54
55     /**
56      * The descriptive information related to this implementation.
57      */

58     private static final String JavaDoc info =
59         "org.apache.catalina.valves.PersistentValve/1.0";
60
61
62     /**
63      * The string manager for this package.
64      */

65     private static final StringManager sm =
66         StringManager.getManager(Constants.Package);
67
68
69     // ------------------------------------------------------------- Properties
70

71
72     /**
73      * Return descriptive information about this Valve implementation.
74      */

75     public String JavaDoc getInfo() {
76
77         return (info);
78
79     }
80
81
82     // --------------------------------------------------------- Public Methods
83

84
85     /**
86      * Select the appropriate child Context to process this request,
87      * based on the specified request URI. If no matching Context can
88      * be found, return an appropriate HTTP error.
89      *
90      * @param request Request to be processed
91      * @param response Response to be produced
92      *
93      * @exception IOException if an input/output error occurred
94      * @exception ServletException if a servlet error occurred
95      */

96     public void invoke(Request request, Response JavaDoc response)
97         throws IOException JavaDoc, ServletException JavaDoc {
98
99         // Select the Context to be used for this Request
100
StandardHost host = (StandardHost) getContainer();
101         Context context = request.getContext();
102         if (context == null) {
103             response.sendError
104                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
105                  sm.getString("standardHost.noContext"));
106             return;
107         }
108
109         // Bind the context CL to the current thread
110
Thread.currentThread().setContextClassLoader
111             (context.getLoader().getClassLoader());
112
113         // Update the session last access time for our session (if any)
114
String JavaDoc sessionId = request.getRequestedSessionId();
115         Manager manager = context.getManager();
116         if (sessionId != null && manager != null) {
117             if (manager instanceof PersistentManager) {
118                 Store store = ((PersistentManager) manager).getStore();
119                 if (store != null) {
120                     Session session = null;
121                     try {
122                         session = store.load(sessionId);
123                     } catch (Exception JavaDoc e) {
124                         container.getLogger().error("deserializeError");
125                     }
126                     if (session != null) {
127                         if (!session.isValid() ||
128                             isSessionStale(session, System.currentTimeMillis())) {
129                             if (container.getLogger().isDebugEnabled())
130                                 container.getLogger().debug("session swapped in is invalid or expired");
131                             session.expire();
132                             store.remove(sessionId);
133                         } else {
134                             session.setManager(manager);
135                             // session.setId(sessionId); Only if new ???
136
manager.add(session);
137                             // ((StandardSession)session).activate();
138
session.access();
139                         }
140                     }
141                 }
142             }
143         }
144         if (container.getLogger().isDebugEnabled())
145             container.getLogger().debug("sessionId: " + sessionId);
146
147         // Ask the next valve to process the request.
148
getNext().invoke(request, response);
149
150         // Read the sessionid after the response.
151
// HttpSession hsess = hreq.getSession(false);
152
Session hsess;
153         try {
154             hsess = request.getSessionInternal();
155         } catch (Exception JavaDoc ex) {
156             hsess = null;
157         }
158         String JavaDoc newsessionId = null;
159         if (hsess!=null)
160             newsessionId = hsess.getIdInternal();
161
162         if (container.getLogger().isDebugEnabled())
163             container.getLogger().debug("newsessionId: " + newsessionId);
164         if (newsessionId!=null) {
165             /* store the session in the store and remove it from the manager */
166             if (manager instanceof PersistentManager) {
167                 Session session = manager.findSession(newsessionId);
168                 Store store = ((PersistentManager) manager).getStore();
169                 if (store != null && session!=null &&
170                     session.isValid() &&
171                     !isSessionStale(session, System.currentTimeMillis())) {
172                     // ((StandardSession)session).passivate();
173
store.save(session);
174                     ((PersistentManager) manager).removeSuper(session);
175                     session.recycle();
176                 } else {
177                     if (container.getLogger().isDebugEnabled())
178                         container.getLogger().debug("newsessionId store: " + store + " session: " +
179                                 session + " valid: " + session.isValid() +
180                                 " Staled: " +
181                                 isSessionStale(session, System.currentTimeMillis()));
182
183                 }
184             } else {
185                 if (container.getLogger().isDebugEnabled())
186                     container.getLogger().debug("newsessionId Manager: " + manager);
187             }
188         }
189     }
190
191     /**
192      * Indicate whether the session has been idle for longer
193      * than its expiration date as of the supplied time.
194      *
195      * FIXME: Probably belongs in the Session class.
196      */

197     protected boolean isSessionStale(Session session, long timeNow) {
198  
199         int maxInactiveInterval = session.getMaxInactiveInterval();
200         if (maxInactiveInterval >= 0) {
201             int timeIdle = // Truncate, do not round up
202
(int) ((timeNow - session.getLastAccessedTime()) / 1000L);
203             if (timeIdle >= maxInactiveInterval)
204                 return true;
205         }
206  
207         return false;
208  
209     }
210
211 }
212
Popular Tags