KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > session > PentahoHttpSessionListener


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created May 10, 2005
14  * @author James Dixon
15  *
16  */

17
18 package org.pentaho.core.session;
19
20 import java.math.BigDecimal JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Locale JavaDoc;
24 import javax.servlet.http.HttpSession JavaDoc;
25 import javax.servlet.http.HttpSessionEvent JavaDoc;
26 import javax.servlet.http.HttpSessionListener JavaDoc;
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 JavaDoc {
36
37     private static final boolean debug = PentahoSystem.debug;
38
39     private static final HashMap JavaDoc sessionMap = new HashMap JavaDoc();
40
41     public void sessionCreated(HttpSessionEvent JavaDoc event) {
42         // we can't find out what the locale of the request is so we go with the
43
// default for now...
44
LocaleHelper.setLocale(Locale.getDefault());
45         String JavaDoc sessionId = event.getSession().getId();
46         if (debug)
47             Logger.debug(this, Messages.getString("HttpSessionListener.DEBUG_SESSION_CREATED", sessionId)); //$NON-NLS-1$
48

49         // AuditHelper.audit( instanceId, String userId, String actionName,
50
// String objectType, MessageTypes.PROCESS_ID_SESSION,
51
// MessageTypes.SESSION_CREATE, "http session", "", 0, null );
52

53     }
54
55     public void sessionDestroyed(HttpSessionEvent JavaDoc event) {
56         HttpSession JavaDoc session = event.getSession();
57         try {
58             if (session != null) {
59                 String JavaDoc sessionId = event.getSession().getId();
60                 Object JavaDoc obj = session.getAttribute("pentaho-session-context"); //$NON-NLS-1$
61
if (obj != null) {
62                     IPentahoSession userSession = (IPentahoSession) obj;
63                     userSession.destroy();
64                 } else {
65                     String JavaDoc info[] = getSessionInfo(sessionId);
66                     if (info != null) {
67                         String JavaDoc instanceId = info[5];
68                         String JavaDoc userId = info[3];
69                         String JavaDoc activityId = info[1];
70                         String JavaDoc objectType = info[2];
71                         String JavaDoc processId = info[0];
72                         String JavaDoc messageType = MessageTypes.SESSION_END;
73                         String JavaDoc message = "http "; //$NON-NLS-1$
74
String JavaDoc value = ""; //$NON-NLS-1$
75
long startTime = Long.parseLong(info[4]);
76                         long endTime = new Date JavaDoc().getTime();
77                         AuditHelper.audit(instanceId, userId, activityId, objectType, processId, messageType, message, value, new BigDecimal JavaDoc((endTime - startTime) / 1000), null);
78                     }
79                 }
80             }
81         } catch (Throwable JavaDoc e) {
82             Logger.error(this, Messages.getErrorString("HttpSessionListener.ERROR_0001_ERROR_DESTROYING_SESSION"), e); //$NON-NLS-1$
83
}
84
85     }
86
87     public static synchronized void registerHttpSession(String JavaDoc sessionId, String JavaDoc processId, String JavaDoc activityId, String JavaDoc objectName, String JavaDoc userName, String JavaDoc id, long start) {
88         sessionMap.put(id, new String JavaDoc[] { processId, activityId, objectName, userName, new Long JavaDoc(start).toString(), sessionId });
89     }
90
91     public static synchronized void deregisterHttpSession(String JavaDoc id) {
92         sessionMap.remove(id);
93     }
94
95     private static synchronized String JavaDoc[] getSessionInfo(String JavaDoc id) {
96         return (String JavaDoc[]) sessionMap.get(id);
97     }
98
99 }
100
Popular Tags