1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| package ui.filter;
import java.io.IOException;
import java.io.Serializable;
import java.util.Hashtable;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* J2EE "Filter" to count page hits. What it counts depends on the
url-mapping
* in web.xml.
*
* @author tim.lucia
*/
public class SessionCountFilter implements Filter, HttpSessionBindingListener, Serializable {
private final static Log logger = LogFactory.getLog(SessionCountFilter.class);
public static final Hashtable sessions = new Hashtable();
public static int sessionCountHighWater = 0;
/**
* Container startup notification
*/
public void init(FilterConfig arg0) throws ServletException
{
logger.debug("init(): " + arg0);
}
/**
* Container shutdown notification
*/
public void destroy()
{
logger.debug("destroy()");
}
/**
* Process the container's filter request.
* @param request - Request object
* @param response - response object
* @param chain - next filter in the chain.
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
chain.doFilter(request, response);
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpSession session = httpRequest.getSession(false);
if (logger.isDebugEnabled()) {
logger.debug("Request " + httpRequest.getRequestURI() +
(session == null ? " returned no session" :
" belongs to session ID " + session.getId()));
}
// Bind to the session, if there is one, and it is new:
if (null != session && session.isNew()) {
session.setAttribute(toString(), this);
session.setAttribute("nbreSession", sessions.size());
}
}
/**
* Implement HttpSessionBindingListener#valueBound
*/
public void valueBound(HttpSessionBindingEvent bindEvent)
{
HttpSession session = bindEvent.getSession();
final String sessionID = session.getId();
sessions.put(session, sessionID);
if (logger.isDebugEnabled()) {
logger.debug("[" + sessions.size() + "] CREATE: " +
sessionID);
}
sessionCountHighWater =
(sessionCountHighWater < sessions.size() ? sessions.size() :
sessionCountHighWater);
}
/**
* Implement HttpSessionBindingListener#valueUnbound
*/
public void valueUnbound(HttpSessionBindingEvent bindEvent)
{
HttpSession session = bindEvent.getSession();
final String sessionID = (String)sessions.get(session);
sessions.remove(session);
if (logger.isDebugEnabled()) {
logger.debug("[" + sessions.size() + "] DESTROY: " +sessionID);
}
}
/**
* Return current count of sessions
* @return The number of sessions currently tracked
*/
public static int getSessionCount()
{
return sessions.size();
}
/**
* Return high water mark of number of sessions
* @return The high water mark of sessions tracked
*/
public static int getSessionCountHighWater()
{
return sessionCountHighWater;
}
/**
* Return string representation of this object
* @return a String representation of this object
*/
public String toString()
{
return getClass().getName() + "#" + hashCode();
}
} |
Partager