📄 onlineusermanager.java
字号:
/*
* Copyright (C) 2002 by MyVietnam.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* All copyright notices regarding mvnForum
* must remain intact in the scripts and in the outputted HTML
* The "powered by" text/logo with a link back to
* http://www.mvnForum.com and http://www.MyVietnam.net in the footer of the pages MUST
* remain visible when the pages are viewed on the internet or intranet.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Support can be obtained from support forums at:
* http://www.mvnForum.com/mvnforum/index
*
* Correspondence and Marketing Questions can be sent to:
* info@MyVietnam.net
*
* @author: Minh Nguyen minhnn@MyVietnam.net
* @author: Mai Nguyen mai.nh@MyVietnam.net
*/
package net.myvietnam.mvnplugin.mvnforum.auth;
import java.util.*;
import java.sql.Timestamp;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import net.myvietnam.mvncore.exception.DatabaseException;
import net.myvietnam.mvncore.exception.AssertionException;
import net.myvietnam.mvncore.exception.BadInputException;
import net.myvietnam.mvncore.exception.NotLoginException;
import net.myvietnam.mvncore.util.ParamUtil;
import net.myvietnam.mvncore.util.DateUtil;
import net.myvietnam.mvncore.security.Encoder;
import net.myvietnam.mvnplugin.mvnforum.MVNForumConfig;
public class OnlineUserManager {
private static final int REMOVE_INTERVAL = 2000; //update every 2 second
private static final String MVNFORUM_SESSION_USERNAME = "mvnforum.membername";
private static final String MVNFORUM_SESSION_PASSWORD = "mvnforum.encodedpassword";
//static variable
private static OnlineUserManager instance = new OnlineUserManager();
//instance variable
private Map userMap = new TreeMap();
private long timeOfLastRemoveAction = 0;
private transient Vector onlineUserListeners;
private OnlineUserManager() {
}
public static OnlineUserManager getInstance() {
return instance;
}
/**
* Util method to be called from Processor.
* It assumes that to input parameters are
* MemberName for username
* MemberMatkhau for password
*/
public void processLogin(HttpServletRequest request)
throws AuthenticationException, AssertionException, DatabaseException, BadInputException {
String memberName = ParamUtil.getParameter(request, "MemberName", true);
String memberPassword = ParamUtil.getParameter(request, "MemberMatkhau", true);
String encodedPassword = Encoder.getMD5_Base64(memberPassword);
try {
login(request, memberName, encodedPassword);
} catch (AuthenticationException ex) {
if (ex.getReason() == NotLoginException.WRONG_PASSWORD) {
request.setAttribute("MemberName", memberName);// so user dont have to retype USER NAME
}
throw ex;
}
}
/**
* NOTE: This method MUST be the only way to authenticate a user
*/
public OnlineUser login(HttpServletRequest request, String memberName, String encodedPassword)
throws AuthenticationException, DatabaseException, AssertionException {
OnlineUser user = OnlineUserFactory.getAuthenticatedUser(memberName, encodedPassword);
HttpSession session = request.getSession();
String sessionID = session.getId();
setOnlineUser(sessionID, user);
// now save the login info in the session
session.setAttribute(MVNFORUM_SESSION_USERNAME, memberName);
session.setAttribute(MVNFORUM_SESSION_PASSWORD, encodedPassword);
return user;
}
public void logout(HttpServletRequest request)
throws DatabaseException, AssertionException {
OnlineUser user = OnlineUserFactory.getAnonymousUser();
HttpSession session = request.getSession();
String sessionID = session.getId();
setOnlineUser(sessionID, user);
}
public OnlineUser getOnlineUser(HttpServletRequest request)
throws AuthenticationException, DatabaseException, AssertionException {
long currentTime = System.currentTimeMillis();
if (currentTime - timeOfLastRemoveAction > REMOVE_INTERVAL) {//update every minute
removeTimeoutUsers();
// update MostOnline here
int currentOnlineUserCount = userMap.size();
fireDataChanged(new OnlineUserEvent(this, currentOnlineUserCount));
}
HttpSession session = request.getSession();
String sessionID = session.getId();
OnlineUser user = getOnlineUser(sessionID);
if (user == null) {
boolean haveLoginInfoInCookie = false;
boolean haveLoginInfoInSession = false;
String memberName = null;
String encodedPassword = null;
memberName = ParamUtil.getAttribute(session, MVNFORUM_SESSION_USERNAME);
encodedPassword = ParamUtil.getAttribute(session, MVNFORUM_SESSION_PASSWORD);
if ( (memberName.length() > 0) && (encodedPassword.length() > 0) ) {
haveLoginInfoInSession = true;
}
if (haveLoginInfoInSession) {
user = login(request, memberName, encodedPassword);
} else if (haveLoginInfoInCookie) {
user = login(request, memberName, encodedPassword);
} else {
user = OnlineUserFactory.getAnonymousUser();
setOnlineUser(sessionID, user);
}
}
user.getOnlineUserAction().updateLastRequestTime();
return user;
}
public Collection getOnlineUserActions(int sortOption) {
Collection collection = userMap.values();
// @todo: find a better sollution, I dont want to copy the Collection
// this ArrayList is used to copy the values
ArrayList retValue = new ArrayList(collection.size());
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
OnlineUser onlineUser = (OnlineUser)iterator.next();
OnlineUserAction onlineUserAction = onlineUser.getOnlineUserAction();
retValue.add(onlineUserAction);
}
return retValue;
}
public void updateOnlineUserAction(HttpServletRequest request, Action action)
throws AssertionException, DatabaseException, AuthenticationException {
if (action != null) {
OnlineUser onlineUser = getOnlineUser(request);
OnlineUserAction onlineUserAction = onlineUser.getOnlineUserAction();
// the setAction has package-default access
// so this is the only way to set action to a user
onlineUserAction.setAction(action);
}
}
/************************************************************************
* private methods
************************************************************************/
private synchronized OnlineUser getOnlineUser(String sessionID) {
return (OnlineUser)userMap.get(sessionID);
}
private synchronized void setOnlineUser(String sessionID, OnlineUser user) {
userMap.put(sessionID, user);
}
private synchronized void removeTimeoutUsers() {
long currentTimeMillis = System.currentTimeMillis();
// try to resolve problem with synchronization on the class-varible : timeOfLastRemoveAction
if (currentTimeMillis - timeOfLastRemoveAction < REMOVE_INTERVAL) {
return;
}
// okie now, go ahead
timeOfLastRemoveAction = currentTimeMillis;
Timestamp currentTime = DateUtil.getCurrentGMTTimestamp();
Collection collection = userMap.values();
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
OnlineUser onlineUser = (OnlineUser)iterator.next();
OnlineUserAction onlineUserAction = onlineUser.getOnlineUserAction();
long duration = currentTime.getTime() - onlineUserAction.getLastRequestTime().getTime();
if (duration > MVNForumConfig.SESSION_DURATION) {
iterator.remove();
}
}
}
/************************************************************************
* Event method
************************************************************************/
public synchronized void removeOnlineUserListener(OnlineUserListener l) {
if (onlineUserListeners != null && onlineUserListeners.contains(l)) {
Vector v = (Vector) onlineUserListeners.clone();
v.removeElement(l);
onlineUserListeners = v;
}
}
public synchronized void addOnlineUserListener(OnlineUserListener l) {
Vector v = onlineUserListeners == null ? new Vector(2) : (Vector) onlineUserListeners.clone();
if (!v.contains(l)) {
v.addElement(l);
onlineUserListeners = v;
}
}
protected void fireDataChanged(OnlineUserEvent e) {
if (onlineUserListeners != null) {
Vector listeners = onlineUserListeners;
int count = listeners.size();
for (int i = 0; i < count; i++) {
((OnlineUserListener) listeners.elementAt(i)).dataChanged(e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -