001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.webservice.monitor;
021:
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpSession;
028:
029: /**
030: * @author mleidig@schlund.de
031: */
032: public class Monitor {
033:
034: public static enum Scope {
035: SESSION, IP
036: };
037:
038: Scope scope;
039: int histSize;
040: Map<String, MonitorHistory> ipToHistory;
041: long timeout = 2 * 60 * 1000;
042: long cleanupInterval = 1 * 60 * 1000;
043: Thread cleanupThread;
044:
045: public Monitor(Scope scope, int histSize) {
046: this .scope = scope;
047: this .histSize = histSize;
048: if (scope == Scope.IP) {
049: ipToHistory = new HashMap<String, MonitorHistory>();
050: cleanupThread = new Thread() {
051: public void run() {
052: while (!isInterrupted()) {
053: synchronized (ipToHistory) {
054: long time = System.currentTimeMillis()
055: - timeout;
056: Iterator<String> it = ipToHistory.keySet()
057: .iterator();
058: while (it.hasNext()) {
059: String ip = it.next();
060: MonitorHistory hist = ipToHistory
061: .get(ip);
062: if (hist.lastModified() < time)
063: ipToHistory.remove(ip);
064: }
065: }
066: try {
067: Thread.sleep(cleanupInterval);
068: } catch (InterruptedException x) {
069: }
070: }
071: }
072: };
073: cleanupThread.start();
074: }
075: }
076:
077: public Scope getScope() {
078: return scope;
079: }
080:
081: public int getHistorySize() {
082: return histSize;
083: }
084:
085: public MonitorHistory getMonitorHistory(HttpServletRequest req) {
086: MonitorHistory mh = null;
087: if (scope == Scope.IP) {
088: String ip = req.getRemoteAddr();
089: synchronized (ipToHistory) {
090: mh = (MonitorHistory) ipToHistory.get(ip);
091: if (mh == null) {
092: mh = new MonitorHistory(histSize);
093: ipToHistory.put(ip, mh);
094: }
095: }
096: } else {
097: HttpSession session = req.getSession(false);
098: if (session != null) {
099: mh = (MonitorHistory) session
100: .getAttribute(MonitorHistory.class.getName());
101: if (mh == null) {
102: mh = new MonitorHistory(histSize);
103: session.setAttribute(
104: MonitorHistory.class.getName(), mh);
105: }
106: } else
107: return new MonitorHistory(histSize);
108: }
109: return mh;
110: }
111:
112: }
|