1 24 package org.ofbiz.webapp.control; 25 26 import java.sql.Timestamp ; 27 import java.util.Enumeration ; 28 import java.util.Map ; 29 import javax.servlet.http.HttpSession ; 30 import javax.servlet.http.HttpSessionEvent ; 31 import javax.servlet.http.HttpSessionListener ; 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 49 public class ControlEventListener implements HttpSessionListener { 50 public static final String 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 event) { 59 HttpSession session = event.getSession(); 60 61 64 countCreateSession(); 65 66 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 event) { 75 HttpSession session = event.getSession(); 76 77 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 (session.getLastAccessedTime())); 85 visit.store(); 86 } 87 88 String 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 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 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 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 sesNames = null; 155 try { 156 sesNames = session.getAttributeNames(); 157 } catch (IllegalStateException e) { 158 Debug.log("Cannot get session attributes : " + e.getMessage(), module); 159 } 160 while (sesNames != null && sesNames.hasMoreElements()) { 161 String attName = (String ) 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 getUserLoginSession(HttpSession session) { 200 Map userLoginSession = (Map ) session.getAttribute("userLoginSession"); 201 202 String sessionData = null; 203 if (userLoginSession != null && userLoginSession.size() > 0) { 204 try { 205 sessionData = XmlSerializer.serialize(userLoginSession); 206 } catch (Exception e) { 207 Debug.logWarning(e, "Problems serializing UserLoginSession", module); 208 } 209 } 210 return sessionData; 211 } 212 } 213 | Popular Tags |