Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 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.net.provider; |
| 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 | import org.onosproject.event.Event; |
| 22 | import org.onosproject.event.EventDeliveryService; |
| 23 | import org.onosproject.event.EventListener; |
| 24 | import org.onosproject.event.ListenerRegistry; |
| 25 | import org.onosproject.event.ListenerService; |
| 26 | |
| 27 | /** |
| 28 | * Basis for components which need to export listener mechanism. |
| 29 | */ |
Thomas Vachuska | 8cd4acf | 2017-09-07 10:51:17 -0700 | [diff] [blame] | 30 | @Component |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 31 | public abstract class AbstractListenerProviderRegistry<E extends Event, L extends EventListener<E>, |
| 32 | P extends Provider, S extends ProviderService<P>> |
| 33 | extends AbstractProviderRegistry<P, S> implements ListenerService<E, L> { |
| 34 | |
| 35 | // If only Java supported mixins... |
| 36 | |
| 37 | protected final ListenerRegistry<E, L> listenerRegistry = new ListenerRegistry<>(); |
| 38 | |
| 39 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 40 | protected EventDeliveryService eventDispatcher; |
| 41 | |
| 42 | @Override |
| 43 | public void addListener(L listener) { |
| 44 | listenerRegistry.addListener(listener); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public void removeListener(L listener) { |
| 49 | listenerRegistry.removeListener(listener); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Safely posts the specified event to the local event dispatcher. |
| 55 | * If there is no event dispatcher or if the event is null, this method |
| 56 | * is a noop. |
| 57 | * |
| 58 | * @param event event to be posted; may be null |
| 59 | */ |
| 60 | protected void post(E event) { |
| 61 | if (event != null && eventDispatcher != null) { |
| 62 | eventDispatcher.post(event); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | } |