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 static 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 private void overlaySettings(ServiceReference<ConfigurationAdmin> configAdminServiceReference) {
65 ConfigurationAdmin configAdmin = bundleContext.getService(configAdminServiceReference);
67 Configuration config = configAdmin.getConfiguration(CONFIG_ID);
69 Dictionary<String, Object> properties = config.getProperties();
71 LOG.debug("Overlaying settings: {}", properties);
73 if(introspector.update(properties)) {
74 if(listener != null) {
75 listener.onDatastoreContextUpdated(introspector.newContextFactory());
79 LOG.debug("No Configuration found for {}", CONFIG_ID);
81 } catch (IOException e) {
82 LOG.error("Error obtaining Configuration for pid {}", CONFIG_ID, e);
83 } catch(IllegalStateException e) {
84 // Ignore - indicates the bundleContext has been closed.
87 bundleContext.ungetService(configAdminServiceReference);
88 } catch (Exception e) {
89 LOG.debug("Error from ungetService", e);
98 if(configListenerServiceRef != null) {
99 configListenerServiceRef.unregister();
103 private class DatastoreConfigurationListener implements ConfigurationListener {
105 public void configurationEvent(ConfigurationEvent event) {
106 if(CONFIG_ID.equals(event.getPid()) && event.getType() == ConfigurationEvent.CM_UPDATED) {
107 LOG.debug("configurationEvent: config {} was updated", CONFIG_ID);
108 overlaySettings(event.getReference());