通过对IBM websphere portal相关文档的研究,成功开发门户上的在线用户统计功能。
思路:借助于IBM Portal 的SiteAnalyzerLogger,定制其UserSessionEventListener实现达到用户登录、注销、超时处理。
功能:在线用户数、在线用户列表、峰值等。
由于前段时间电脑硬盘损坏,好多东西损失,千辛万苦,反编译之后附上。
public class OnlineUser {
private String loginName; private String commonName;
public OnlineUser(String loginName) { /* 12*/ this.loginName = loginName; }
public String getLoginName() { /* 16*/ return loginName; }
public void setLoginName(String loginName) { /* 20*/ this.loginName = loginName; }
public String getCommonName() { /* 25*/ return commonName; }
public void setCommonName(String commonName) { /* 29*/ this.commonName = commonName; }
public boolean equals(Object o) { /* 34*/ if (this == o) { /* 34*/ return true; } /* 35*/ if (o == null || getClass() != o.getClass()) { /* 35*/ return false; } /* 37*/ OnlineUser that = (OnlineUser)o; /* 39*/ return loginName == null ? that.loginName == null : loginName.equals(that.loginName); }
public int hashCode() { /* 45*/ return loginName == null ? 0 : loginName.hashCode(); }
public String toString() { /* 50*/ return commonName; } }
import cn.sh.ideal.dbpool.ConnectionPool; import com.ibm.portal.events.UserSessionEventListener; import com.ibm.portal.puma.User; import com.ibm.wps.services.siteanalyzer.SiteAnalyzerLogger; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.Timestamp; import java.util.Date; import java.util.HashSet; import java.util.Set; import java.util.Vector; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession;
public class PortalUserTotal extends SiteAnalyzerLogger implements UserSessionEventListener {
private static Vector vector = new Vector(); private static int most = 0;
public PortalUserTotal(String s) { super(s); }
public PortalUserTotal(String s, String s1) { /* 35*/ super(s, s1); }
public void end(HttpServletRequest httpservletrequest) { /* 41*/ HttpSession httpsession = httpservletrequest.getSession(false); /* 42*/ if (httpsession != null) { /* 44*/ try { /* 44*/ User user = (User)httpsession.getAttribute("com.ibm.wps.util.session_user"); /* 45*/ if (user != null) { /* 46*/ OnlineUser onlineUser = new OnlineUser(user.getName()); /* 47*/ vector.remove(onlineUser); } } /* 50*/ catch (Exception exception) { } } }
public void loginFailed(HttpServletRequest httpservletrequest, int ii) { /* 58*/ HttpSession httpsession = httpservletrequest.getSession(false); /* 59*/ if (httpsession != null) { /* 61*/ try { /* 61*/ User user = (User)httpsession.getAttribute("com.ibm.wps.util.session_user"); /* 62*/ if (user != null) { /* 63*/ OnlineUser onlineUser = new OnlineUser(user.getName()); /* 64*/ onlineUser.setCommonName(user.getCommonName()); /* 65*/ vector.add(onlineUser); } } /* 68*/ catch (Exception e) { } } }
public void start(HttpServletRequest httpservletrequest) { /* 78*/ HttpSession httpsession = httpservletrequest.getSession(false); /* 79*/ if (httpsession != null) { /* 81*/ try { /* 81*/ User user = (User)httpsession.getAttribute("com.ibm.wps.util.session_user"); /* 82*/ if (user != null) { /* 83*/ OnlineUser onlineUser = new OnlineUser(user.getName()); /* 84*/ onlineUser.setCommonName(user.getCommonName()); /* 85*/ vector.add(onlineUser); /* 86*/ String method = httpservletrequest.getMethod(); /* 87*/ if (method.equals("GET")) { /* 88*/ logToDB(user.getName(), httpservletrequest.getRemoteAddr(), "/u4F1A/u8BDD/u7EE7/u7EED"); } else { /* 90*/ logToDB(user.getName(), httpservletrequest.getRemoteAddr(), "/u7528/u6237/u767B/u5F55"); } /* 91*/ if (vector.size() > most) { /* 92*/ most++; } } } /* 95*/ catch (Exception e) { } } }
public void timedOut(HttpSession httpsession) { /* 105*/ if (httpsession != null) { /* 107*/ try { /* 107*/ User user = (User)httpsession.getAttribute("com.ibm.wps.util.session_user"); /* 108*/ if (user != null) { /* 109*/ OnlineUser onlineUser = new OnlineUser(user.getName()); /* 110*/ vector.remove(onlineUser); /* 111*/ logToDB(user.getName(), "0", "/u4F1A/u8BDD/u8D85/u65F6"); } } /* 113*/ catch (Exception exception) { } } }
public static void endSession(String userStr, String ip) { /* 119*/ OnlineUser onlineUser = new OnlineUser(userStr); /* 120*/ vector.remove(onlineUser); /* 121*/ logToDB(userStr, ip, "/u7528/u6237/u6CE8/u9500"); }
private static void logToDB(String loginname, String ip, String operate) { /* 125*/ Connection conn = null; /* 126*/ PreparedStatement stmt = null; /* 127*/ ConnectionPool connectionPool = ConnectionPool.getInstance(); /* 129*/ try { /* 129*/ Date date = new Date(); /* 130*/ Timestamp ts = new Timestamp(date.getTime()); /* 131*/ conn = connectionPool.getConnection(); /* 132*/ int i = 0; /* 134*/ try { /* 134*/ while (conn.isClosed() && i < 5) { /* 136*/ Thread.sleep(50L); /* 137*/ conn = connectionPool.getConnection(); /* 138*/ i++; } } /* 140*/ catch (Exception e) { /* 141*/ e.printStackTrace(); } /* 143*/ String sql = "insert into uman.t_portal_log (time,username,ip,actions) values (?,?,?,?)"; /* 144*/ stmt = conn.prepareStatement(sql); /* 145*/ stmt.setTimestamp(1, ts); /* 146*/ stmt.setString(2, loginname); /* 147*/ stmt.setString(3, ip); /* 148*/ stmt.setString(4, operate); /* 149*/ stmt.execute(); /* 150*/ stmt.close(); /* 151*/ stmt = null; } /* 152*/ catch (Exception e) { /* 153*/ e.printStackTrace(); } /* 155*/ finally { /* 155*/ if (stmt != null) { /* 156*/ try { /* 156*/ stmt.close(); /* 156*/ stmt = null; } /* 156*/ catch (Exception e) { } } /* 158*/ connectionPool.freeConnection(conn, true); } }
public static int getTotal() { /* 166*/ return vector.size(); }
public static Vector getUsersInfo() { /* 181*/ return (Vector)vector.clone(); }
public static Set getUserSetInfo() { /* 186*/ return new HashSet(vector); }
public static int getMost() { /* 190*/ return most; }
}