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 org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
11 import org.opendaylight.controller.config.spi.ModuleFactory;
12 import org.osgi.framework.BundleContext;
13 import org.osgi.framework.InvalidSyntaxException;
14 import org.osgi.framework.ServiceReference;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
18 import java.util.AbstractMap;
19 import java.util.Collection;
20 import java.util.HashMap;
24 * Retrieves list of currently registered Module Factories using bundlecontext.
26 public class BundleContextBackedModuleFactoriesResolver implements
27 ModuleFactoriesResolver {
28 private static final Logger logger = LoggerFactory
29 .getLogger(BundleContextBackedModuleFactoriesResolver.class);
30 private final BundleContext bundleContext;
32 public BundleContextBackedModuleFactoriesResolver(
33 BundleContext bundleContext) {
34 this.bundleContext = bundleContext;
38 public Map<String, Map.Entry<ModuleFactory, BundleContext>> getAllFactories() {
39 Collection<ServiceReference<ModuleFactory>> serviceReferences;
41 serviceReferences = bundleContext.getServiceReferences(
42 ModuleFactory.class, null);
43 } catch (InvalidSyntaxException e) {
44 throw new IllegalStateException(e);
46 Map<String, Map.Entry<ModuleFactory, BundleContext>> result = new HashMap<>(serviceReferences.size());
47 for (ServiceReference<ModuleFactory> serviceReference : serviceReferences) {
48 ModuleFactory factory = bundleContext.getService(serviceReference);
49 // null if the service is not registered, the service object
50 // returned by a ServiceFactory does not
51 // implement the classes under which it was registered or the
52 // ServiceFactory threw an exception.
54 throw new NullPointerException("ServiceReference of class" + serviceReference.getClass() + "not found.");
57 String moduleName = factory.getImplementationName();
58 if (moduleName == null || moduleName.isEmpty()) {
59 throw new IllegalStateException(
60 "Invalid implementation name for " + factory);
62 if (serviceReference.getBundle() == null || serviceReference.getBundle().getBundleContext() == null) {
63 throw new NullPointerException("Bundle context of " + factory + " ModuleFactory not found.");
65 logger.debug("Reading factory {} {}", moduleName, factory);
67 Map.Entry<ModuleFactory, BundleContext> conflicting = result.get(moduleName);
68 if (conflicting != null) {
70 .format("Module name is not unique. Found two conflicting factories with same name '%s': '%s' '%s'",
71 moduleName, conflicting.getKey(), factory);
73 throw new IllegalArgumentException(error);
75 result.put(moduleName, new AbstractMap.SimpleImmutableEntry<>(factory,
76 serviceReference.getBundle().getBundleContext()));