2 * Copyright (c) 2013 Cisco 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.config.manager.impl.osgi;
10 import com.google.common.base.Preconditions;
11 import java.util.Dictionary;
12 import java.util.HashSet;
13 import java.util.Hashtable;
15 import java.util.Map.Entry;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.controller.config.api.ModuleIdentifier;
19 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.ServiceRegistration;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Registers instantiated beans as OSGi services and unregisters these services
27 * if beans are destroyed.
29 public class BeanToOsgiServiceManager {
30 private static final String SERVICE_NAME_OSGI_PROP = "name";
33 * To be called for every created, reconfigured and recreated config bean.
34 * It is expected that before using this method OSGi service registry will
35 * be cleaned from previous registrations.
37 public OsgiRegistration registerToOsgi(final AutoCloseable instance, final ModuleIdentifier moduleIdentifier,
38 final BundleContext bundleContext,
39 final Map<ServiceInterfaceAnnotation, String /* service ref name */> serviceNamesToAnnotations) {
40 return new OsgiRegistration(instance, moduleIdentifier, bundleContext, serviceNamesToAnnotations);
43 public static class OsgiRegistration implements AutoCloseable {
44 private static final Logger LOG = LoggerFactory.getLogger(OsgiRegistration.class);
47 private AutoCloseable instance;
48 private final ModuleIdentifier moduleIdentifier;
50 private final Set<ServiceRegistration<?>> serviceRegistrations;
52 private final Map<ServiceInterfaceAnnotation, String /* service ref name */> serviceNamesToAnnotations;
54 public OsgiRegistration(final AutoCloseable instance, final ModuleIdentifier moduleIdentifier,
55 final BundleContext bundleContext,
56 final Map<ServiceInterfaceAnnotation, String /* service ref name */> serviceNamesToAnnotations) {
57 this.instance = instance;
58 this.moduleIdentifier = moduleIdentifier;
59 this.serviceNamesToAnnotations = serviceNamesToAnnotations;
60 this.serviceRegistrations = registerToSR(instance, bundleContext, serviceNamesToAnnotations);
63 private static Set<ServiceRegistration<?>> registerToSR(final AutoCloseable instance, final BundleContext bundleContext,
64 final Map<ServiceInterfaceAnnotation, String /* service ref name */> serviceNamesToAnnotations) {
65 Set<ServiceRegistration<?>> serviceRegistrations = new HashSet<>();
66 for (Entry<ServiceInterfaceAnnotation, String /* service ref name */> entry : serviceNamesToAnnotations.entrySet()) {
67 ServiceInterfaceAnnotation annotation = entry.getKey();
68 Class<?> requiredInterface = annotation.osgiRegistrationType();
70 if(!annotation.registerToOsgi()) {
71 LOG.debug("registerToOsgi for service interface {} is false - not registering", requiredInterface);
75 Preconditions.checkState(requiredInterface.isInstance(instance), instance.getClass().getName() +
76 " instance should implement " + requiredInterface.getName());
77 Dictionary<String, String> propertiesForOsgi = createProps(entry.getValue());
78 ServiceRegistration<?> serviceRegistration = bundleContext
79 .registerService(requiredInterface.getName(), instance, propertiesForOsgi);
80 serviceRegistrations.add(serviceRegistration);
82 return serviceRegistrations;
86 public synchronized void close() {
87 for (ServiceRegistration<?> serviceRegistration : serviceRegistrations) {
89 serviceRegistration.unregister();
90 } catch(final IllegalStateException e) {
91 LOG.trace("Cannot unregister {}", serviceRegistration, e);
94 serviceRegistrations.clear();
97 public synchronized void updateRegistrations(final Map<ServiceInterfaceAnnotation, String /* service ref name */> newAnnotationMapping,
98 final BundleContext bundleContext, final AutoCloseable newInstance) {
99 boolean notEquals = !this.instance.equals(newInstance);
100 notEquals |= !newAnnotationMapping.equals(serviceNamesToAnnotations);
102 // FIXME: changing from old state to new state can be improved by computing the diff
103 LOG.debug("Detected change in service registrations for {}: old: {}, new: {}", moduleIdentifier,
104 serviceNamesToAnnotations, newAnnotationMapping);
106 this.instance = newInstance;
107 Set<ServiceRegistration<?>> newRegs = registerToSR(instance, bundleContext, newAnnotationMapping);
108 serviceRegistrations.clear();
109 serviceRegistrations.addAll(newRegs);
113 private static Dictionary<String, String> createProps(final String serviceName) {
114 Hashtable<String, String> result = new Hashtable<>();
115 result.put(SERVICE_NAME_OSGI_PROP, serviceName);