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
8 * hangum - initial API and implementation
9 ******************************************************************************/
10 package com
.hangum
.tadpole
.engine
.utils
;
12 import java
.util
.HashMap
;
15 import java
.util
.concurrent
.ConcurrentHashMap
;
17 import javax
.servlet
.http
.HttpSession
;
19 import org
.apache
.log4j
.Logger
;
21 import com
.hangum
.tadpole
.session
.manager
.SessionManager
;
24 * 사용자 session 을 저장하고 관리하는 유틸 클래스이다.
29 public class HttpSessionCollectorUtil
{
30 private static final Logger logger
= Logger
.getLogger(HttpSessionCollectorListener
.class);
31 private static HttpSessionCollectorUtil sessionCollector
= null;//new HttpSessionCollectorUtil();
32 public static enum COLLECT_KEY
{SESSION
, TIMEOUT
};
34 /** id, httpsession */
35 private final Map
<String
, Map
<String
, Object
>> mapSession
= new ConcurrentHashMap
<String
, Map
<String
, Object
>>();
37 private HttpSessionCollectorUtil() {};
44 public static HttpSessionCollectorUtil
getInstance() {
45 if(sessionCollector
== null) {
46 sessionCollector
= new HttpSessionCollectorUtil();
48 Thread httpSessionChecker
= new Thread(new SessionLiveChecker(), "Tadpole_HttpSessionChecker");
49 httpSessionChecker
.start();
51 return sessionCollector
;
59 * @param intMinuteTimeOut
61 public void sessionCreated(String id
, HttpSession session
, int intMinuteTimeOut
) {
62 if(logger
.isDebugEnabled()) logger
.debug(String
.format("---> [login]%s->%s", id
, session
.getId()));
64 Map
<String
, Object
> mapUserData
= new HashMap
<String
, Object
>();
65 mapUserData
.put(COLLECT_KEY
.SESSION
.name(), session
);
66 mapUserData
.put(COLLECT_KEY
.TIMEOUT
.name(), intMinuteTimeOut
);
68 mapSession
.put(id
, mapUserData
);
76 public void sessionDestroyed(final String strEmail
) {
77 Map
<String
, Object
> mapUserData
= mapSession
.remove(strEmail
);
80 HttpSession httpSesssion
= (HttpSession
)mapUserData
.get(COLLECT_KEY
.SESSION
.name());
81 httpSesssion
.invalidate();
82 } catch(Throwable e
) {
83 logger
.error(String
.format("System invalidate user %s, messages %s", strEmail
, e
.getMessage()));
85 if(logger
.isDebugEnabled()) logger
.debug("========= remove connection start " + strEmail
);
86 SessionManager
.removeConnection(strEmail
);
87 if(logger
.isDebugEnabled()) logger
.debug("========= remove connection end ");
97 public Map
<String
, Map
<String
, Object
>> getSessions() {
107 public Map
<String
, Object
> find(String strEmail
) {
108 return mapSession
.get(strEmail
);
117 public HttpSession
findSession(String strEmail
) {
118 Map
<String
, Object
> mapUserData
= find(strEmail
);
119 if(mapUserData
!= null) {
120 return (HttpSession
)mapUserData
.get(COLLECT_KEY
.SESSION
.name());
127 * HttpSession live checker
131 class SessionLiveChecker
implements Runnable
{
132 private static final Logger logger
= Logger
.getLogger(HttpSessionCollectorListener
.class);
134 public SessionLiveChecker() {
141 // // 10 분에 한번씩 Thread 검사.
142 // try { Thread.sleep((60 * 1000) * 1); } catch(Exception e) {};
144 Map
<String
, Map
<String
, Object
>> allUserSession
= HttpSessionCollectorUtil
.getInstance().getSessions();
145 Set
<String
> keys
= allUserSession
.keySet();
146 for(String id
: keys
) {
147 Map
<String
, Object
> mapUserData
= allUserSession
.get(id
);
148 HttpSession httpSession
= (HttpSession
)mapUserData
.get(HttpSessionCollectorUtil
.COLLECT_KEY
.SESSION
.name());
149 Integer intTimeOut
= (Integer
)mapUserData
.get(HttpSessionCollectorUtil
.COLLECT_KEY
.TIMEOUT
.name());
150 long userTime
= intTimeOut
* 60 * 1000;
152 long gapTime
= System
.currentTimeMillis() - httpSession
.getLastAccessedTime();
154 if(logger
.isDebugEnabled()) {
155 logger
.debug("=========== session live checker ===============");
156 logger
.debug(String
.format("[session user] id=%s", id
));
157 logger
.debug(String
.format("[userTime]%s[gapTime]%s", userTime
, gapTime
));
158 logger
.debug("=========== session live checker ===============");
161 if(gapTime
> userTime
) {
162 if(logger
.isDebugEnabled()) logger
.debug("[session invalidate is ]" + id
);
163 HttpSessionCollectorUtil
.getInstance().sessionDestroyed(id
);
166 // session 이 만료되어서 시간을 가져 올수 없는 상태이므로 세션과 커넥션을 처리합니다.
167 } catch(IllegalStateException e
) {
168 if(logger
.isDebugEnabled()) logger
.debug("[ise][session invalidate is ]" + id
);
169 HttpSessionCollectorUtil
.getInstance().sessionDestroyed(id
);
173 // 엔진의 설정 정보를 디비에서 가져와서 동기화(?)
174 // TDDO 여기서 해야하나도 싶고요.(hangum)
175 // final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
176 // shell.getDisplay().syncExec(new Runnable() {
178 // public void run() {
180 // TadpoleApplicationContextManager.initAdminSystemSetting();
181 // } catch(Exception e) {
182 // logger.error("re initialize system setting", e);
187 // 10 분에 한번씩 Thread 검사.
188 try { Thread
.sleep((60 * 1000) * 10); } catch(Exception e
) {};