
Author: ceki Date: Thu Dec 18 22:12:54 2008 New Revision: 2088 Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/tracker/ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/tracker/AppenderTrackerTImpl.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/tracker/SimulationEvent.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/tracker/TEntry.java Log: heavy refactorization Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/tracker/AppenderTrackerTImpl.java ============================================================================== --- (empty file) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/tracker/AppenderTrackerTImpl.java Thu Dec 18 22:12:54 2008 @@ -0,0 +1,98 @@ +/** + * Logback: the generic, reliable, fast and flexible logging framework. + * + * Copyright (C) 2000-2008, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ +package ch.qos.logback.core.sift.tracker; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +import ch.qos.logback.core.Appender; +import ch.qos.logback.core.sift.AppenderTracker; + +/** + * This is an alternative (slower) implementation of AppenderTracker for testing + * purposes. + * + * @author Ceki Gulcu + */ +public class AppenderTrackerTImpl implements AppenderTracker<Object, String> { + + List<TEntry> entryList = new LinkedList<TEntry>(); + long lastCheck = 0; + + public AppenderTrackerTImpl() { + } + + @SuppressWarnings("unchecked") + synchronized public void put(String k, Appender<Object> appender, + long timestamp) { + TEntry te = getEntry(k); + if (te != null) { + te.timestamp = timestamp; + } else { + te = new TEntry(k, appender, timestamp); + entryList.add(te); + } + Collections.sort(entryList); + } + + @SuppressWarnings("unchecked") + synchronized public Appender<Object> get(String k, long timestamp) { + TEntry te = getEntry(k); + if (te == null) { + return null; + } else { + te.timestamp = timestamp; + Collections.sort(entryList); + return te.appender; + } + } + + synchronized public void stopStaleAppenders(long timestamp) { + if (lastCheck + MILLIS_IN_ONE_SECOND > timestamp) { + return; + } + lastCheck = timestamp; + while (entryList.size() != 0 && isEntryStale(entryList.get(0), timestamp)) { + entryList.remove(0); + } + } + + final private boolean isEntryStale(TEntry entry, long now) { + return ((entry.timestamp + THRESHOLD) < now); + } + + synchronized public List<String> keyList() { + List<String> keyList = new ArrayList<String>(); + for (TEntry e : entryList) { + keyList.add(e.key); + } + return keyList; + } + + synchronized public List<Appender<Object>> valueList() { + List<Appender<Object>> appenderList = new ArrayList<Appender<Object>>(); + for (TEntry e : entryList) { + appenderList.add(e.appender); + } + return appenderList; + } + + private TEntry getEntry(String k) { + for (int i = 0; i < entryList.size(); i++) { + TEntry te = entryList.get(i); + if (te.key.equals(k)) { + return te; + } + } + return null; + } +} Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/tracker/SimulationEvent.java ============================================================================== --- (empty file) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/tracker/SimulationEvent.java Thu Dec 18 22:12:54 2008 @@ -0,0 +1,17 @@ +package ch.qos.logback.core.sift.tracker; + + +public class SimulationEvent { + + public String key; + public long timestamp; + + public SimulationEvent(String key, long timestamp) { + this.key = key; + this.timestamp = timestamp; + } + + public String toString() { + return "Event: k=" + key +", timestamp=" + timestamp; + } +} Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/tracker/TEntry.java ============================================================================== --- (empty file) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/tracker/TEntry.java Thu Dec 18 22:12:54 2008 @@ -0,0 +1,45 @@ +/** + * Logback: the generic, reliable, fast and flexible logging framework. + * + * Copyright (C) 2000-2008, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ +package ch.qos.logback.core.sift.tracker; + +import ch.qos.logback.core.Appender; + +public class TEntry implements Comparable { + + String key; + long timestamp; + Appender<Object> appender; + + TEntry(String key, Appender<Object> appender, long timestamp) { + this.key = key; + this.appender = appender; + this.timestamp = timestamp; + } + + public int compareTo(Object o) { + if(!(o instanceof TEntry)) { + throw new IllegalArgumentException("arguments must be of type "+TEntry.class); + } + + TEntry other = (TEntry) o; + if(timestamp > other.timestamp) { + return 1; + } + if(timestamp == other.timestamp) { + return 0; + } + return -1; + } + + @Override + public String toString() { + return "("+key+","+timestamp+")"; + } +}