X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-manager%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fmanager%2Fimpl%2Fosgi%2FBundleContextBackedModuleFactoriesResolver.java;h=0cbbbab912d3cce521fda7de34837092613b9a47;hp=77bfc495b8a0b3c12c0140f4ef5f59a8c78777a1;hb=0e74e5866c506da072cae3a2897335df7170f958;hpb=9fb64948564e252018f9b1e13e7cea2c92f991aa diff --git a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/BundleContextBackedModuleFactoriesResolver.java b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/BundleContextBackedModuleFactoriesResolver.java index 77bfc495b8..0cbbbab912 100644 --- a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/BundleContextBackedModuleFactoriesResolver.java +++ b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/BundleContextBackedModuleFactoriesResolver.java @@ -7,47 +7,70 @@ */ package org.opendaylight.controller.config.manager.impl.osgi; -import java.util.ArrayList; +import java.util.AbstractMap; import java.util.Collection; -import java.util.List; - +import java.util.HashMap; +import java.util.Map; import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver; import org.opendaylight.controller.config.spi.ModuleFactory; import org.osgi.framework.BundleContext; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Retrieves list of currently registered Module Factories using bundlecontext. */ -public class BundleContextBackedModuleFactoriesResolver implements - ModuleFactoriesResolver { +public class BundleContextBackedModuleFactoriesResolver implements ModuleFactoriesResolver { + private static final Logger LOG = LoggerFactory.getLogger(BundleContextBackedModuleFactoriesResolver.class); private final BundleContext bundleContext; public BundleContextBackedModuleFactoriesResolver( - BundleContext bundleContext) { + final BundleContext bundleContext) { this.bundleContext = bundleContext; } @Override - public List getAllFactories() { + public Map> getAllFactories() { Collection> serviceReferences; try { - serviceReferences = bundleContext.getServiceReferences( - ModuleFactory.class, null); + serviceReferences = bundleContext.getServiceReferences(ModuleFactory.class, null); } catch (InvalidSyntaxException e) { throw new IllegalStateException(e); } - List result = new ArrayList<>(serviceReferences.size()); + Map> result = new HashMap<>(serviceReferences.size()); for (ServiceReference serviceReference : serviceReferences) { - ModuleFactory service = bundleContext.getService(serviceReference); + ModuleFactory factory = bundleContext.getService(serviceReference); // null if the service is not registered, the service object // returned by a ServiceFactory does not // implement the classes under which it was registered or the // ServiceFactory threw an exception. - if (service != null) { - result.add(service); + if (factory == null) { + throw new NullPointerException("ServiceReference of class" + serviceReference.getClass() + "not found."); + } + + String moduleName = factory.getImplementationName(); + if (moduleName == null || moduleName.isEmpty()) { + throw new IllegalStateException( + "Invalid implementation name for " + factory); + } + if (serviceReference.getBundle() == null || serviceReference.getBundle().getBundleContext() == null) { + throw new NullPointerException("Bundle context of " + factory + " ModuleFactory not found."); } + LOG.debug("Reading factory {} {}", moduleName, factory); + + Map.Entry conflicting = result.get(moduleName); + if (conflicting != null) { + String error = String.format( + "Module name is not unique. Found two conflicting factories with same name '%s': '%s' '%s'", + moduleName, conflicting.getKey(), factory); + LOG.error(error); + throw new IllegalArgumentException(error); + } + + result.put(moduleName, new AbstractMap.SimpleImmutableEntry<>(factory, + serviceReference.getBundle().getBundleContext())); } return result; }