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.framework.ServiceRegistration;
15 import org.osgi.service.cm.Configuration;
16 import org.osgi.service.cm.ConfigurationAdmin;
17 import org.osgi.service.cm.ConfigurationEvent;
18 import org.osgi.service.cm.ConfigurationListener;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Class that overlays DatastoreContext settings with settings obtained from an OSGi Config Admin
26 * @author Thomas Pantelis
28 public class DatastoreContextConfigAdminOverlay implements AutoCloseable {
29 public static final String CONFIG_ID = "org.opendaylight.controller.cluster.datastore";
31 public interface Listener {
32 void onDatastoreContextUpdated(DatastoreContextFactory contextFactory);
35 private static final Logger LOG = LoggerFactory.getLogger(DatastoreContextConfigAdminOverlay.class);
37 private final DatastoreContextIntrospector introspector;
38 private final BundleContext bundleContext;
39 private ServiceRegistration<?> configListenerServiceRef;
40 private Listener listener;
42 public DatastoreContextConfigAdminOverlay(DatastoreContextIntrospector introspector,
43 BundleContext bundleContext) {
44 this.introspector = introspector;
45 this.bundleContext = bundleContext;
47 ServiceReference<ConfigurationAdmin> configAdminServiceReference =
48 bundleContext.getServiceReference(ConfigurationAdmin.class);
49 if (configAdminServiceReference == null) {
50 LOG.warn("No ConfigurationAdmin service found");
52 overlaySettings(configAdminServiceReference);
54 configListenerServiceRef = bundleContext.registerService(ConfigurationListener.class.getName(),
55 new DatastoreConfigurationListener(), null);
59 public void setListener(Listener listener) {
60 this.listener = listener;
63 @SuppressWarnings("checkstyle:IllegalCatch")
64 private void overlaySettings(ServiceReference<ConfigurationAdmin> configAdminServiceReference) {
66 ConfigurationAdmin configAdmin = bundleContext.getService(configAdminServiceReference);
68 Configuration config = configAdmin.getConfiguration(CONFIG_ID);
70 Dictionary<String, Object> properties = config.getProperties();
72 LOG.debug("Overlaying settings: {}", properties);
74 if (introspector.update(properties)) {
75 if (listener != null) {
76 listener.onDatastoreContextUpdated(introspector.newContextFactory());
80 LOG.debug("No Configuration found for {}", CONFIG_ID);
82 } catch (IOException e) {
83 LOG.error("Error obtaining Configuration for pid {}", CONFIG_ID, e);
84 } catch (IllegalStateException e) {
85 // Ignore - indicates the bundleContext has been closed.
88 bundleContext.ungetService(configAdminServiceReference);
89 } catch (Exception e) {
90 LOG.debug("Error from ungetService", e);
99 if (configListenerServiceRef != null) {
100 configListenerServiceRef.unregister();
104 private class DatastoreConfigurationListener implements ConfigurationListener {
106 public void configurationEvent(ConfigurationEvent event) {
107 if (CONFIG_ID.equals(event.getPid()) && event.getType() == ConfigurationEvent.CM_UPDATED) {
108 LOG.debug("configurationEvent: config {} was updated", CONFIG_ID);
109 overlaySettings(event.getReference());