Posted on 2007-11-25 21:23
linlin yu 阅读(1028)
评论(0) 编辑 收藏 所属分类:
servlet
1,ServletContextListener
package com.soyoung.xxx.util;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
/** *//**
*
* @author linlinyu
* @version 1.0
* @web.listener
*/
public class XXXContext implements ServletContextListener {
private String mVersion = null;
private String mBuildTime = null;
private String mBuildUser = null;
public static final String XXX_CONTEXT = "xxxContext";
private static String version_config = "/version.properties";
private static ServletContext mContext = null;
private static Log mLogger = LogFactory.getLog(XXXContext.class);
private final SynchronizedInt mSessionCount = new SynchronizedInt(0);
public String getBuildTime() {
return mBuildTime;
}
public String getBuildUser() {
return mBuildUser;
}
public String getVersion() {
return mVersion;
}
public XXXContext() {
super();
Properties props = new Properties();
try {
props.load(getClass().getResourceAsStream(version_config));
} catch (Exception e) {
mLogger.error("version.properties not found", e);
}
mVersion = props.getProperty("xxx.version", "UNKNOWN");
mBuildTime = props.getProperty("xxx.buildTime", "UNKNOWN");
mBuildUser = props.getProperty("xxx.buildUser", "UNKNOWN");
}
public static XXXContext getXXXContextt() {
ServletContext sc = XXXContext.getServletContext();
return (XXXContext) sc.getAttribute(XXX_CONTEXT);
}
public static ServletContext getServletContext() {
return mContext;
}
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
public void contextInitialized(ServletContextEvent sce) {
mContext = sce.getServletContext();
mContext.setAttribute(XXX_CONTEXT, this);
}
public void sessionCreated(HttpSessionEvent se) {
mSessionCount.increment();
mLogger.debug("sessions=" + mSessionCount + ":freemem="
+ Runtime.getRuntime().freeMemory() + ":totmem="
+ Runtime.getRuntime().totalMemory());
}
public void sessionDestroyed(HttpSessionEvent se) {
mSessionCount.decrement();
mLogger.debug("sessions=" + mSessionCount + ":freemem="
+ Runtime.getRuntime().freeMemory() + ":totalmem="
+ Runtime.getRuntime().totalMemory());
}
public int getSessionCount() {
return mSessionCount.complement();
}
}
2,HttpSessionListener
package com.soyoung.xxx.util;
import java.io.Serializable;
import java.security.Principal;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/** *//**
*
* @author linlinyu
* @version 1.0
* @web.listener
*
*/
public class XXXSession implements HttpSessionListener,
HttpSessionActivationListener, Serializable {
public static final String XXX_SESSION = "com.soyoung.xxx.XXXSession";
private Object userData = null;
private HttpSession httpSession = null;
private static Log mLogger = LogFactory.getFactory().getInstance(
XXXSession.class);
public static XXXSession getXXXSession(HttpServletRequest request) {
XXXSession xxxSession = null;
HttpSession session = request.getSession(false);
if (xxxSession == null) {
xxxSession = new XXXSession();
session.setAttribute(XXX_SESSION, xxxSession);
}
Principal principal = request.getUserPrincipal();
if (xxxSession.getUserData() == null && principal != null) {
// Object user = findUserByName(principal);
// if(user != null){
// xxxSession.setUserData(user);
// }
}
return xxxSession;
}
public Object getSessionAttribute(String atrribue) {
return httpSession.getAttribute(atrribue);
}
public void sessionCreated(HttpSessionEvent se) {
XXXSession xxxSession = new XXXSession();
xxxSession.setHttpSession(se.getSession());
se.getSession().setAttribute(XXX_SESSION, xxxSession);
XXXContext ctx = XXXContext.getXXXContextt();
ctx.sessionCreated(se);
}
public void sessionDestroyed(HttpSessionEvent se) {
XXXContext ctx = XXXContext.getXXXContextt();
ctx.sessionDestroyed(se);
clearSession(se);
}
public void sessionDidActivate(HttpSessionEvent arg0) {
}
public void sessionWillPassivate(HttpSessionEvent se) {
clearSession(se);
}
private void clearSession(HttpSessionEvent se) {
HttpSession session = se.getSession();
try {
session.removeAttribute(XXX_SESSION);
} catch (Throwable e) {
if (mLogger.isDebugEnabled()) {
// ignore purge exceptions
mLogger.debug("EXCEPTION PURGING session attributes", e);
}
}
}
public Object getUserData() {
return userData;
}
public void setUserData(Object userData) {
this.userData = userData;
}
public HttpSession getHttpSession() {
return httpSession;
}
public void setHttpSession(HttpSession httpSession) {
this.httpSession = httpSession;
}
}