EMMA Coverage Report (generated Tue Feb 12 22:23:49 ICT 2008)
[all classes][net.sourceforge.hiveutils.web.util]

COVERAGE SUMMARY FOR SOURCE FILE [LoggingSessionListener.java]

nameclass, %method, %block, %line, %
LoggingSessionListener.java0%   (0/1)0%   (0/6)0%   (0/136)0%   (0/26)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class LoggingSessionListener0%   (0/1)0%   (0/6)0%   (0/136)0%   (0/26)
<static initializer> 0%   (0/1)0%   (0/4)0%   (0/1)
LoggingSessionListener (): void 0%   (0/1)0%   (0/6)0%   (0/2)
getCreationFormatter (): MessageFormat 0%   (0/1)0%   (0/12)0%   (0/3)
getDestructionFormatter (): MessageFormat 0%   (0/1)0%   (0/12)0%   (0/3)
sessionCreated (HttpSessionEvent): void 0%   (0/1)0%   (0/40)0%   (0/7)
sessionDestroyed (HttpSessionEvent): void 0%   (0/1)0%   (0/62)0%   (0/10)

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 
15package net.sourceforge.hiveutils.web.util;
16 
17import java.text.MessageFormat;
18 
19import javax.servlet.http.HttpSession;
20import javax.servlet.http.HttpSessionEvent;
21import javax.servlet.http.HttpSessionListener;
22 
23import org.apache.commons.logging.Log;
24import 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 */
32public 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}

[all classes][net.sourceforge.hiveutils.web.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov