KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > control > ControlEventListener


1 /*
2  * $Id: ControlEventListener.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.webapp.control;
25
26 import java.sql.Timestamp JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.servlet.http.HttpSession JavaDoc;
30 import javax.servlet.http.HttpSessionEvent JavaDoc;
31 import javax.servlet.http.HttpSessionListener JavaDoc;
32
33 import org.ofbiz.base.util.Debug;
34 import org.ofbiz.base.util.UtilDateTime;
35 import org.ofbiz.base.util.UtilMisc;
36 import org.ofbiz.webapp.stats.VisitHandler;
37 import org.ofbiz.entity.GenericEntityException;
38 import org.ofbiz.entity.GenericValue;
39 import org.ofbiz.entity.serialize.XmlSerializer;
40 import org.ofbiz.entity.transaction.TransactionUtil;
41
42 /**
43  * HttpSessionListener that gathers and tracks various information and statistics
44  *
45  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
46  * @version $Rev: 5462 $
47  * @since 2.0
48  */

49 public class ControlEventListener implements HttpSessionListener JavaDoc {
50     // Debug module name
51
public static final String JavaDoc module = ControlEventListener.class.getName();
52
53     protected static long totalActiveSessions = 0;
54     protected static long totalPassiveSessions = 0;
55
56     public ControlEventListener() {}
57
58     public void sessionCreated(HttpSessionEvent JavaDoc event) {
59         HttpSession JavaDoc session = event.getSession();
60
61         // get/create the visit
62
// NOTE: don't create the visit here, just let the control servlet do it; GenericValue visit = VisitHandler.getVisit(session);
63

64         countCreateSession();
65
66         // property setting flag for logging stats
67
if (System.getProperty("org.ofbiz.log.session.stats") != null) {
68             session.setAttribute("org.ofbiz.log.session.stats", "Y");
69         }
70
71         Debug.logInfo("Creating session: " + session.getId(), module);
72     }
73
74     public void sessionDestroyed(HttpSessionEvent JavaDoc event) {
75         HttpSession JavaDoc session = event.getSession();
76         
77         // Finalize the Visit
78
boolean beganTransaction = false;
79         try {
80             beganTransaction = TransactionUtil.begin();
81         
82             GenericValue visit = VisitHandler.getVisit(session);
83             if (visit != null) {
84                 visit.set("thruDate", new Timestamp JavaDoc(session.getLastAccessedTime()));
85                 visit.store();
86             }
87
88             // Store the UserLoginSession
89
String JavaDoc userLoginSessionString = getUserLoginSession(session);
90             GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
91             if (userLogin != null && userLoginSessionString != null) {
92                 GenericValue userLoginSession = null;
93                 userLoginSession = userLogin.getRelatedOne("UserLoginSession");
94
95                 if (userLoginSession == null) {
96                     userLoginSession = userLogin.getDelegator().makeValue("UserLoginSession",
97                             UtilMisc.toMap("userLoginId", userLogin.getString("userLoginId")));
98                     userLogin.getDelegator().create(userLoginSession);
99                 }
100                 userLoginSession.set("savedDate", UtilDateTime.nowTimestamp());
101                 userLoginSession.set("sessionData", userLoginSessionString);
102                 userLoginSession.store();
103             }
104
105             countDestroySession();
106             Debug.logInfo("Destroying session: " + session.getId(), module);
107             this.logStats(session, visit);
108         } catch (GenericEntityException e) {
109             try {
110                 // only rollback the transaction if we started one...
111
TransactionUtil.rollback(beganTransaction, "Error saving information about closed HttpSession", e);
112             } catch (GenericEntityException e2) {
113                 Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module);
114             }
115
116             Debug.logError(e, "Error in session destuction information persistence", module);
117         } finally {
118             // only commit the transaction if we started one... this will throw an exception if it fails
119
try {
120                 TransactionUtil.commit(beganTransaction);
121             } catch (GenericEntityException e) {
122                 Debug.logError(e, "Could not commit transaction for update visit for session destuction", module);
123             }
124         }
125     }
126
127     public void logStats(HttpSession JavaDoc session, GenericValue visit) {
128         if (Debug.verboseOn() || session.getAttribute("org.ofbiz.log.session.stats") != null) {
129             Debug.log("<===================================================================>", module);
130             Debug.log("Session ID : " + session.getId(), module);
131             Debug.log("Created Time : " + session.getCreationTime(), module);
132             Debug.log("Last Access : " + session.getLastAccessedTime(), module);
133             Debug.log("Max Inactive : " + session.getMaxInactiveInterval(), module);
134             Debug.log("--------------------------------------------------------------------", module);
135             Debug.log("Total Sessions : " + ControlEventListener.getTotalActiveSessions(), module);
136             Debug.log("Total Active : " + ControlEventListener.getTotalActiveSessions(), module);
137             Debug.log("Total Passive : " + ControlEventListener.getTotalPassiveSessions(), module);
138             Debug.log("** note : this session has been counted as destroyed.", module);
139             Debug.log("--------------------------------------------------------------------", module);
140             Debug.log("Visit ID : " + visit.getString("visitId"), module);
141             Debug.log("Party ID : " + visit.getString("partyId"), module);
142             Debug.log("Client IP : " + visit.getString("clientIpAddress"), module);
143             Debug.log("Client Host : " + visit.getString("clientHostName"), module);
144             Debug.log("Client User : " + visit.getString("clientUser"), module);
145             Debug.log("WebApp : " + visit.getString("webappName"), module);
146             Debug.log("Locale : " + visit.getString("initialLocale"), module);
147             Debug.log("UserAgent : " + visit.getString("initialUserAgent"), module);
148             Debug.log("Referrer : " + visit.getString("initialReferrer"), module);
149             Debug.log("Initial Req : " + visit.getString("initialRequest"), module);
150             Debug.log("Visit From : " + visit.getString("fromDate"), module);
151             Debug.log("Visit Thru : " + visit.getString("thruDate"), module);
152             Debug.log("--------------------------------------------------------------------", module);
153             Debug.log("--- Start Session Attributes: ---", module);
154             Enumeration JavaDoc sesNames = null;
155             try {
156                 sesNames = session.getAttributeNames();
157             } catch (IllegalStateException JavaDoc e) {
158                 Debug.log("Cannot get session attributes : " + e.getMessage(), module);
159             }
160             while (sesNames != null && sesNames.hasMoreElements()) {
161                 String JavaDoc attName = (String JavaDoc) sesNames.nextElement();
162                 Debug.log(attName + ":" + session.getAttribute(attName), module);
163             }
164             Debug.log("--- End Session Attributes ---", module);
165             Debug.log("<===================================================================>", module);
166         }
167     }
168
169     public static long getTotalActiveSessions() {
170         return totalActiveSessions;
171     }
172
173     public static long getTotalPassiveSessions() {
174         return totalPassiveSessions;
175     }
176
177     public static long getTotalSessions() {
178         return totalActiveSessions + totalPassiveSessions;
179     }
180
181     public static void countCreateSession() {
182         totalActiveSessions++;
183     }
184
185     public static void countDestroySession() {
186         totalActiveSessions--;
187     }
188
189     public static void countPassivateSession() {
190         totalActiveSessions--;
191         totalPassiveSessions++;
192     }
193
194     public static void countActivateSession() {
195         totalActiveSessions++;
196         totalPassiveSessions--;
197     }
198
199     private String JavaDoc getUserLoginSession(HttpSession JavaDoc session) {
200         Map JavaDoc userLoginSession = (Map JavaDoc) session.getAttribute("userLoginSession");
201
202         String JavaDoc sessionData = null;
203         if (userLoginSession != null && userLoginSession.size() > 0) {
204             try {
205                 sessionData = XmlSerializer.serialize(userLoginSession);
206             } catch (Exception JavaDoc e) {
207                 Debug.logWarning(e, "Problems serializing UserLoginSession", module);
208             }
209         }
210         return sessionData;
211     }
212 }
213
Popular Tags