Merge "BUG 1839 - HTTP delete of non existing data"
[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 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;
17
18 import java.util.AbstractMap;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 /**
24  * Retrieves list of currently registered Module Factories using bundlecontext.
25  */
26 public class BundleContextBackedModuleFactoriesResolver implements
27         ModuleFactoriesResolver {
28     private static final Logger LOGGER = LoggerFactory
29             .getLogger(BundleContextBackedModuleFactoriesResolver.class);
30     private final BundleContext bundleContext;
31
32     public BundleContextBackedModuleFactoriesResolver(
33             BundleContext bundleContext) {
34         this.bundleContext = bundleContext;
35     }
36
37     @Override
38     public Map<String, Map.Entry<ModuleFactory, BundleContext>> getAllFactories() {
39         Collection<ServiceReference<ModuleFactory>> serviceReferences;
40         try {
41             serviceReferences = bundleContext.getServiceReferences(
42                     ModuleFactory.class, null);
43         } catch (InvalidSyntaxException e) {
44             throw new IllegalStateException(e);
45         }
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.
53             if(factory == null) {
54                 throw new NullPointerException("ServiceReference of class" + serviceReference.getClass() + "not found.");
55             }
56
57             String moduleName = factory.getImplementationName();
58             if (moduleName == null || moduleName.isEmpty()) {
59                 throw new IllegalStateException(
60                         "Invalid implementation name for " + factory);
61             }
62             if (serviceReference.getBundle() == null || serviceReference.getBundle().getBundleContext() == null) {
63                 throw new NullPointerException("Bundle context of " + factory + " ModuleFactory not found.");
64             }
65             LOGGER.debug("Reading factory {} {}", moduleName, factory);
66
67             Map.Entry<ModuleFactory, BundleContext> conflicting = result.get(moduleName);
68             if (conflicting != null) {
69                 String error = String
70                         .format("Module name is not unique. Found two conflicting factories with same name '%s': '%s' '%s'",
71                                 moduleName, conflicting.getKey(), factory);
72                 LOGGER.error(error);
73                 throw new IllegalArgumentException(error);
74             } else {
75                 result.put(moduleName, new AbstractMap.SimpleImmutableEntry<>(factory,
76                         serviceReference.getBundle().getBundleContext()));
77             }
78         }
79         return result;
80     }
81 }