1 17 18 package org.pentaho.core.session; 19 20 import java.math.BigDecimal ; 21 import java.util.Date ; 22 import java.util.HashMap ; 23 import java.util.Locale ; 24 import javax.servlet.http.HttpSession ; 25 import javax.servlet.http.HttpSessionEvent ; 26 import javax.servlet.http.HttpSessionListener ; 27 28 import org.pentaho.core.audit.AuditHelper; 29 import org.pentaho.core.audit.MessageTypes; 30 import org.pentaho.core.system.PentahoSystem; 31 import org.pentaho.messages.Messages; 32 import org.pentaho.messages.util.LocaleHelper; 33 import org.pentaho.util.logging.Logger; 34 35 public class PentahoHttpSessionListener implements HttpSessionListener { 36 37 private static final boolean debug = PentahoSystem.debug; 38 39 private static final HashMap sessionMap = new HashMap (); 40 41 public void sessionCreated(HttpSessionEvent event) { 42 LocaleHelper.setLocale(Locale.getDefault()); 45 String sessionId = event.getSession().getId(); 46 if (debug) 47 Logger.debug(this, Messages.getString("HttpSessionListener.DEBUG_SESSION_CREATED", sessionId)); 49 53 } 54 55 public void sessionDestroyed(HttpSessionEvent event) { 56 HttpSession session = event.getSession(); 57 try { 58 if (session != null) { 59 String sessionId = event.getSession().getId(); 60 Object obj = session.getAttribute("pentaho-session-context"); if (obj != null) { 62 IPentahoSession userSession = (IPentahoSession) obj; 63 userSession.destroy(); 64 } else { 65 String info[] = getSessionInfo(sessionId); 66 if (info != null) { 67 String instanceId = info[5]; 68 String userId = info[3]; 69 String activityId = info[1]; 70 String objectType = info[2]; 71 String processId = info[0]; 72 String messageType = MessageTypes.SESSION_END; 73 String message = "http "; String value = ""; long startTime = Long.parseLong(info[4]); 76 long endTime = new Date ().getTime(); 77 AuditHelper.audit(instanceId, userId, activityId, objectType, processId, messageType, message, value, new BigDecimal ((endTime - startTime) / 1000), null); 78 } 79 } 80 } 81 } catch (Throwable e) { 82 Logger.error(this, Messages.getErrorString("HttpSessionListener.ERROR_0001_ERROR_DESTROYING_SESSION"), e); } 84 85 } 86 87 public static synchronized void registerHttpSession(String sessionId, String processId, String activityId, String objectName, String userName, String id, long start) { 88 sessionMap.put(id, new String [] { processId, activityId, objectName, userName, new Long (start).toString(), sessionId }); 89 } 90 91 public static synchronized void deregisterHttpSession(String id) { 92 sessionMap.remove(id); 93 } 94 95 private static synchronized String [] getSessionInfo(String id) { 96 return (String []) sessionMap.get(id); 97 } 98 99 } 100 | Popular Tags |