001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created May 10, 2005
014: * @author James Dixon
015: *
016: */
017:
018: package org.pentaho.core.session;
019:
020: import java.math.BigDecimal;
021: import java.util.Date;
022: import java.util.HashMap;
023: import java.util.Locale;
024: import javax.servlet.http.HttpSession;
025: import javax.servlet.http.HttpSessionEvent;
026: import javax.servlet.http.HttpSessionListener;
027:
028: import org.pentaho.core.audit.AuditHelper;
029: import org.pentaho.core.audit.MessageTypes;
030: import org.pentaho.core.system.PentahoSystem;
031: import org.pentaho.messages.Messages;
032: import org.pentaho.messages.util.LocaleHelper;
033: import org.pentaho.util.logging.Logger;
034:
035: public class PentahoHttpSessionListener implements HttpSessionListener {
036:
037: private static final boolean debug = PentahoSystem.debug;
038:
039: private static final HashMap sessionMap = new HashMap();
040:
041: public void sessionCreated(HttpSessionEvent event) {
042: // we can't find out what the locale of the request is so we go with the
043: // default for now...
044: LocaleHelper.setLocale(Locale.getDefault());
045: String sessionId = event.getSession().getId();
046: if (debug)
047: Logger
048: .debug(
049: this ,
050: Messages
051: .getString(
052: "HttpSessionListener.DEBUG_SESSION_CREATED", sessionId)); //$NON-NLS-1$
053:
054: // AuditHelper.audit( instanceId, String userId, String actionName,
055: // String objectType, MessageTypes.PROCESS_ID_SESSION,
056: // MessageTypes.SESSION_CREATE, "http session", "", 0, null );
057:
058: }
059:
060: public void sessionDestroyed(HttpSessionEvent event) {
061: HttpSession session = event.getSession();
062: try {
063: if (session != null) {
064: String sessionId = event.getSession().getId();
065: Object obj = session
066: .getAttribute("pentaho-session-context"); //$NON-NLS-1$
067: if (obj != null) {
068: IPentahoSession userSession = (IPentahoSession) obj;
069: userSession.destroy();
070: } else {
071: String info[] = getSessionInfo(sessionId);
072: if (info != null) {
073: String instanceId = info[5];
074: String userId = info[3];
075: String activityId = info[1];
076: String objectType = info[2];
077: String processId = info[0];
078: String messageType = MessageTypes.SESSION_END;
079: String message = "http "; //$NON-NLS-1$
080: String value = ""; //$NON-NLS-1$
081: long startTime = Long.parseLong(info[4]);
082: long endTime = new Date().getTime();
083: AuditHelper.audit(instanceId, userId,
084: activityId, objectType, processId,
085: messageType, message, value,
086: new BigDecimal(
087: (endTime - startTime) / 1000),
088: null);
089: }
090: }
091: }
092: } catch (Throwable e) {
093: Logger
094: .error(
095: this ,
096: Messages
097: .getErrorString("HttpSessionListener.ERROR_0001_ERROR_DESTROYING_SESSION"), e); //$NON-NLS-1$
098: }
099:
100: }
101:
102: public static synchronized void registerHttpSession(
103: String sessionId, String processId, String activityId,
104: String objectName, String userName, String id, long start) {
105: sessionMap.put(id, new String[] { processId, activityId,
106: objectName, userName, new Long(start).toString(),
107: sessionId });
108: }
109:
110: public static synchronized void deregisterHttpSession(String id) {
111: sessionMap.remove(id);
112: }
113:
114: private static synchronized String[] getSessionInfo(String id) {
115: return (String[]) sessionMap.get(id);
116: }
117:
118: }
|