20ee55519b945e42ffb3e06acf53b53d42a93556
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DatastoreContextConfigAdminOverlay.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.cluster.datastore;
9
10 import java.io.IOException;
11 import java.util.Dictionary;
12 import org.osgi.framework.BundleContext;
13 import org.osgi.framework.ServiceReference;
14 import org.osgi.service.cm.Configuration;
15 import org.osgi.service.cm.ConfigurationAdmin;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Class that overlays DatastoreContext settings with settings obtained from an OSGi Config Admin
21  * service.
22  *
23  * @author Thomas Pantelis
24  */
25 public class DatastoreContextConfigAdminOverlay implements AutoCloseable {
26     public static final String CONFIG_ID = "org.opendaylight.controller.cluster.datastore";
27
28     private static final Logger LOG = LoggerFactory.getLogger(DatastoreContextConfigAdminOverlay.class);
29
30     private final DatastoreContextIntrospector introspector;
31     private final BundleContext bundleContext;
32
33     public DatastoreContextConfigAdminOverlay(DatastoreContextIntrospector introspector, BundleContext bundleContext) {
34         this.introspector = introspector;
35         this.bundleContext = bundleContext;
36
37         ServiceReference<ConfigurationAdmin> configAdminServiceReference =
38                 bundleContext.getServiceReference(ConfigurationAdmin.class);
39         if(configAdminServiceReference == null) {
40             LOG.warn("No ConfigurationAdmin service found");
41         } else {
42             overlaySettings(configAdminServiceReference);
43         }
44     }
45
46     private void overlaySettings(ServiceReference<ConfigurationAdmin> configAdminServiceReference) {
47         try {
48             ConfigurationAdmin configAdmin = bundleContext.getService(configAdminServiceReference);
49
50             Configuration config = configAdmin.getConfiguration(CONFIG_ID);
51             if(config != null) {
52                 Dictionary<String, Object> properties = config.getProperties();
53
54                 LOG.debug("Overlaying settings: {}", properties);
55
56                 introspector.update(properties);
57             } else {
58                 LOG.debug("No Configuration found for {}", CONFIG_ID);
59             }
60         } catch (IOException e) {
61             LOG.error("Error obtaining Configuration for pid {}", CONFIG_ID, e);
62         } catch(IllegalStateException e) {
63             // Ignore - indicates the bundleContext has been closed.
64         } finally {
65             try {
66                 bundleContext.ungetService(configAdminServiceReference);
67             } catch (Exception e) {
68                 LOG.debug("Error from ungetService", e);
69             }
70         }
71     }
72
73     @Override
74     public void close() {
75     }
76 }