2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.binding.api;
10 import java.util.concurrent.ExecutorService;
11 import java.util.concurrent.Executors;
12 import org.osgi.framework.BundleActivator;
13 import org.osgi.framework.BundleContext;
14 import org.osgi.framework.ServiceReference;
15 import org.osgi.util.tracker.ServiceTracker;
16 import org.osgi.util.tracker.ServiceTrackerCustomizer;
18 public abstract class AbstractBrokerAwareActivator implements BundleActivator {
20 private static final ExecutorService mdActivationPool = Executors.newCachedThreadPool();
21 private BundleContext context;
22 private ServiceTracker<BindingAwareBroker, BindingAwareBroker> tracker;
23 private BindingAwareBroker broker;
24 private final ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker> customizer = new ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker>() {
27 public BindingAwareBroker addingService(final ServiceReference<BindingAwareBroker> reference) {
28 broker = context.getService(reference);
29 mdActivationPool.execute(new Runnable() {
33 onBrokerAvailable(broker, context);
40 public void modifiedService(final ServiceReference<BindingAwareBroker> reference, final BindingAwareBroker service) {
41 removedService(reference, service);
42 addingService(reference);
46 public void removedService(final ServiceReference<BindingAwareBroker> reference, final BindingAwareBroker service) {
47 broker = context.getService(reference);
48 mdActivationPool.execute(new Runnable() {
52 onBrokerRemoved(broker, context);
61 public final void start(final BundleContext context) throws Exception {
62 this.context = context;
64 tracker = new ServiceTracker<>(context, BindingAwareBroker.class, customizer);
72 public final void stop(final BundleContext context) throws Exception {
77 protected void startImpl(final BundleContext context) throws Exception {
82 * Called when this bundle is stopped so the Framework can perform the
83 * bundle-specific activities necessary to stop the bundle. In general, this
84 * method should undo the work that the {@code BundleActivator.start} method
85 * started. There should be no active threads that were started by this
86 * bundle when this bundle returns. A stopped bundle must not call any
90 * This method must complete and return to its caller in a timely manner.
92 * @param context The execution context of the bundle being stopped.
94 protected void stopImpl(final BundleContext context) {
99 protected abstract void onBrokerAvailable(BindingAwareBroker broker, BundleContext context);
101 protected void onBrokerRemoved(final BindingAwareBroker broker, final BundleContext context) {