| 1 | /** |
| 2 | * Copyright (C) 2007 NetMind Consulting Bt. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * 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 | */ |
| 18 | |
| 19 | package hu.netmind.beankeeper.event.impl; |
| 20 | |
| 21 | import hu.netmind.beankeeper.event.EventDispatcher; |
| 22 | import hu.netmind.beankeeper.event.PersistenceEventListener; |
| 23 | import hu.netmind.beankeeper.event.PersistenceEvent; |
| 24 | import java.util.*; |
| 25 | import org.apache.log4j.Logger; |
| 26 | |
| 27 | /** |
| 28 | * Dispatches all events from the library internals to all registered |
| 29 | * listeners. |
| 30 | * @author Brautigam Robert |
| 31 | * @version CVS Revision: $Revision$ |
| 32 | */ |
| 33 | public class EventDispatcherImpl implements EventDispatcher |
| 34 | { |
| 35 | private static Logger logger = Logger.getLogger(EventDispatcherImpl.class); |
| 36 | private List<PersistenceEventListener> listeners = |
| 37 | new LinkedList<PersistenceEventListener>(); |
| 38 | private ThreadLocal<Set<PersistenceEventListener>> runningListeners = |
| 39 | new ThreadLocal<Set<PersistenceEventListener>>(); |
| 40 | |
| 41 | public void init(Map parameters) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | public void release() |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Register the given listener to this dispatcher. |
| 51 | */ |
| 52 | public void registerListener(PersistenceEventListener listener) |
| 53 | { |
| 54 | registerListener(listener,PRI_NORMAL); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Register the given listener to this dispatcher with a priority. |
| 59 | */ |
| 60 | public void registerListener(PersistenceEventListener listener, int priority) |
| 61 | { |
| 62 | synchronized ( listeners ) |
| 63 | { |
| 64 | if ( ! listeners.contains(listener) ) |
| 65 | listeners.add(listener); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Remove the given listener from this dispatcher. |
| 71 | */ |
| 72 | public void unregisterListener(PersistenceEventListener listener) |
| 73 | { |
| 74 | synchronized ( listeners ) |
| 75 | { |
| 76 | listeners.remove(listener); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Notify all listeners of given event. This may not reach all event |
| 82 | * handlers, if there was an exception. |
| 83 | * @throws Exception The event handlers' exception is forwarded as-is. |
| 84 | */ |
| 85 | public void notify(PersistenceEvent event) |
| 86 | throws Exception |
| 87 | { |
| 88 | notify(event,true); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Notify all listeners in a guaranteed way. Exceptions that occur |
| 93 | * in the handlers will be logged, but not cause the event delivery |
| 94 | * to fail. |
| 95 | * @return True if the event delivery was a full success, false is one or |
| 96 | * more event handlers failed. |
| 97 | */ |
| 98 | public boolean notifyAll(PersistenceEvent event) |
| 99 | { |
| 100 | try |
| 101 | { |
| 102 | return notify(event,false); |
| 103 | } catch ( Exception e ) { |
| 104 | // This should not be |
| 105 | logger.error("guaranteed delivery threw error, this should no be possible",e); |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Go through all listeners and notify them. Also, prevent a listener notifying |
| 112 | * itself in an infinite loop. |
| 113 | * @param throwExceptions If true, method will stop on the first error and |
| 114 | * throw it back. |
| 115 | */ |
| 116 | private boolean notify(PersistenceEvent event, boolean throwExceptions) |
| 117 | throws Exception |
| 118 | { |
| 119 | if ( runningListeners.get() == null ) |
| 120 | runningListeners.set(new HashSet<PersistenceEventListener>()); |
| 121 | boolean success = true; |
| 122 | if ( logger.isDebugEnabled() ) |
| 123 | logger.debug("delivering "+event+" to "+listeners.size()+" listeners"); |
| 124 | // Copy listeners into a local list |
| 125 | List<PersistenceEventListener> localListeners = null; |
| 126 | synchronized ( listeners ) |
| 127 | { |
| 128 | localListeners = new ArrayList(listeners); |
| 129 | } |
| 130 | // Deliver events to local copy of listeners |
| 131 | for ( PersistenceEventListener listener : localListeners ) |
| 132 | { |
| 133 | // Insert listener to a thread local list, so the next |
| 134 | // time this method gets called (recursively) then this |
| 135 | // listener will not be executed again. |
| 136 | if ( runningListeners.get().contains(listener) ) |
| 137 | continue; // Skip this listener, it's already on our call stack |
| 138 | runningListeners.get().add(listener); |
| 139 | // Run the handler |
| 140 | try |
| 141 | { |
| 142 | listener.handle(event); |
| 143 | } catch ( Exception e ) { |
| 144 | if ( throwExceptions ) |
| 145 | throw e; |
| 146 | logger.warn("an event handler for event: "+event+", was not successful on guaranteed delivery",e); |
| 147 | success=false; |
| 148 | } |
| 149 | // Remove the handler from running list |
| 150 | runningListeners.get().remove(listener); |
| 151 | } |
| 152 | return success; |
| 153 | } |
| 154 | |
| 155 | } |
| 156 | |
| 157 | |