사용자가 api 를 만들수 있는지 여부를 설정 하도록 개선.
[Tadpole.git] / com.hangum.tadpole.commons.sql / src / com / hangum / tadpole / engine / utils / HttpSessionCollectorUtil.java
blob9f11a6a4e28225738f57bc035194c87ab2d6a18c
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.HashMap;
13 import java.util.Map;
14 import java.util.Set;
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;
23 /**
24 * 사용자 session 을 저장하고 관리하는 유틸 클래스이다.
26 * @author hangum
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() {};
39 /**
40 * get instance
42 * @return
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;
54 /**
55 * session created
57 * @param id
58 * @param session
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);
71 /**
72 * session destoryed
74 * @param id
76 public void sessionDestroyed(final String strEmail) {
77 Map<String, Object> mapUserData = mapSession.remove(strEmail);
79 try {
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()));
84 } finally {
85 if(logger.isDebugEnabled()) logger.debug("========= remove connection start " + strEmail);
86 SessionManager.removeConnection(strEmail);
87 if(logger.isDebugEnabled()) logger.debug("========= remove connection end ");
92 /**
93 * get all session
95 * @return
97 public Map<String, Map<String, Object>> getSessions() {
98 return mapSession;
102 * find id to session
104 * @param id
105 * @return
107 public Map<String, Object> find(String strEmail) {
108 return mapSession.get(strEmail);
112 * find session
114 * @param strEmail
115 * @return
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());
122 return null;
127 * HttpSession live checker
128 * @author hangum
131 class SessionLiveChecker implements Runnable {
132 private static final Logger logger = Logger.getLogger(HttpSessionCollectorListener.class);
134 public SessionLiveChecker() {
137 @Override
138 public void run() {
140 while(true) {
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;
151 try {
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() {
177 // @Override
178 // public void run() {
179 // try{
180 // TadpoleApplicationContextManager.initAdminSystemSetting();
181 // } catch(Exception e) {
182 // logger.error("re initialize system setting", e);
183 // }
184 // }
185 // });
187 // 10 분에 한번씩 Thread 검사.
188 try { Thread.sleep((60 * 1000) * 10); } catch(Exception e) {};
189 } // while