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 java.util.AbstractMap;
11 import java.util.HashMap;
13 import java.util.Map.Entry;
14 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
15 import org.opendaylight.controller.config.spi.ModuleFactory;
16 import org.osgi.framework.BundleContext;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
21 * Retrieves list of currently registered Module Factories using bundlecontext.
23 public class BundleContextBackedModuleFactoriesResolver implements
24 ModuleFactoriesResolver {
25 private static final Logger LOG = LoggerFactory
26 .getLogger(BundleContextBackedModuleFactoriesResolver.class);
27 private ModuleFactoryBundleTracker moduleFactoryBundleTracker;
29 public BundleContextBackedModuleFactoriesResolver() {
32 public void setModuleFactoryBundleTracker(ModuleFactoryBundleTracker moduleFactoryBundleTracker) {
33 this.moduleFactoryBundleTracker = moduleFactoryBundleTracker;
37 public Map<String, Map.Entry<ModuleFactory, BundleContext>> getAllFactories() {
38 Map<String, Map.Entry<ModuleFactory, BundleContext>> result = new HashMap<>();
39 for(Entry<ModuleFactory, BundleContext> entry: moduleFactoryBundleTracker.getModuleFactoryEntries()) {
40 ModuleFactory factory = entry.getKey();
41 BundleContext bundleContext = entry.getValue();
42 String moduleName = factory .getImplementationName();
43 if (moduleName == null || moduleName.isEmpty()) {
44 throw new IllegalStateException("Invalid implementation name for " + factory);
47 LOG.debug("Processing factory {} {}", moduleName, factory);
49 Map.Entry<ModuleFactory, BundleContext> conflicting = result.get(moduleName);
50 if (conflicting != null) {
52 .format("Module name is not unique. Found two conflicting factories with same name '%s': '%s' '%s'",
53 moduleName, conflicting.getKey(), factory);
55 throw new IllegalArgumentException(error);
57 result.put(moduleName, new AbstractMap.SimpleImmutableEntry<>(factory, bundleContext));