5c5abe2f053caec4f7d00565fc13fe9b34a45188
[controller.git] / opendaylight / md-sal / sal-dom-api / src / main / java / org / opendaylight / controller / sal / core / api / AbstractProvider.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.core.api;
9
10 import java.util.Collection;
11 import java.util.Collections;
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;
17
18 /**
19  * Deprecated.
20  *
21  * @deprecated Use blueprint instead for code wiring.
22  */
23 @Deprecated
24 public abstract class AbstractProvider implements BundleActivator, Provider,ServiceTrackerCustomizer<Broker, Broker> {
25
26     private Broker broker;
27     private BundleContext context;
28     private ServiceTracker<Broker, Broker> tracker;
29
30     @Override
31     public Collection<ProviderFunctionality> getProviderFunctionality() {
32         return Collections.emptySet();
33     }
34
35     @Override
36     public final void start(final BundleContext bundleContext) throws Exception {
37         this.context = bundleContext;
38         this.startImpl(bundleContext);
39         tracker = new ServiceTracker<>(bundleContext, Broker.class, this);
40         tracker.open();
41     }
42
43     protected void startImpl(final BundleContext bundleContext) {
44         // NOOP
45     }
46
47     protected void stopImpl(final BundleContext bundleContext) {
48         // NOOP
49     }
50
51     @Override
52     public final void stop(final BundleContext bundleContext) throws Exception {
53         broker = null;
54
55         if (tracker != null) {
56             tracker.close();
57         }
58
59         tracker = null;
60         stopImpl(bundleContext);
61     }
62
63     @Override
64     public Broker addingService(final ServiceReference<Broker> reference) {
65         if (broker == null && context != null) {
66             broker = context.getService(reference);
67             broker.registerProvider(this, context);
68             return broker;
69         }
70
71         return null;
72     }
73
74     @Override
75     public void modifiedService(final ServiceReference<Broker> reference, final Broker service) {
76         // NOOP
77     }
78
79     @Override
80     public void removedService(final ServiceReference<Broker> reference, final Broker service) {
81         stopImpl(context);
82     }
83 }