001: package org.objectweb.celtix.bus.busimpl;
002:
003: import java.util.*;
004: import java.util.logging.Level;
005: import java.util.logging.Logger;
006:
007: import org.objectweb.celtix.Bus;
008: import org.objectweb.celtix.BusEvent;
009: import org.objectweb.celtix.BusEventCache;
010:
011: public class BusEventCacheImpl implements BusEventCache {
012:
013: public static final String MAX_BUS_EVENTS = "bus:events:cache:size";
014: private static final Logger LOG = Logger
015: .getLogger(BusEventCacheImpl.class.getName());
016:
017: private List<BusEvent> events;
018:
019: private int maxEventsCache = 100;
020:
021: public BusEventCacheImpl(Bus b) {
022:
023: initCache();
024: }
025:
026: private void initCache() {
027: /* TODO read the EventCache configuration */
028: events = new ArrayList<BusEvent>(maxEventsCache);
029: }
030:
031: public synchronized void addEvent(BusEvent e) {
032: if (events.size() == maxEventsCache) {
033: BusEvent event = (BusEvent) events.get(0);
034: LOG.log(Level.FINE, "Event " + event.getID()
035: + " is removed because the event cache is full."
036: + " Maximum number of events stored : "
037: + maxEventsCache);
038:
039: events.remove(event);
040: }
041:
042: events.add(e);
043: }
044:
045: public synchronized List<BusEvent> flushEvents() {
046: List<BusEvent> result = new ArrayList<BusEvent>(events.size());
047: result.addAll(events);
048: events.clear();
049: return result;
050: }
051:
052: public synchronized List<BusEvent> flushEvents(String eventID) {
053: List<BusEvent> result = new ArrayList<BusEvent>();
054: for (Iterator<BusEvent> i = events.iterator(); i.hasNext();) {
055: BusEvent event = i.next();
056: if (eventID.equals(event.getID())) {
057: result.add(event);
058: i.remove();
059: }
060: }
061:
062: return result;
063: }
064:
065: public synchronized List<BusEvent> flushEvents(Class<?> eventClass) {
066: List<BusEvent> result = new ArrayList<BusEvent>();
067: for (Iterator<BusEvent> i = events.iterator(); i.hasNext();) {
068: BusEvent event = i.next();
069: if (eventClass.isAssignableFrom(event.getClass())) {
070: result.add(event);
071: i.remove();
072: }
073: }
074:
075: return result;
076: }
077:
078: public List<BusEvent> getEvents() {
079: return events;
080: }
081:
082: public List<BusEvent> getEvents(String eventID) {
083: List<BusEvent> result = new ArrayList<BusEvent>();
084:
085: for (int i = 0; i < events.size(); i++) {
086: BusEvent event = (BusEvent) events.get(i);
087:
088: if (eventID.equals(event.getID())) {
089: result.add(event);
090: }
091: }
092:
093: return result;
094: }
095:
096: public void setCacheSize(int size) {
097: maxEventsCache = size;
098: ((ArrayList) events).ensureCapacity(maxEventsCache);
099: }
100: }
|