📄 serverhitbin.java
字号:
/* * $Id: ServerHitBin.java 5462 2005-08-05 18:35:48Z jonesde $ * * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.webapp.stats;import java.net.InetAddress;import java.util.Calendar;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilHttp;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;/** * <p>Counts server hits and tracks statistics for request, events and views * <p>Handles total stats since the server started and binned * stats according to settings in the serverstats.properties file. * * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @version $Rev: 5462 $ * @since 2.0 */public class ServerHitBin { // Debug module name public static final String module = ServerHitBin.class.getName(); public static final int REQUEST = 1; public static final int EVENT = 2; public static final int VIEW = 3; public static final int ENTITY = 4; public static final int SERVICE = 5; public static final String[] typeNames = {"", "Request", "Event", "View", "Entity", "Service"}; public static final String[] typeIds = {"", "REQUEST", "EVENT", "VIEW", "ENTITY", "SERVICE"}; public static void countRequest(String id, HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin, GenericDelegator delegator) { countHit(id, REQUEST, request, startTime, runningTime, userLogin, delegator); } public static void countEvent(String id, HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin, GenericDelegator delegator) { countHit(id, EVENT, request, startTime, runningTime, userLogin, delegator); } public static void countView(String id, HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin, GenericDelegator delegator) { countHit(id, VIEW, request, startTime, runningTime, userLogin, delegator); } public static void countEntity(String id, HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin, GenericDelegator delegator) { countHit(id, ENTITY, request, startTime, runningTime, userLogin, delegator); } public static void countService(String id, HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin, GenericDelegator delegator) { countHit(id, SERVICE, request, startTime, runningTime, userLogin, delegator); } public static void countHit(String id, int type, HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin, GenericDelegator delegator) { // only count hits if enabled, if not specified defaults to false if (!"true".equals(UtilProperties.getPropertyValue("serverstats", "stats.enable." + typeIds[type]))) return; countHit(id, type, request, startTime, runningTime, userLogin, delegator, true); } public static void advanceAllBins(long toTime) { advanceAllBins(toTime, requestHistory); advanceAllBins(toTime, eventHistory); advanceAllBins(toTime, viewHistory); advanceAllBins(toTime, entityHistory); advanceAllBins(toTime, serviceHistory); } static void advanceAllBins(long toTime, Map binMap) { Iterator entries = binMap.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); if (entry.getValue() != null) { ServerHitBin bin = (ServerHitBin) entry.getValue(); bin.advanceBin(toTime); } } } protected static void countHit(String id, int type, HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin, GenericDelegator delegator, boolean isOriginal) { if (delegator == null) { throw new IllegalArgumentException("The delgator passed to countHit cannot be null"); } ServerHitBin bin = null; LinkedList binList = null; switch (type) { case REQUEST: binList = (LinkedList) requestHistory.get(id); break; case EVENT: binList = (LinkedList) eventHistory.get(id); break; case VIEW: binList = (LinkedList) viewHistory.get(id); break; case ENTITY: binList = (LinkedList) entityHistory.get(id); break; case SERVICE: binList = (LinkedList) serviceHistory.get(id); break; } if (binList == null) { synchronized (ServerHitBin.class) { switch (type) { case REQUEST: binList = (LinkedList) requestHistory.get(id); break; case EVENT: binList = (LinkedList) eventHistory.get(id); break; case VIEW: binList = (LinkedList) viewHistory.get(id); break; case ENTITY: binList = (LinkedList) entityHistory.get(id); break; case SERVICE: binList = (LinkedList) serviceHistory.get(id); break; } if (binList == null) { binList = new LinkedList(); switch (type) { case REQUEST: requestHistory.put(id, binList); break; case EVENT: eventHistory.put(id, binList); break; case VIEW: viewHistory.put(id, binList); break; case ENTITY: entityHistory.put(id, binList); break; case SERVICE: serviceHistory.put(id, binList); break; } } } } if (binList.size() > 0) { bin = (ServerHitBin) binList.getFirst(); } if (bin == null) { synchronized (ServerHitBin.class) { if (binList.size() > 0) { bin = (ServerHitBin) binList.getFirst(); } if (bin == null) { bin = new ServerHitBin(id, type, true, delegator); binList.addFirst(bin); } } } bin.addHit(startTime, runningTime); if (isOriginal && !"GLOBAL".equals(id)) { bin.saveHit(request, startTime, runningTime, userLogin); } // count since start global and per id hits if (!"GLOBAL".equals(id)) countHitSinceStart(id, type, startTime, runningTime, isOriginal, delegator); // also count hits up the hierarchy if the id contains a '.' if (id.indexOf('.') > 0) { countHit(id.substring(0, id.lastIndexOf('.')), type, request, startTime, runningTime, userLogin, delegator, false); } if (isOriginal && !"GLOBAL".equals(id)) countHit("GLOBAL", type, request, startTime, runningTime, userLogin, delegator, true); } static void countHitSinceStart(String id, int type, long startTime, long runningTime, boolean isOriginal, GenericDelegator delegator) { if (delegator == null) { throw new IllegalArgumentException("The delgator passed to countHitSinceStart cannot be null"); } ServerHitBin bin = null; // save in global, and try to get bin by id switch (type) { case REQUEST: bin = (ServerHitBin) requestSinceStarted.get(id); break; case EVENT: bin = (ServerHitBin) eventSinceStarted.get(id); break; case VIEW: bin = (ServerHitBin) viewSinceStarted.get(id); break; case ENTITY: bin = (ServerHitBin) entitySinceStarted.get(id); break; case SERVICE: bin = (ServerHitBin) serviceSinceStarted.get(id); break; } if (bin == null) { synchronized (ServerHitBin.class) { switch (type) { case REQUEST: bin = (ServerHitBin) requestSinceStarted.get(id); break; case EVENT: bin = (ServerHitBin) eventSinceStarted.get(id); break; case VIEW: bin = (ServerHitBin) viewSinceStarted.get(id); break; case ENTITY: bin = (ServerHitBin) entitySinceStarted.get(id); break; case SERVICE: bin = (ServerHitBin) serviceSinceStarted.get(id); break; } if (bin == null) { bin = new ServerHitBin(id, type, false, delegator); switch (type) { case REQUEST: requestSinceStarted.put(id, bin); break; case EVENT: eventSinceStarted.put(id, bin); break; case VIEW: viewSinceStarted.put(id, bin); break; case ENTITY: entitySinceStarted.put(id, bin); break; case SERVICE: serviceSinceStarted.put(id, bin); break; } } } } bin.addHit(startTime, runningTime); if (isOriginal) countHitSinceStart("GLOBAL", type, startTime, runningTime, false, delegator); } // these Maps contain Lists of ServerHitBin objects by id, the most recent is first in the list public static Map requestHistory = new HashMap(); public static Map eventHistory = new HashMap(); public static Map viewHistory = new HashMap(); public static Map entityHistory = new HashMap(); public static Map serviceHistory = new HashMap(); // these Maps contain ServerHitBin objects by id public static Map requestSinceStarted = new HashMap(); public static Map eventSinceStarted = new HashMap(); public static Map viewSinceStarted = new HashMap(); public static Map entitySinceStarted = new HashMap(); public static Map serviceSinceStarted = new HashMap();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -