KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > j2ee > session > ValidatingInterceptor


1 // ========================================================================
2
// $Id: ValidatingInterceptor.java,v 1.4 2004/05/09 20:30:48 gregwilkins Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// 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.jfox.ioc.logger.Logger;
21
22 //----------------------------------------
23

24
25 public class ValidatingInterceptor
26   extends AroundInterceptor
27 {
28   protected static final Logger _log=Logger.getLogger(ValidatingInterceptor.class);
29
30   protected void before() throws IllegalStateException JavaDoc {if (_running) checkValid();}
31   protected void after() {}
32
33   //----------------------------------------
34

35   protected boolean _running=false;
36
37   public void start() {_log.trace("start()");_running=true;}
38   public void stop() {_log.trace("stop()"); _running=false;}
39
40   protected void
41     checkValid()
42     throws IllegalStateException JavaDoc
43   {
44     boolean valid=false;
45     State state=getState();
46     try
47     {
48       int mii=state.getMaxInactiveInterval(); // secs
49
int keep=mii;
50       mii=mii<1?getManager().getStore().getActualMaxInactiveInterval():mii; // secs
51
long lat=state.getLastAccessedTime(); // milisecs
52
long now=System.currentTimeMillis(); // milisecs
53

54       int age=(int)((now-lat)/1000); // secs
55

56       valid=(age<mii);
57       if (_log.isTraceEnabled()) _log.trace("session keep="+keep+", mii="+mii+", lat="+lat+", now="+now+", age="+age+", valid="+valid);
58     }
59     catch (java.rmi.NoSuchObjectException JavaDoc ignore)
60     {
61       // _log.info("IGNORE ABOVE NoSuchEntityException - harmless");
62
}
63     catch (javax.ejb.NoSuchEntityException JavaDoc ignore)
64     {
65       // _log.info("IGNORE ABOVE NoSuchEntityException - harmless");
66
}
67     catch (Exception JavaDoc e)
68     {
69       _log.error("couldn't determine validity of HttpSession", e);
70     }
71
72     if (!valid)
73       throw new IllegalStateException JavaDoc("invalid HttpSession - timed out");
74   }
75
76   // public Object clone() { return this; } // Stateless
77
}
78
Popular Tags