Merge "Remove raw references to Map in XSQL"
[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
26         ModuleFactoriesResolver {
27     private static final Logger LOG = LoggerFactory
28             .getLogger(BundleContextBackedModuleFactoriesResolver.class);
29     private final BundleContext bundleContext;
30
31     public BundleContextBackedModuleFactoriesResolver(
32             BundleContext bundleContext) {
33         this.bundleContext = bundleContext;
34     }
35
36     @Override
37     public Map<String, Map.Entry<ModuleFactory, BundleContext>> getAllFactories() {
38         Collection<ServiceReference<ModuleFactory>> serviceReferences;
39         try {
40             serviceReferences = bundleContext.getServiceReferences(
41                     ModuleFactory.class, null);
42         } catch (InvalidSyntaxException e) {
43             throw new IllegalStateException(e);
44         }
45         Map<String, Map.Entry<ModuleFactory, BundleContext>> result = new HashMap<>(serviceReferences.size());
46         for (ServiceReference<ModuleFactory> serviceReference : serviceReferences) {
47             ModuleFactory factory = bundleContext.getService(serviceReference);
48             // null if the service is not registered, the service object
49             // returned by a ServiceFactory does not
50             // implement the classes under which it was registered or the
51             // ServiceFactory threw an exception.
52             if(factory == null) {
53                 throw new NullPointerException("ServiceReference of class" + serviceReference.getClass() + "not found.");
54             }
55
56             String moduleName = factory.getImplementationName();
57             if (moduleName == null || moduleName.isEmpty()) {
58                 throw new IllegalStateException(
59                         "Invalid implementation name for " + factory);
60             }
61             if (serviceReference.getBundle() == null || serviceReference.getBundle().getBundleContext() == null) {
62                 throw new NullPointerException("Bundle context of " + factory + " ModuleFactory not found.");
63             }
64             LOG.debug("Reading factory {} {}", moduleName, factory);
65
66             Map.Entry<ModuleFactory, BundleContext> conflicting = result.get(moduleName);
67             if (conflicting != null) {
68                 String error = String
69                         .format("Module name is not unique. Found two conflicting factories with same name '%s': '%s' '%s'",
70                                 moduleName, conflicting.getKey(), factory);
71                 LOG.error(error);
72                 throw new IllegalArgumentException(error);
73             } else {
74                 result.put(moduleName, new AbstractMap.SimpleImmutableEntry<>(factory,
75                         serviceReference.getBundle().getBundleContext()));
76             }
77         }
78         return result;
79     }
80 }