web網站統計線上使用者
阿新 • • 發佈:2019-01-10
/*
* Copyright 2015 textile.com All right reserved. This software is the confidential and proprietary information of
* textile.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
* in accordance with the terms of the license agreement you entered into with textile.com.
*/
package com.az.textile.customer.account.validator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import com.az.textile.common.service.CommonService;
import com.az.textile.configfront.config.ConfigConstant;
import com.az.textile.configfront.controller.FrontGlobalController;
import com.jfinal.plugin.activerecord.Record;
/**
* 類SessionUserListener.java的實現描述:
*
* @author yangdong 2015年8月29日 下午2:13:09
*/
public class SessionUserListener extends FrontGlobalController implements HttpSessionListener {
// key為sessionId,value為HttpSession,使用static,定義靜態變數,使之程式執行時,一直存在記憶體中。
private static Map<String, HttpSession> sessionMap = new java.util.concurrent.ConcurrentHashMap<String, HttpSession>();
/**
* HttpSessionListener中的方法,在建立session
*/
@Override
public void sessionCreated(HttpSessionEvent event) {
// TODO Auto-generated method stub
}
/**
* HttpSessionListener中的方法,回收session時,刪除sessionMap中對應的session
*/
@Override
public void sessionDestroyed(HttpSessionEvent event) {
getSessionMap().remove(event.getSession().getId());
}
/**
* 得到線上使用者會話集合
*/
public static List<HttpSession> getUserSessions() {
List<HttpSession> list = new ArrayList<HttpSession>();
Iterator<String> iterator = getSessionMapKeySetIt();
while (iterator.hasNext()) {
String key = iterator.next();
HttpSession session = getSessionMap().get(key);
list.add(session);
}
return list;
}
/**
* 得到使用者對應會話map,key為使用者ID,value為會話ID
*/
public static Map<String, String> getUserSessionMap() {
Map<String, String> map = new HashMap<String, String>();
Iterator<String> iter = getSessionMapKeySetIt();
while (iter.hasNext()) {
String sessionId = iter.next();
HttpSession session = getSessionMap().get(sessionId);
Record record = (Record) session.getAttribute(CommonService.me.encryptSessionKeyToBase64(ConfigConstant.SESSION_REGISTER_USER_KEY));
if (record != null) {
map.put(record.getStr("uuid"), sessionId);
}
}
return map;
}
/**
* 移除使用者Session
*/
public synchronized static void removeUserSession(String userId) {
Map<String, String> userSessionMap = getUserSessionMap();
if (userSessionMap.containsKey(userId)) {
String sessionId = userSessionMap.get(userId);
getSessionMap().get(sessionId).invalidate();
getSessionMap().remove(sessionId);
}
}
/**
* 增加使用者到session集合中
*/
public static void addUserSession(HttpSession session) {
getSessionMap().put(session.getId(), session);
}
/**
* 移除一個session
*/
public static void removeSession(String sessionID) {
getSessionMap().remove(sessionID);
}
public static boolean containsKey(String key) {
return getSessionMap().containsKey(key);
}
/**
* 判斷該使用者是否已重複登入,使用 同步方法,只允許一個執行緒進入,才好驗證是否重複登入
*
* @param user
* @return
*/
public synchronized static boolean checkIfHasLogin(Record user) {
Iterator<String> iter = getSessionMapKeySetIt();
while (iter.hasNext()) {
String sessionId = iter.next();
HttpSession session = getSessionMap().get(sessionId);
Record sessionuser = (Record) session.getAttribute(CommonService.me.encryptSessionKeyToBase64(ConfigConstant.SESSION_REGISTER_USER_KEY));
if (sessionuser != null) {
if (sessionuser.getStr("uuid").equals(user.getStr("uuid"))) {
return true;
}
}
}
return false;
}
/**
* 獲取線上的sessionMap
*/
public static Map<String, HttpSession> getSessionMap() {
return sessionMap;
}
/**
* 獲取線上sessionMap中的SessionId
*/
public static Iterator<String> getSessionMapKeySetIt() {
return getSessionMap().keySet().iterator();
}
}
* Copyright 2015 textile.com All right reserved. This software is the confidential and proprietary information of
* textile.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
* in accordance with the terms of the license agreement you entered into with textile.com.
*/
package com.az.textile.customer.account.validator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import com.az.textile.common.service.CommonService;
import com.az.textile.configfront.config.ConfigConstant;
import com.az.textile.configfront.controller.FrontGlobalController;
import com.jfinal.plugin.activerecord.Record;
/**
* 類SessionUserListener.java的實現描述:
*
* @author yangdong 2015年8月29日 下午2:13:09
*/
public class SessionUserListener extends FrontGlobalController implements HttpSessionListener {
// key為sessionId,value為HttpSession,使用static,定義靜態變數,使之程式執行時,一直存在記憶體中。
private static Map<String, HttpSession> sessionMap = new java.util.concurrent.ConcurrentHashMap<String, HttpSession>();
/**
* HttpSessionListener中的方法,在建立session
*/
@Override
public void sessionCreated(HttpSessionEvent event) {
// TODO Auto-generated method stub
}
/**
* HttpSessionListener中的方法,回收session時,刪除sessionMap中對應的session
*/
@Override
public void sessionDestroyed(HttpSessionEvent event) {
getSessionMap().remove(event.getSession().getId());
}
/**
* 得到線上使用者會話集合
*/
public static List<HttpSession> getUserSessions() {
List<HttpSession> list = new ArrayList<HttpSession>();
Iterator<String> iterator = getSessionMapKeySetIt();
while (iterator.hasNext()) {
String key = iterator.next();
HttpSession session = getSessionMap().get(key);
list.add(session);
}
return list;
}
/**
* 得到使用者對應會話map,key為使用者ID,value為會話ID
*/
public static Map<String, String> getUserSessionMap() {
Map<String, String> map = new HashMap<String, String>();
Iterator<String> iter = getSessionMapKeySetIt();
while (iter.hasNext()) {
String sessionId = iter.next();
HttpSession session = getSessionMap().get(sessionId);
Record record = (Record) session.getAttribute(CommonService.me.encryptSessionKeyToBase64(ConfigConstant.SESSION_REGISTER_USER_KEY));
if (record != null) {
map.put(record.getStr("uuid"), sessionId);
}
}
return map;
}
/**
* 移除使用者Session
*/
public synchronized static void removeUserSession(String userId) {
Map<String, String> userSessionMap = getUserSessionMap();
if (userSessionMap.containsKey(userId)) {
String sessionId = userSessionMap.get(userId);
getSessionMap().get(sessionId).invalidate();
getSessionMap().remove(sessionId);
}
}
/**
* 增加使用者到session集合中
*/
public static void addUserSession(HttpSession session) {
getSessionMap().put(session.getId(), session);
}
/**
* 移除一個session
*/
public static void removeSession(String sessionID) {
getSessionMap().remove(sessionID);
}
public static boolean containsKey(String key) {
return getSessionMap().containsKey(key);
}
/**
* 判斷該使用者是否已重複登入,使用 同步方法,只允許一個執行緒進入,才好驗證是否重複登入
*
* @param user
* @return
*/
public synchronized static boolean checkIfHasLogin(Record user) {
Iterator<String> iter = getSessionMapKeySetIt();
while (iter.hasNext()) {
String sessionId = iter.next();
HttpSession session = getSessionMap().get(sessionId);
Record sessionuser = (Record) session.getAttribute(CommonService.me.encryptSessionKeyToBase64(ConfigConstant.SESSION_REGISTER_USER_KEY));
if (sessionuser != null) {
if (sessionuser.getStr("uuid").equals(user.getStr("uuid"))) {
return true;
}
}
}
return false;
}
/**
* 獲取線上的sessionMap
*/
public static Map<String, HttpSession> getSessionMap() {
return sessionMap;
}
/**
* 獲取線上sessionMap中的SessionId
*/
public static Iterator<String> getSessionMapKeySetIt() {
return getSessionMap().keySet().iterator();
}
}