2 * Copyright (c) 2015 Brocade Communications 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.cluster.datastore;
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;
20 * Class that overlays DatastoreContext settings with settings obtained from an OSGi Config Admin
23 * @author Thomas Pantelis
25 public class DatastoreContextConfigAdminOverlay implements AutoCloseable {
26 public static final String CONFIG_ID = "org.opendaylight.controller.cluster.datastore";
28 private static final Logger LOG = LoggerFactory.getLogger(DatastoreContextConfigAdminOverlay.class);
30 private final DatastoreContextIntrospector introspector;
31 private final BundleContext bundleContext;
33 public DatastoreContextConfigAdminOverlay(DatastoreContextIntrospector introspector, BundleContext bundleContext) {
34 this.introspector = introspector;
35 this.bundleContext = bundleContext;
37 ServiceReference<ConfigurationAdmin> configAdminServiceReference =
38 bundleContext.getServiceReference(ConfigurationAdmin.class);
39 if(configAdminServiceReference == null) {
40 LOG.warn("No ConfigurationAdmin service found");
42 overlaySettings(configAdminServiceReference);
46 private void overlaySettings(ServiceReference<ConfigurationAdmin> configAdminServiceReference) {
48 ConfigurationAdmin configAdmin = bundleContext.getService(configAdminServiceReference);
50 Configuration config = configAdmin.getConfiguration(CONFIG_ID);
52 Dictionary<String, Object> properties = config.getProperties();
54 LOG.debug("Overlaying settings: {}", properties);
56 introspector.update(properties);
58 LOG.debug("No Configuration found for {}", CONFIG_ID);
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.
66 bundleContext.ungetService(configAdminServiceReference);
67 } catch (Exception e) {
68 LOG.debug("Error from ungetService", e);