?? sessionuserobject.java
字號:
/*
* SessionUserObject.java
*
* 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
* (at your option) any later version.
*
* 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 Library 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.
*
* Author: Winter Lau (javayou@gmail.com)
* http://dlog4j.sourceforge.net
*/
package com.liusoft.dlog4j;
import java.sql.Date;
import java.sql.Timestamp;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.liusoft.dlog4j.base.ContactInfo;
import com.liusoft.dlog4j.base.CountInfo;
import com.liusoft.dlog4j.base._UserBeanBase;
import com.liusoft.dlog4j.beans.UserBean;
import com.liusoft.dlog4j.dao.FCKUploadFileDAO;
import com.liusoft.dlog4j.db.HibernateUtils;
/**
* 記錄在會話中的用戶基本資料
* @see com.liusoft.dlog4j.beans.UserBean
* @author Winter Lau
*/
public class SessionUserObject extends _UserBeanBase implements HttpSessionBindingListener{
private final static Log log = LogFactory.getLog(SessionUserObject.class);
/* session 相關的信息,跟bean無關 */
private String sessionId;
/**
* 從PO對象中復制一份數據,克隆
* @param bean
* @return
*/
public static SessionUserObject copyFrom(UserBean bean){
SessionUserObject user = new SessionUserObject();
user.setId(bean.getId());
user.setName(bean.getName());
user.setNickname(bean.getNickname());
user.setSex(bean.getSex());
if(bean.getBirth()!=null)
user.setBirth((Date)bean.getBirth().clone());
if(bean.getContactInfo()!=null)
user.setContactInfo((ContactInfo)bean.getContactInfo().clone());
if(bean.getCount()!=null)
user.setCount((CountInfo)bean.getCount().clone());
user.setResume(bean.getResume());
user.setRegTime(new Timestamp(bean.getRegTime().getTime()));
if(bean.getLastTime()!=null)
user.setLastTime(new Timestamp(bean.getLastTime().getTime()));
user.setLastAddr(bean.getLastAddr());
user.setStatus(bean.getStatus());
user.setKeepDays(bean.getKeepDays());
user.setOwnSiteId(bean.getOwnSiteId());
user.setPortrait(bean.getPortrait());
user.setRole(bean.getRole());
return user;
}
/**
* 保存session_id防止某些應用服務器會話實效后無法獲取session_id
*/
public void valueBound(HttpSessionBindingEvent e) {
this.sessionId = e.getSession().getId();
}
/**
* 執行用戶注銷方法
* 由于該方法是由應用服務器調用的,不經過HibernateFilter,因此必須手動關閉Session
*/
public void valueUnbound(HttpSessionBindingEvent e) {
SessionUserObject user = (SessionUserObject)e.getValue();
if(user != null){
try{
UserLoginManager.logoutUser(user);
}catch(Exception excp){
log.error("Error when logout user, userid="+user.getId(), excp);
}
try{
//清除dlog_fck_upload_file的相關文件
FCKUploadFileDAO.cleanupOfSession(user.getSessionId(), user.getId());
}catch(Exception excp){
log.error("Error when cleanup upload files, userid="+user.getId(), excp);
}
//此處由于不受HibernateFilter控制,因此需要顯式的來關閉數據庫資源
HibernateUtils.closeSession();
}
}
private String getSessionId() {
return sessionId;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -