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