- initialize netezza
[Tadpole.git] / com.hangum.tadpole.commons.sql / src / com / hangum / tadpole / session / manager / SessionManager.java
blob859eba1bd7a3e5124985151efa77e6c9b47a58d2
1 /*******************************************************************************
2 * Copyright (c) 2013 hangum.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser Public License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7 *
8 * Contributors:
9 * hangum - initial API and implementation
10 ******************************************************************************/
11 package com.hangum.tadpole.session.manager;
13 import java.sql.Timestamp;
14 import java.text.MessageFormat;
15 import java.util.ArrayList;
16 import java.util.Enumeration;
17 import java.util.List;
18 import java.util.Map;
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpSession;
22 import javax.servlet.http.HttpSessionContext;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.log4j.Logger;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.core.runtime.jobs.Job;
30 import org.eclipse.rap.rwt.RWT;
31 import org.eclipse.rap.rwt.client.service.JavaScriptExecutor;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.PlatformUI;
35 import com.hangum.tadpole.commons.libs.core.define.PublicTadpoleDefine;
36 import com.hangum.tadpole.engine.TadpoleEngineActivator;
37 import com.hangum.tadpole.engine.manager.TadpoleSQLManager;
38 import com.hangum.tadpole.engine.manager.TadpoleSQLTransactionManager;
39 import com.hangum.tadpole.engine.query.dao.ManagerListDTO;
40 import com.hangum.tadpole.engine.query.dao.system.UserDAO;
41 import com.hangum.tadpole.engine.query.dao.system.UserDBDAO;
42 import com.hangum.tadpole.engine.query.dao.system.UserInfoDataDAO;
43 import com.hangum.tadpole.engine.query.sql.TadpoleSystem_UserInfoData;
44 import com.hangum.tadpole.engine.utils.HttpSessionCollectorUtil;
46 /**
47 * tadpole의 session manager입니다
49 * @author hangum
52 public class SessionManager {
53 /**
54 * Logger for this class
56 private static final Logger logger = Logger.getLogger(SessionManager.class);
58 /** login ip를 가져온 경로를 지정한다 */
59 public static enum LOGIN_IP_TYPE {BROWSER_IP, SERVLET_REQUEST};
61 /**
62 * <pre>
63 * MANAGER_SEQ는 그룹의 manager 권한 사용자의 seq 입니다. seq로 그룹의 db list를 얻기위해 미리 가져옵니다.
64 * </pre>
66 * @author hangum
68 public static enum NAME {
69 /** webrtc, request.getRemoteAddrress */
70 LOGIN_IP_TYPE,
71 LOGIN_IP,
73 /* 자신의 유저 seq */ USER_SEQ,
74 LOGIN_EMAIL,
75 // LOGIN_PASSWORD,
76 LOGIN_NAME,
77 IS_REGIST_DB,
79 IS_SHARED_DB,
80 LIMIT_ADD_DB_CNT,
81 IS_MODIFY_PERFERENCE,
82 SERVICE_END,
83 LANGUAGE,
84 TIMEZONE,
86 /* 대표적인 권한 타입 */ REPRESENT_ROLE_TYPE,
87 USER_INFO_DATA,
89 USE_OTP, OTP_SECRET_KEY,
91 UNLOCK_DB_LIST,
93 PERSPECTIVE,
95 ALL_MANAGER_DB_LIST
97 /**
98 * UserManager Object list를 설정한다.
100 * @param managerDTO
102 public static void initManagerDBList() {
103 HttpSession sStore = RWT.getRequest().getSession();
104 sStore.setAttribute(NAME.ALL_MANAGER_DB_LIST.name(), new ArrayList<ManagerListDTO>());
108 * UserManager Object list를 설정한다.
110 * @param managerDTO
112 public static void setManagerDBList(List<ManagerListDTO> listManagerDTO) {
113 HttpSession sStore = RWT.getRequest().getSession();
114 sStore.setAttribute(NAME.ALL_MANAGER_DB_LIST.name(), listManagerDTO);
118 * UserManager object list를 가져온다.
120 * @return
122 public static List<ManagerListDTO> getManagerDBList() {
123 HttpSession sStore = RWT.getRequest().getSession();
124 Object listObj = sStore.getAttribute(NAME.ALL_MANAGER_DB_LIST.name());
125 if(listObj == null) {
126 return new ArrayList<ManagerListDTO>();
127 } else {
128 return (List<ManagerListDTO>)listObj;
133 * is login?
135 * @return
137 public static boolean isLogin() {
138 if(getUserSeq() == 0) return false;
139 else return true;
143 * Update session information.<br>
144 * <br>
145 * Session uses the information in multiple places(preference, user info etc.).
146 * So when updating the information stored in the Session,
147 * you must update the information given session.
149 * @param key Session Attribute name
150 * @param value Object
152 public static void updateSessionAttribute(String key, Object value) {
153 HttpSession sStore = RWT.getRequest().getSession();
154 sStore.setAttribute(key, value);
158 * 사용자를 session에 등록
160 * @param userDao
161 * @param loginType
162 * @param ip
164 public static void addSession(UserDAO userDao, String loginType, String ip) {
165 HttpSession sStore = RWT.getRequest().getSession();
167 sStore.setAttribute(NAME.LOGIN_IP_TYPE.name(), loginType);
168 sStore.setAttribute(NAME.LOGIN_IP.name(), ip);
169 sStore.setAttribute(NAME.REPRESENT_ROLE_TYPE.name(), userDao.getRole_type());
170 sStore.setAttribute(NAME.USER_SEQ.name(), userDao.getSeq());
171 sStore.setAttribute(NAME.LOGIN_EMAIL.name(), userDao.getEmail());
172 // sStore.setAttribute(NAME.LOGIN_PASSWORD.name(), userDao.getPasswd());
173 sStore.setAttribute(NAME.LOGIN_NAME.name(), userDao.getName());
174 sStore.setAttribute(NAME.IS_REGIST_DB.name(), userDao.getIs_regist_db());
175 sStore.setAttribute(NAME.LANGUAGE.name(), userDao.getLanguage());
176 sStore.setAttribute(NAME.TIMEZONE.name(), userDao.getTimezone());
178 sStore.setAttribute(NAME.IS_SHARED_DB.name(), userDao.getIs_shared_db());
179 sStore.setAttribute(NAME.IS_MODIFY_PERFERENCE.name(), userDao.getIs_modify_perference());
180 sStore.setAttribute(NAME.LIMIT_ADD_DB_CNT.name(), userDao.getLimit_add_db_cnt());
181 sStore.setAttribute(NAME.SERVICE_END.name(), userDao.getService_end());
183 sStore.setAttribute(NAME.PERSPECTIVE.name(), "default");
185 sStore.setAttribute(NAME.USE_OTP.name(), userDao.getUse_otp());
186 sStore.setAttribute(NAME.OTP_SECRET_KEY.name(), userDao.getOtp_secret());
188 sStore.setAttribute(NAME.UNLOCK_DB_LIST.name(), new ArrayList<Integer>());
192 * get login ip type
194 * @param getLoginIpType
196 public static String getLoginIpType() {
197 HttpSession sStore = RWT.getRequest().getSession();
198 return (String)sStore.getAttribute(NAME.LOGIN_IP_TYPE.name());
202 * get login ip
203 * @return
205 public static String getLoginIp() {
206 HttpSession sStore = RWT.getRequest().getSession();
207 return (String)sStore.getAttribute(NAME.LOGIN_IP.name());
210 // /**
211 // * set password
212 // *
213 // * @param strPasswd
214 // */
215 // public static void setPassword(String strPasswd) {
216 // HttpSession sStore = RWT.getRequest().getSession();
217 // sStore.setAttribute(NAME.LOGIN_PASSWORD.name(), strPasswd);
218 // }
220 public static void setUesrSeq(int seq) {
221 HttpSession sStore = RWT.getRequest().getSession();
222 sStore.setAttribute(NAME.USER_SEQ.name(), seq);
224 public static int getUserSeq() {
225 HttpSession sStore = RWT.getRequest().getSession();
226 Object obj = sStore.getAttribute(NAME.USER_SEQ.name());
228 if(obj == null) return 0;
229 else return (Integer)obj;
232 public static String getEMAIL() {
233 HttpSession sStore = RWT.getRequest().getSession();
235 return (String)sStore.getAttribute(NAME.LOGIN_EMAIL.name());
238 // public static String getPassword() {
239 // HttpSession sStore = RWT.getRequest().getSession();
240 // return (String)sStore.getAttribute(NAME.LOGIN_PASSWORD.name());
241 // }
243 public static String getName() {
244 HttpSession sStore = RWT.getRequest().getSession();
245 return (String)sStore.getAttribute(NAME.LOGIN_NAME.name());
247 public static String getIsRegistDB() {
248 HttpSession sStore = RWT.getRequest().getSession();
249 return (String)sStore.getAttribute(NAME.IS_REGIST_DB.name());
252 public static String getIsSharedDB() {
253 HttpSession sStore = RWT.getRequest().getSession();
254 return (String)sStore.getAttribute(NAME.IS_SHARED_DB.name());
257 public static Integer getLimitAddDBCnt() {
258 HttpSession sStore = RWT.getRequest().getSession();
259 return (Integer)sStore.getAttribute(NAME.LIMIT_ADD_DB_CNT.name());
262 public static String getIsModifyPerference() {
263 HttpSession sStore = RWT.getRequest().getSession();
264 return (String)sStore.getAttribute(NAME.IS_MODIFY_PERFERENCE.name());
267 public static Timestamp getServiceEnd() {
268 HttpSession sStore = RWT.getRequest().getSession();
269 return (Timestamp)sStore.getAttribute(NAME.SERVICE_END.name());
272 public static String getUseOTP() {
273 HttpSession sStore = RWT.getRequest().getSession();
274 return (String)sStore.getAttribute(NAME.USE_OTP.name());
276 public static String getOTPSecretKey() {
277 HttpSession sStore = RWT.getRequest().getSession();
278 return (String)sStore.getAttribute(NAME.OTP_SECRET_KEY.name());
280 public static String getLangeage() {
281 HttpSession sStore = RWT.getRequest().getSession();
282 return (String)sStore.getAttribute(NAME.LANGUAGE.name());
284 public static String getTimezone() {
285 HttpSession sStore = RWT.getRequest().getSession();
286 return (String)sStore.getAttribute(NAME.TIMEZONE.name());
290 * 자신이 대표 권한을 리턴합니다.
292 * <pre>
293 * 권한 중복일 경우
294 * admin이면서 manager일수는 없습니다.
295 * 1) admin
296 * 2) manager
297 * 3) dba
298 * 4) user
300 * group당 manager권한은 반듯이 하나입니다.
301 * manager권한이 정지되면 그룹을 수정 못하는 것으로.
302 * </pre>
304 * @return
306 public static String getRepresentRole() {
307 HttpSession sStore = RWT.getRequest().getSession();
308 return (String)sStore.getAttribute(NAME.REPRESENT_ROLE_TYPE.name());
311 public static boolean isSystemAdmin() {
312 return PublicTadpoleDefine.DB_USER_ROLE_TYPE.SYSTEM_ADMIN.name().equals(getRepresentRole()) ? true : false;
316 * 초기 접속시 사용자의 모든 프리퍼런스 데이터를 설정합니다.
318 public static void setUserAllPreferenceData(Map<String, Object> mapUserInfo) {
319 HttpSession sStore = RWT.getRequest().getSession();
320 sStore.setAttribute(NAME.USER_INFO_DATA.name(), mapUserInfo);
324 * 기존 세션 정보를 추가합니다.
325 * @param key
326 * @param obj
328 public static void setUserInfo(String key, String obj) {
330 HttpSession sStore = RWT.getRequest().getSession();
331 Map<String, Object> mapUserInfoData = (Map<String, Object>)sStore.getAttribute(NAME.USER_INFO_DATA.name());
332 UserInfoDataDAO userInfoDataDAO = (UserInfoDataDAO)mapUserInfoData.get(key);
333 if(userInfoDataDAO == null) {
334 userInfoDataDAO = new UserInfoDataDAO(SessionManager.getUserSeq(), key, obj);
336 try {
337 TadpoleSystem_UserInfoData.insertUserInfoData(userInfoDataDAO);
338 } catch(Exception e) {
339 logger.error("User data save exception [key]" + key + "[value]" + obj, e);
341 } else {
342 userInfoDataDAO.setValue0(obj);
345 mapUserInfoData.put(key, userInfoDataDAO);
346 sStore.setAttribute(NAME.USER_INFO_DATA.name(), mapUserInfoData);
350 * 사용자 User 정보 .
352 * @param key
353 * @param value
354 * @return
356 public static UserInfoDataDAO getUserInfo(String key, String value) {
357 HttpSession sStore = RWT.getRequest().getSession();
358 Map<String, Object> mapUserInfoData = (Map<String, Object>)sStore.getAttribute(NAME.USER_INFO_DATA.name());
360 UserInfoDataDAO userData = (UserInfoDataDAO)mapUserInfoData.get(key);
361 if(userData == null) {
362 userData = new UserInfoDataDAO(SessionManager.getUserSeq(), key, value);
363 try {
364 TadpoleSystem_UserInfoData.insertUserInfoData(userData);
365 } catch(Exception e) {
366 logger.error("User data save exception [key]" + key + "[value]" + value, e);
369 mapUserInfoData.put(key, userData);
372 return userData;
376 * set unlock db list
377 * @param userDB
378 * @return
380 public static boolean setUnlokDB(final UserDBDAO userDB) {
381 HttpSession sStore = RWT.getRequest().getSession();
382 List<UserDBDAO> listUnlockDB = (List)sStore.getAttribute(NAME.UNLOCK_DB_LIST.name());
384 return listUnlockDB.add(userDB);
388 * is unlock db
389 * @param userDB
390 * @return
392 public static boolean isUnlockDB(final UserDBDAO userDB) {
393 HttpSession sStore = RWT.getRequest().getSession();
394 List<UserDBDAO> listUnlockDB = (List)sStore.getAttribute(NAME.UNLOCK_DB_LIST.name());
396 return listUnlockDB.contains(userDB);
400 * unlock session
402 * @param userDB
404 public static void removeUnlockDB(final UserDBDAO userDB) {
405 HttpSession sStore = RWT.getRequest().getSession();
406 List<UserDBDAO> listUnlockDB = (List)sStore.getAttribute(NAME.UNLOCK_DB_LIST.name());
408 listUnlockDB.remove(userDB);
412 * logout 처리를 합니다.
414 public static void logout(final String strID) {
415 HttpSessionCollectorUtil.getInstance().sessionDestroyed(strID);
417 HttpServletRequest request = RWT.getRequest();
418 try {
419 HttpSession sStore = request.getSession();
420 // sStore.setAttribute(NAME.USER_SEQ.toString(), 0);
421 sStore.invalidate();
422 } catch(Throwable e) {
423 // ignore exception
426 try {
427 // fixed https://github.com/hangum/TadpoleForDBTools/issues/708
428 // ps - 사용자 session id를 보여주고 싶지 않아서 배열을 이용해서 뺐어요. - hangum
429 String[] arryRequestURL = StringUtils.split(request.getRequestURL().toString(), ";");
430 String browserText = MessageFormat.format("parent.window.location.href = \"{0}\";", arryRequestURL[0]);
431 JavaScriptExecutor executor = RWT.getClient().getService( JavaScriptExecutor.class );
432 executor.execute("setTimeout('"+browserText+"', 100)" );
433 } catch(Exception e) {
434 logger.error("loguout", e);
435 } finally {
436 // removeConnection(strID);
441 * 사용자 커넥션을 삭제한다.
443 * @param strID
445 public static void removeConnection(final String strID) {
446 removeTransactionInstance(strID);
447 removeNonTransactionInstance(strID);
451 * remove instance
453 * @param strID
455 private static void removeTransactionInstance(final String strID) {
456 Job job = new Job("Remove transaction instance") { //$NON-NLS-1$
457 @Override
458 public IStatus run(IProgressMonitor monitor) {
460 try {
461 TadpoleSQLTransactionManager.executeAllRollback(strID);
462 } catch(Exception e) {
463 logger.error("removeTransactionInstance connection instance", e);
464 return new Status(Status.WARNING, TadpoleEngineActivator.PLUGIN_ID, e.getMessage(), e);
465 } finally {
466 monitor.done();
468 return Status.OK_STATUS;
473 job.setName("RemoveTransaction instance");
474 job.setUser(false);
475 job.schedule();
479 * remove instance
481 * @param strID
483 private static void removeNonTransactionInstance(final String strID) {
484 Job job = new Job("Remove non transaction connection instance") { //$NON-NLS-1$
485 @Override
486 public IStatus run(IProgressMonitor monitor) {
488 try {
489 TadpoleSQLManager.removeAllInstance(strID);
490 } catch(Exception e) {
491 logger.error("remove user connection instance", e);
492 return new Status(Status.WARNING, TadpoleEngineActivator.PLUGIN_ID, e.getMessage(), e);
493 } finally {
494 monitor.done();
496 return Status.OK_STATUS;
501 job.setName("Remove normal instance");
502 job.setUser(false);
503 job.schedule();
507 * 사용자 session을 invalidate시킵니다.
509 public static void invalidate() {
510 try {
511 HttpSession sStore = RWT.getRequest().getSession();
513 HttpSessionContext hsc = sStore.getSessionContext();
514 Enumeration ids = hsc.getIds();
515 while(ids.hasMoreElements()) {
516 String id = (String)ids.nextElement();
518 if(logger.isDebugEnabled()) logger.debug("==========================> " + hsc.getSession(id));
521 } catch(Exception e) {
522 logger.error("user session invalidate", e);
526 // public static String getPerspective() {
527 // UserInfoDataDAO userInfo = SessionManager.getUserInfo(NAME.PERSPECTIVE.name(), "default");
528 // return userInfo.getValue0();
529 // }
531 // public static void setPerspective(String persp) {
532 // // db update
533 // try {
534 // TadpoleSystem_UserInfoData.updateUserInfoData(NAME.PERSPECTIVE.name(), persp);
535 // // session update
536 // SessionManager.setUserInfo(NAME.PERSPECTIVE.name(), persp);
537 // SessionManager.resetPerspective();
538 // } catch (Exception e) {
539 // logger.error("Error change perspective", e);
540 // }
541 // }
543 public static void resetPerspective() {
544 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
545 if (window != null) {
546 window.getActivePage().resetPerspective();