Fix Eclipse warnings in config-manager
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / BundleContextBackedModuleFactoriesResolver.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.manager.impl.osgi;
9
10 import java.util.AbstractMap;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.Map;
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.osgi.framework.InvalidSyntaxException;
18 import org.osgi.framework.ServiceReference;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Retrieves list of currently registered Module Factories using bundlecontext.
24  */
25 public class BundleContextBackedModuleFactoriesResolver implements ModuleFactoriesResolver {
26     private static final Logger LOG = LoggerFactory.getLogger(BundleContextBackedModuleFactoriesResolver.class);
27     private final BundleContext bundleContext;
28
29     public BundleContextBackedModuleFactoriesResolver(
30             final BundleContext bundleContext) {
31         this.bundleContext = bundleContext;
32     }
33
34     @Override
35     public Map<String, Map.Entry<ModuleFactory, BundleContext>> getAllFactories() {
36         Collection<ServiceReference<ModuleFactory>> serviceReferences;
37         try {
38             serviceReferences = bundleContext.getServiceReferences(ModuleFactory.class, null);
39         } catch (InvalidSyntaxException e) {
40             throw new IllegalStateException(e);
41         }
42         Map<String, Map.Entry<ModuleFactory, BundleContext>> result = new HashMap<>(serviceReferences.size());
43         for (ServiceReference<ModuleFactory> serviceReference : serviceReferences) {
44             ModuleFactory factory = bundleContext.getService(serviceReference);
45             // null if the service is not registered, the service object
46             // returned by a ServiceFactory does not
47             // implement the classes under which it was registered or the
48             // ServiceFactory threw an exception.
49             if (factory == null) {
50                 throw new NullPointerException("ServiceReference of class" + serviceReference.getClass() + "not found.");
51             }
52
53             String moduleName = factory.getImplementationName();
54             if (moduleName == null || moduleName.isEmpty()) {
55                 throw new IllegalStateException(
56                         "Invalid implementation name for " + factory);
57             }
58             if (serviceReference.getBundle() == null || serviceReference.getBundle().getBundleContext() == null) {
59                 throw new NullPointerException("Bundle context of " + factory + " ModuleFactory not found.");
60             }
61             LOG.debug("Reading factory {} {}", moduleName, factory);
62
63             Map.Entry<ModuleFactory, BundleContext> conflicting = result.get(moduleName);
64             if (conflicting != null) {
65                 String error = String.format(
66                     "Module name is not unique. Found two conflicting factories with same name '%s': '%s' '%s'",
67                     moduleName, conflicting.getKey(), factory);
68                 LOG.error(error);
69                 throw new IllegalArgumentException(error);
70             }
71
72             result.put(moduleName, new AbstractMap.SimpleImmutableEntry<>(factory,
73                     serviceReference.getBundle().getBundleContext()));
74         }
75         return result;
76     }
77 }