d1a3f85f946e1165e1b1c6f4bed46699e6a5dac4
[controller.git] / opendaylight / md-sal / sal-binding-api / src / main / java / org / opendaylight / controller / sal / binding / api / AbstractBrokerAwareActivator.java
1 package org.opendaylight.controller.sal.binding.api;
2
3 import java.util.concurrent.ExecutorService;
4 import java.util.concurrent.Executors;
5
6 import org.osgi.framework.BundleActivator;
7 import org.osgi.framework.BundleContext;
8 import org.osgi.framework.ServiceReference;
9 import org.osgi.util.tracker.ServiceTracker;
10 import org.osgi.util.tracker.ServiceTrackerCustomizer;
11
12 public abstract class AbstractBrokerAwareActivator implements BundleActivator {
13
14     private static final ExecutorService mdActivationPool = Executors.newCachedThreadPool();
15     private BundleContext context;
16     private ServiceTracker<BindingAwareBroker, BindingAwareBroker> tracker;
17     private BindingAwareBroker broker;
18     private ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker> customizer = new ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker>() {
19         
20         @Override
21         public BindingAwareBroker addingService(ServiceReference<BindingAwareBroker> reference) {
22             broker = context.getService(reference);
23             mdActivationPool.execute(new Runnable() {
24                 
25                 @Override
26                 public void run() {
27                     onBrokerAvailable(broker, context);;
28                 }
29             });
30             return broker;
31         }
32         
33         @Override
34         public void modifiedService(ServiceReference<BindingAwareBroker> reference, BindingAwareBroker service) {
35             // TODO Auto-generated method stub
36             
37         }
38
39         @Override
40         public void removedService(ServiceReference<BindingAwareBroker> reference, BindingAwareBroker service) {
41             // TODO Auto-generated method stub
42             
43         }
44
45     };
46     
47     
48     @Override
49     public final void start(BundleContext context) throws Exception {
50         this.context = context;
51         startImpl(context);
52         tracker = new ServiceTracker<>(context, BindingAwareBroker.class, customizer);
53         tracker.open();
54         
55     }
56
57
58     
59     @Override
60     public final  void stop(BundleContext context) throws Exception {
61         tracker.close();
62         stopImpl(context);
63     }
64     
65     
66     /**
67      * Called when this bundle is started (before
68      * {@link #onSessionInitiated(ProviderContext)} so the Framework can perform
69      * the bundle-specific activities necessary to start this bundle. This
70      * method can be used to register services or to allocate any resources that
71      * this bundle needs.
72      * 
73      * <p>
74      * This method must complete and return to its caller in a timely manner.
75      * 
76      * @param context
77      *            The execution context of the bundle being started.
78      * @throws Exception
79      *             If this method throws an exception, this bundle is marked as
80      *             stopped and the Framework will remove this bundle's
81      *             listeners, unregister all services registered by this bundle,
82      *             and release all services used by this bundle.
83      */
84     protected void startImpl(BundleContext context) {
85         // NOOP
86     }
87
88     /**
89      * Called when this bundle is stopped so the Framework can perform the
90      * bundle-specific activities necessary to stop the bundle. In general, this
91      * method should undo the work that the {@code BundleActivator.start} method
92      * started. There should be no active threads that were started by this
93      * bundle when this bundle returns. A stopped bundle must not call any
94      * Framework objects.
95      * 
96      * <p>
97      * This method must complete and return to its caller in a timely manner.
98      * 
99      * @param context The execution context of the bundle being stopped.
100      * @throws Exception If this method throws an exception, the bundle is still
101      *         marked as stopped, and the Framework will remove the bundle's
102      *         listeners, unregister all services registered by the bundle, and
103      *         release all services used by the bundle.
104      */
105     protected void stopImpl(BundleContext context) {
106         // NOOP
107     }
108     
109
110     protected abstract void onBrokerAvailable(BindingAwareBroker broker, BundleContext context);
111     
112     protected void onBrokerRemoved(BindingAwareBroker broker, BundleContext context) {
113         
114     }
115 }