01: // ========================================================================
02: // $Id: ValidatingInterceptor.java,v 1.4 2004/05/09 20:30:48 gregwilkins Exp $
03: // Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
04: // ------------------------------------------------------------------------
05: // Licensed under the Apache License, Version 2.0 (the "License");
06: // you may not use this file except in compliance with the License.
07: // You may obtain a copy of the License at
08: // http://www.apache.org/licenses/LICENSE-2.0
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14: // ========================================================================
15:
16: package org.mortbay.j2ee.session;
17:
18: //----------------------------------------
19:
20: import org.jboss.logging.Logger;
21:
22: //----------------------------------------
23:
24: public class ValidatingInterceptor extends AroundInterceptor {
25: protected static final Logger _log = Logger
26: .getLogger(ValidatingInterceptor.class);
27:
28: protected void before() throws IllegalStateException {
29: if (_running)
30: checkValid();
31: }
32:
33: protected void after() {
34: }
35:
36: //----------------------------------------
37:
38: protected boolean _running = false;
39:
40: public void start() {
41: _log.trace("start()");
42: _running = true;
43: }
44:
45: public void stop() {
46: _log.trace("stop()");
47: _running = false;
48: }
49:
50: protected void checkValid() throws IllegalStateException {
51: boolean valid = false;
52: State state = getState();
53: try {
54: int mii = state.getMaxInactiveInterval(); // secs
55: int keep = mii;
56: mii = mii < 1 ? getManager().getStore()
57: .getActualMaxInactiveInterval() : mii; // secs
58: long lat = state.getLastAccessedTime(); // milisecs
59: long now = System.currentTimeMillis(); // milisecs
60:
61: int age = (int) ((now - lat) / 1000); // secs
62:
63: valid = (age < mii);
64: if (_log.isTraceEnabled())
65: _log.trace("session keep=" + keep + ", mii=" + mii
66: + ", lat=" + lat + ", now=" + now + ", age="
67: + age + ", valid=" + valid);
68: } catch (java.rmi.NoSuchObjectException ignore) {
69: // _log.info("IGNORE ABOVE NoSuchEntityException - harmless");
70: } catch (javax.ejb.NoSuchEntityException ignore) {
71: // _log.info("IGNORE ABOVE NoSuchEntityException - harmless");
72: } catch (Exception e) {
73: _log.error("couldn't determine validity of HttpSession", e);
74: }
75:
76: if (!valid)
77: throw new IllegalStateException(
78: "invalid HttpSession - timed out");
79: }
80:
81: // public Object clone() { return this; } // Stateless
82: }
|