001: /*
002: * $Id: ControlEventListener.java,v 1.2 2003/09/14 05:36:47 jonesde Exp $
003: *
004: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.content.webapp.control;
025:
026: import java.sql.Timestamp;
027: import java.util.Map;
028:
029: import javax.servlet.http.HttpSession;
030: import javax.servlet.http.HttpSessionEvent;
031: import javax.servlet.http.HttpSessionListener;
032:
033: import org.ofbiz.base.util.Debug;
034: import org.ofbiz.base.util.UtilDateTime;
035: import org.ofbiz.base.util.UtilMisc;
036: import org.ofbiz.content.stats.VisitHandler;
037: import org.ofbiz.entity.GenericEntityException;
038: import org.ofbiz.entity.GenericValue;
039: import org.ofbiz.entity.serialize.XmlSerializer;
040:
041: /**
042: * HttpSessionListener that gathers and tracks various information and statistics
043: *
044: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
045: * @version $Revision: 1.2 $
046: * @since 2.0
047: */
048: public class ControlEventListener implements HttpSessionListener {
049: // Debug module name
050: public static final String module = ControlEventListener.class
051: .getName();
052:
053: protected static long totalActiveSessions = 0;
054: protected static long totalPassiveSessions = 0;
055:
056: public ControlEventListener() {
057: }
058:
059: public void sessionCreated(HttpSessionEvent event) {
060: HttpSession session = event.getSession();
061:
062: // get/create the visit
063: // NOTE: don't create the visit here, just let the control servlet do it; GenericValue visit = VisitHandler.getVisit(session);
064:
065: countCreateSession();
066:
067: Debug.logInfo("Creating session: " + session.getId(), module);
068: }
069:
070: public void sessionDestroyed(HttpSessionEvent event) {
071: HttpSession session = event.getSession();
072:
073: // Finalize the Visit
074: GenericValue visit = VisitHandler.getVisit(session);
075: if (visit != null) {
076: visit.set("thruDate", new Timestamp(session
077: .getLastAccessedTime()));
078: try {
079: visit.store();
080: } catch (GenericEntityException e) {
081: Debug.logError(e,
082: "Could not update visit for session destuction: "
083: + visit, module);
084: }
085: }
086:
087: // Store the UserLoginSession
088: String userLoginSessionString = getUserLoginSession(session);
089: GenericValue userLogin = (GenericValue) session
090: .getAttribute("userLogin");
091: if (userLogin != null && userLoginSessionString != null) {
092: GenericValue userLoginSession = null;
093: try {
094: userLoginSession = userLogin
095: .getRelatedOne("UserLoginSession");
096:
097: if (userLoginSession == null) {
098: userLoginSession = userLogin
099: .getDelegator()
100: .makeValue(
101: "UserLoginSession",
102: UtilMisc
103: .toMap(
104: "userLoginId",
105: userLogin
106: .getString("userLoginId")));
107: userLogin.getDelegator().create(userLoginSession);
108: }
109: userLoginSession.set("savedDate", UtilDateTime
110: .nowTimestamp());
111: userLoginSession.set("sessionData",
112: userLoginSessionString);
113: userLoginSession.store();
114: } catch (GenericEntityException e) {
115: }
116: }
117:
118: countDestroySession();
119:
120: Debug.logInfo("Destroying session: " + session.getId(), module);
121: }
122:
123: public static long getTotalActiveSessions() {
124: return totalActiveSessions;
125: }
126:
127: public static long getTotalPassiveSessions() {
128: return totalPassiveSessions;
129: }
130:
131: public static long getTotalSessions() {
132: return totalActiveSessions + totalPassiveSessions;
133: }
134:
135: public static void countCreateSession() {
136: totalActiveSessions++;
137: }
138:
139: public static void countDestroySession() {
140: totalActiveSessions--;
141: }
142:
143: public static void countPassivateSession() {
144: totalActiveSessions--;
145: totalPassiveSessions++;
146: }
147:
148: public static void countActivateSession() {
149: totalActiveSessions++;
150: totalPassiveSessions--;
151: }
152:
153: private String getUserLoginSession(HttpSession session) {
154: Map userLoginSession = (Map) session
155: .getAttribute("userLoginSession");
156:
157: String sessionData = null;
158: if (userLoginSession != null && userLoginSession.size() > 0) {
159: try {
160: sessionData = XmlSerializer.serialize(userLoginSession);
161: } catch (Exception e) {
162: Debug
163: .logWarning(
164: e,
165: "Problems serializing UserLoginSession",
166: module);
167: }
168: }
169: return sessionData;
170: }
171: }
|