| 1 | package org.jtoolkit.essence.data.impl; |
| 2 | |
| 3 | import org.apache.commons.logging.Log; |
| 4 | import org.apache.commons.logging.LogFactory; |
| 5 | import org.jetbrains.annotations.NotNull; |
| 6 | import org.jtoolkit.essence.concurrency.Callback; |
| 7 | import org.jtoolkit.essence.concurrency.impl.CallbackSetImpl; |
| 8 | import org.jtoolkit.essence.data.Event; |
| 9 | |
| 10 | import java.util.Arrays; |
| 11 | import java.util.LinkedHashSet; |
| 12 | import java.util.Set; |
| 13 | |
| 14 | /* |
| 15 | Copyright 2006 Peter Lawrey |
| 16 | |
| 17 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 18 | you may not use this file except in compliance with the License. |
| 19 | You may obtain a copy of the License at |
| 20 | |
| 21 | http://www.apache.org/licenses/LICENSE-2.0 |
| 22 | |
| 23 | Unless required by applicable law or agreed to in writing, software |
| 24 | distributed under the License is distributed on an "AS IS" BASIS, |
| 25 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 | See the License for the specific language governing permissions and |
| 27 | limitations under the License. |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * @author Peter Lawrey |
| 32 | */ |
| 33 | public class ListenerSetImpl extends CallbackSetImpl<Event> implements Event.ListenerSet { |
| 34 | private static final Log LOG = LogFactory.getLog(ListenerSetImpl.class); |
| 35 | private final Set<Event.Listener> listeners = new LinkedHashSet<Event.Listener>(); |
| 36 | @SuppressWarnings({"FieldAccessedSynchronizedAndUnsynchronized"}) |
| 37 | private Event.Listener[] listenerArr; |
| 38 | @SuppressWarnings({"FieldAccessedSynchronizedAndUnsynchronized"}) |
| 39 | private boolean online = false; |
| 40 | |
| 41 | public ListenerSetImpl() { |
| 42 | super(CallbackMode.THREADED); |
| 43 | resetListenerArr(); |
| 44 | } |
| 45 | |
| 46 | public void addCallback(@NotNull Callback<Event> callback) { |
| 47 | synchronized (callbacks) { |
| 48 | try { |
| 49 | callback.onCallback(new Event.StatusEvent(online)); |
| 50 | } catch (IllegalStateException e) { |
| 51 | dropCallback(callback, e); |
| 52 | return; |
| 53 | } catch (Exception e) { |
| 54 | LOG.warn(getThreadName() + ": Dropping callback " + callback, e); |
| 55 | return; |
| 56 | } |
| 57 | super.addCallback(callback); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private static String getThreadName() { |
| 62 | return Thread.currentThread().getName(); |
| 63 | } |
| 64 | |
| 65 | public void add(@NotNull Event.Listener l) { |
| 66 | synchronized (listeners) { |
| 67 | if (listeners.add(callbackMode == CallbackMode.SIMPLE ? l : new ThreadedListener(l))) |
| 68 | resetListenerArr(); |
| 69 | } |
| 70 | l.onStatusEvent(online); |
| 71 | } |
| 72 | |
| 73 | public boolean isEmpty() { |
| 74 | synchronized (listeners) { |
| 75 | return super.isEmpty() && listenerArr.length == 0; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @SuppressWarnings({"CastToIncompatibleInterface"}) |
| 80 | public void onCallback(@NotNull Event event) { |
| 81 | super.onCallback(event); |
| 82 | Event.Listener[] listenerArr; |
| 83 | synchronized (listeners) { |
| 84 | listenerArr = this.listenerArr; |
| 85 | } |
| 86 | for (Event.Listener l : listenerArr) |
| 87 | try { |
| 88 | event.notifyListener(l); |
| 89 | } catch (IllegalStateException e) { |
| 90 | Callback callback; |
| 91 | if (l instanceof ThreadedListener) |
| 92 | callback = (Callback) ((ThreadedListener) l).getUnderlying(); |
| 93 | else |
| 94 | callback = (Callback) l; |
| 95 | if (LOG.isDebugEnabled()) LOG.debug(getThreadName() + ": Dropping callback " + callback, e); |
| 96 | super.removeCallback(callback); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public void notifyReset(boolean online2) { |
| 101 | online = online2; |
| 102 | onCallback(new Event.StatusEvent(online2)); |
| 103 | } |
| 104 | |
| 105 | public void remove(@NotNull Event.Listener l) { |
| 106 | synchronized (listeners) { |
| 107 | if (listeners.remove(l)) |
| 108 | resetListenerArr(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | private void resetListenerArr() { |
| 113 | listenerArr = listeners.toArray(new Event.Listener[listeners.size()]); |
| 114 | } |
| 115 | |
| 116 | public String toString() { |
| 117 | return "ListenerSet callbacks=" + Arrays.asList(getCallbackArray()) + ", listeners=" + Arrays.asList(listenerArr); |
| 118 | } |
| 119 | } |