001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.net.core.event;
006:
007: import com.tc.logging.TCLogger;
008: import com.tc.net.core.TCConnection;
009: import com.tc.net.protocol.TCNetworkMessage;
010: import com.tc.util.concurrent.SetOnceFlag;
011:
012: import java.util.Iterator;
013: import java.util.List;
014:
015: // calls each event only once
016: public class TCConnectionEventCaller {
017: private final SetOnceFlag connectEvent = new SetOnceFlag();
018: private final SetOnceFlag eofEvent = new SetOnceFlag();
019: private final SetOnceFlag errorEvent = new SetOnceFlag();
020: private final SetOnceFlag closeEvent = new SetOnceFlag();
021: private final TCLogger logger;
022:
023: public TCConnectionEventCaller(TCLogger logger) {
024: this .logger = logger;
025: }
026:
027: public void fireErrorEvent(List eventListeners, TCConnection conn,
028: final Exception exception, final TCNetworkMessage context) {
029: if (errorEvent.attemptSet()) {
030: final TCConnectionErrorEvent event = new TCConnectionErrorEvent(
031: conn, exception, context);
032: fireErrorEvent(eventListeners, event, logger);
033: }
034: }
035:
036: public static void fireErrorEvent(List eventListeners,
037: final TCConnectionErrorEvent event, final TCLogger logger) {
038: for (Iterator iter = eventListeners.iterator(); iter.hasNext();) {
039: TCConnectionEventListener listener = (TCConnectionEventListener) iter
040: .next();
041: try {
042: listener.errorEvent(event);
043: } catch (Exception e) {
044: logger.error("Unhandled exception in event handler", e);
045: }
046: }
047: }
048:
049: public void fireConnectEvent(List eventListeners, TCConnection conn) {
050: if (connectEvent.attemptSet()) {
051: final TCConnectionEvent event = new TCConnectionEvent(conn);
052: fireConnectEvent(eventListeners, event, logger);
053: }
054: }
055:
056: public static void fireConnectEvent(List eventListeners,
057: final TCConnectionEvent event, final TCLogger logger) {
058: for (Iterator iter = eventListeners.iterator(); iter.hasNext();) {
059: TCConnectionEventListener listener = (TCConnectionEventListener) iter
060: .next();
061: try {
062: listener.connectEvent(event);
063: } catch (Exception e) {
064: logger.error("Unhandled exception in event handler", e);
065: }
066: }
067: }
068:
069: public void fireEndOfFileEvent(List eventListeners,
070: TCConnection conn) {
071: if (eofEvent.attemptSet()) {
072: final TCConnectionEvent event = new TCConnectionEvent(conn);
073: fireEndOfFileEvent(eventListeners, event, logger);
074: }
075: }
076:
077: public static void fireEndOfFileEvent(List eventListeners,
078: final TCConnectionEvent event, final TCLogger logger) {
079: for (Iterator iter = eventListeners.iterator(); iter.hasNext();) {
080: TCConnectionEventListener listener = (TCConnectionEventListener) iter
081: .next();
082: try {
083: listener.endOfFileEvent(event);
084: } catch (Exception e) {
085: logger.error("Unhandled exception in event handler", e);
086: }
087: }
088: }
089:
090: public void fireCloseEvent(List eventListeners, TCConnection conn) {
091: if (closeEvent.attemptSet()) {
092: final TCConnectionEvent event = new TCConnectionEvent(conn);
093: fireCloseEvent(eventListeners, event, logger);
094: }
095: }
096:
097: public static void fireCloseEvent(List eventListeners,
098: final TCConnectionEvent event, TCLogger logger) {
099: for (Iterator iter = eventListeners.iterator(); iter.hasNext();) {
100: TCConnectionEventListener listener = (TCConnectionEventListener) iter
101: .next();
102: try {
103: listener.closeEvent(event);
104: } catch (Exception e) {
105: logger.error("Unhandled exception in event handler", e);
106: }
107: }
108: }
109:
110: }
|