최신버전에 맞는 타겟플렛폼 설정.
[Tadpole.git] / com.hangum.tadpole.commons.sql / src / com / hangum / tadpole / engine / utils / HttpSessionCollectorUtil.java
blobce4cbbbb3aad0136ed4b88eff73348e074503df9
1 /*******************************************************************************
2 * All rights reserved. This program and the accompanying materials
3 * are made available under the terms of the GNU Lesser Public License v2.1
4 * which accompanies this distribution, and is available at
5 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6 *
7 * Contributors:
8 * hangum - initial API and implementation
9 ******************************************************************************/
10 package com.hangum.tadpole.engine.utils;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.concurrent.ConcurrentHashMap;
19 import javax.servlet.http.HttpSession;
21 import org.apache.log4j.Logger;
23 import com.hangum.tadpole.session.manager.SessionManager;
25 /**
26 * 사용자 session 을 저장하고 관리하는 유틸 클래스이다.
28 * @author hangum
31 public class HttpSessionCollectorUtil {
32 private static final Logger logger = Logger.getLogger(HttpSessionCollectorListener.class);
33 private static HttpSessionCollectorUtil sessionCollector = null;//new HttpSessionCollectorUtil();
34 public static enum COLLECT_KEY {SESSION, TIMEOUT};
36 /** id, httpsession */
37 private final Map<String, Map<String, Object>> mapSession = new ConcurrentHashMap<String, Map<String, Object>>();
39 private HttpSessionCollectorUtil() {};
41 /**
42 * get instance
44 * @return
46 public static HttpSessionCollectorUtil getInstance() {
47 if(sessionCollector == null) {
48 sessionCollector = new HttpSessionCollectorUtil();
50 Thread httpSessionChecker = new Thread(new SessionLiveChecker(), "Tadpole_HttpSessionChecker");
51 httpSessionChecker.start();
53 return sessionCollector;
56 /**
57 * session created
59 * @param id
60 * @param session
61 * @param intMinuteTimeOut
63 public void sessionCreated(String id, HttpSession session, int intMinuteTimeOut) {
64 if(logger.isDebugEnabled()) logger.debug(String.format("---> [login]%s->%s", id, session.getId()));
66 Map<String, Object> mapUserData = new HashMap<String, Object>();
67 mapUserData.put(COLLECT_KEY.SESSION.name(), session);
68 mapUserData.put(COLLECT_KEY.TIMEOUT.name(), intMinuteTimeOut);
70 mapSession.put(id, mapUserData);
73 /**
74 * session destoryed
76 * @param id
78 public void sessionDestroyed(final String strEmail) {
79 Map<String, Object> mapUserData = mapSession.remove(strEmail);
81 try {
82 HttpSession httpSesssion = (HttpSession)mapUserData.get(COLLECT_KEY.SESSION.name());
83 httpSesssion.invalidate();
84 } catch(Throwable e) {
85 logger.error(String.format("System invalidate user %s, messages %s", strEmail, e.getMessage()));
86 } finally {
87 if(logger.isDebugEnabled()) logger.debug("========= remove connection start " + strEmail);
88 SessionManager.removeConnection(strEmail);
89 if(logger.isDebugEnabled()) logger.debug("========= remove connection end ");
94 /**
95 * get all session
97 * @return
99 public Map<String, Map<String, Object>> getSessions() {
100 return mapSession;
104 * find id to session
106 * @param id
107 * @return
109 public Map<String, Object> find(String strEmail) {
110 return mapSession.get(strEmail);
114 * find session
116 * @param strEmail
117 * @return
119 public HttpSession findSession(String strEmail) {
120 Map<String, Object> mapUserData = find(strEmail);
121 if(mapUserData != null) {
122 return (HttpSession)mapUserData.get(COLLECT_KEY.SESSION.name());
124 return null;
129 * HttpSession live checker
130 * @author hangum
133 class SessionLiveChecker implements Runnable {
134 private static final Logger logger = Logger.getLogger(HttpSessionCollectorListener.class);
136 public SessionLiveChecker() {
139 @Override
140 public void run() {
142 while(true) {
143 // // 10 분에 한번씩 Thread 검사.
144 // try { Thread.sleep((60 * 1000) * 1); } catch(Exception e) {};
146 Map<String, Map<String, Object>> allUserSession = HttpSessionCollectorUtil.getInstance().getSessions();
147 final List<String> listIDs = new ArrayList<String>(allUserSession.keySet());
148 for (String id : listIDs) {
149 Map<String, Object> mapUserData = allUserSession.get(id);
150 HttpSession httpSession = (HttpSession)mapUserData.get(HttpSessionCollectorUtil.COLLECT_KEY.SESSION.name());
151 Integer intTimeOut = (Integer)mapUserData.get(HttpSessionCollectorUtil.COLLECT_KEY.TIMEOUT.name());
152 long userTime = intTimeOut * 60 * 1000;
153 try {
154 long gapTime = System.currentTimeMillis() - httpSession.getLastAccessedTime();
156 if(logger.isDebugEnabled()) {
157 logger.debug("=========== session live checker ===============");
158 logger.debug(String.format("[session user] id=%s", id));
159 logger.debug(String.format("[userTime]%s[gapTime]%s", userTime, gapTime));
160 logger.debug("=========== session live checker ===============");
163 if(gapTime > userTime) {
164 if(logger.isDebugEnabled()) logger.debug("[session invalidate is ]" + id);
165 HttpSessionCollectorUtil.getInstance().sessionDestroyed(id);
168 // session 이 만료되어서 시간을 가져 올수 없는 상태이므로 세션과 커넥션을 처리합니다.
169 } catch(IllegalStateException e) {
170 if(logger.isDebugEnabled()) logger.debug("[ise][session invalidate is ]" + id);
171 HttpSessionCollectorUtil.getInstance().sessionDestroyed(id);
175 // 엔진의 설정 정보를 디비에서 가져와서 동기화(?)
176 // TDDO 여기서 해야하나도 싶고요.(hangum)
177 // final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
178 // shell.getDisplay().syncExec(new Runnable() {
179 // @Override
180 // public void run() {
181 // try{
182 // TadpoleApplicationContextManager.initAdminSystemSetting();
183 // } catch(Exception e) {
184 // logger.error("re initialize system setting", e);
185 // }
186 // }
187 // });
189 // 10 분에 한번씩 Thread 검사.
190 try { Thread.sleep((60 * 1000) * 10); } catch(Exception e) {};
191 } // while