1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
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 | package net.sourceforge.hiveutils.web.util; |
16 | |
17 | import java.text.MessageFormat; |
18 | |
19 | import javax.servlet.http.HttpSession; |
20 | import javax.servlet.http.HttpSessionEvent; |
21 | import javax.servlet.http.HttpSessionListener; |
22 | |
23 | import org.apache.commons.logging.Log; |
24 | import org.apache.commons.logging.LogFactory; |
25 | |
26 | /** |
27 | * SessionListener that logs all session creation/destruction, including the |
28 | * the duration of activity of each session. |
29 | * |
30 | * @author Jean-Francois Poilpret |
31 | */ |
32 | public class LoggingSessionListener implements HttpSessionListener |
33 | { |
34 | static private final Log _logger = LogFactory.getLog(LoggingSessionListener.class); |
35 | static private final String CREATE = "Session id <{0}> created (total sessions = {1})"; |
36 | static private final String DESTROY = "Session id <{0}> destroyed (lifetime = {2} ms; " + |
37 | "inactive during {3} ms; total sessions = {1})"; |
38 | |
39 | public void sessionCreated(HttpSessionEvent e) |
40 | { |
41 | _activeSessions++; |
42 | if (_logger.isDebugEnabled()) |
43 | { |
44 | HttpSession session = e.getSession(); |
45 | Object[] args = new Object[] {session.getId(), _activeSessions}; |
46 | String message = getCreationFormatter().format( |
47 | args, new StringBuffer(), null).toString(); |
48 | _logger.debug(message); |
49 | } |
50 | } |
51 | |
52 | public void sessionDestroyed(HttpSessionEvent e) |
53 | { |
54 | _activeSessions--; |
55 | if (_logger.isDebugEnabled()) |
56 | { |
57 | HttpSession session = e.getSession(); |
58 | long now = System.currentTimeMillis(); |
59 | long activeTime = now - session.getCreationTime(); |
60 | long lastTime = now - session.getLastAccessedTime(); |
61 | Object[] args = new Object[] { session.getId(), |
62 | _activeSessions, |
63 | activeTime, |
64 | lastTime}; |
65 | String message = getDestructionFormatter().format( |
66 | args, new StringBuffer(), null).toString(); |
67 | _logger.debug(message); |
68 | } |
69 | } |
70 | |
71 | protected MessageFormat getCreationFormatter() |
72 | { |
73 | if (_creationFormatter == null) |
74 | { |
75 | _creationFormatter = new MessageFormat(CREATE); |
76 | } |
77 | return _creationFormatter; |
78 | } |
79 | |
80 | protected MessageFormat getDestructionFormatter() |
81 | { |
82 | if (_destructionFormatter == null) |
83 | { |
84 | _destructionFormatter = new MessageFormat(DESTROY); |
85 | } |
86 | return _destructionFormatter; |
87 | } |
88 | |
89 | protected MessageFormat _creationFormatter; |
90 | protected MessageFormat _destructionFormatter; |
91 | protected int _activeSessions = 0; |
92 | } |