Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.event; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Component; |
| 19 | import org.apache.felix.scr.annotations.Reference; |
| 20 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 21 | |
| 22 | /** |
| 23 | * Basis for components which need to export listener mechanism. |
| 24 | */ |
| 25 | @Component(componentAbstract = true) |
| 26 | public abstract class AbstractListenerManager<E extends Event, L extends EventListener<E>> |
| 27 | implements ListenerService<E, L> { |
| 28 | |
| 29 | protected final ListenerRegistry<E, L> listenerRegistry = new ListenerRegistry<>(); |
| 30 | |
| 31 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 32 | protected EventDeliveryService eventDispatcher; |
| 33 | |
| 34 | @Override |
| 35 | public void addListener(L listener) { |
| 36 | listenerRegistry.addListener(listener); |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public void removeListener(L listener) { |
| 41 | listenerRegistry.removeListener(listener); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Safely posts the specified event to the local event dispatcher. |
| 47 | * If there is no event dispatcher or if the event is null, this method |
| 48 | * is a noop. |
| 49 | * |
| 50 | * @param event event to be posted; may be null |
| 51 | */ |
| 52 | protected void post(E event) { |
| 53 | if (event != null && eventDispatcher != null) { |
| 54 | eventDispatcher.post(event); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | } |