01: /**
02: * Copyright (C) 2007 NetMind Consulting Bt.
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */package hu.netmind.persistence;
18:
19: import java.util.*;
20: import hu.netmind.persistence.event.*;
21: import org.apache.log4j.Logger;
22:
23: /**
24: * Dispatches all events from the library internals to all registered
25: * listeners.
26: * @author Brautigam Robert
27: * @version CVS Revision: $Revision$
28: */
29: public class EventDispatcher {
30: private static Logger logger = Logger
31: .getLogger(EventDispatcher.class);
32: private List listeners;
33:
34: public EventDispatcher() {
35: listeners = new LinkedList();
36: }
37:
38: /**
39: * Register the given listener to this dispatcher.
40: */
41: public synchronized void registerListener(
42: PersistenceEventListener listener) {
43: if (!listeners.contains(listener))
44: listeners.add(listener);
45: }
46:
47: /**
48: * Remove the given listener from this dispatcher.
49: */
50: public synchronized void unregisterListener(
51: PersistenceEventListener listener) {
52: listeners.remove(listener);
53: }
54:
55: /**
56: * Notify all listeners of given event.
57: */
58: synchronized void notify(PersistenceEvent event) throws Exception {
59: for (int i = 0; i < listeners.size(); i++)
60: ((PersistenceEventListener) listeners.get(i)).handle(event);
61: }
62:
63: /**
64: * Notify listeners, but catch all exceptions, and do not rollback
65: * current transaction, if such exceptions are thrown.
66: */
67: void safeNotify(PersistenceEvent event) {
68: try {
69: notify(event);
70: } catch (Throwable e) {
71: logger
72: .warn(
73: "delivering event '"
74: + event
75: + "' was unsuccessful, but exception handled, because safe notify was invoked.",
76: e);
77: }
78: }
79: }
|