Merge "Add exists method on DOMStoreReadTransaction and DOMDataReadTransaction"
[controller.git] / opendaylight / config / config-persister-feature-adapter / src / main / java / org / opendaylight / controller / configpusherfeature / internal / FeatureServiceCustomizer.java
1 /*
2  * Copyright (c) 2014 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.configpusherfeature.internal;
9
10 import org.apache.karaf.features.FeaturesListener;
11 import org.apache.karaf.features.FeaturesService;
12 import org.opendaylight.controller.config.persist.api.ConfigPusher;
13 import org.osgi.framework.BundleContext;
14 import org.osgi.framework.ServiceReference;
15 import org.osgi.framework.ServiceRegistration;
16 import org.osgi.util.tracker.ServiceTrackerCustomizer;
17
18 public class FeatureServiceCustomizer implements ServiceTrackerCustomizer<FeaturesService, FeaturesService>, AutoCloseable {
19     private ConfigPusher configPusher = null;
20     private ConfigFeaturesListener configFeaturesListener = null;
21     private ServiceRegistration<?> registration;
22
23     FeatureServiceCustomizer(ConfigPusher c) {
24         configPusher = c;
25     }
26
27
28     @Override
29     public FeaturesService addingService(ServiceReference<FeaturesService> reference) {
30         BundleContext bc = reference.getBundle().getBundleContext();
31         FeaturesService featureService = bc.getService(reference);
32         configFeaturesListener = new ConfigFeaturesListener(configPusher,featureService);
33         registration = bc.registerService(FeaturesListener.class.getCanonicalName(), configFeaturesListener, null);
34         return featureService;
35     }
36
37     @Override
38     public void modifiedService(ServiceReference<FeaturesService> reference,
39             FeaturesService service) {
40         // we don't care if the properties change
41
42     }
43
44     @Override
45     public void removedService(ServiceReference<FeaturesService> reference,
46             FeaturesService service) {
47         close();
48     }
49
50     @Override
51     public void close() {
52         if(registration != null) {
53             registration.unregister();
54             registration = null;
55         }
56     }
57
58 }