Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / factoriesresolver / HardcodedModuleFactoriesResolver.java
index 04f651f6d301e39d890aea81270e05c458d33853..b6cc61e1e3175c289ea607ee4f405876819ef045 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -7,21 +7,35 @@
  */
 package org.opendaylight.controller.config.manager.impl.factoriesresolver;
 
-import java.util.Arrays;
-import java.util.List;
-
+import java.util.AbstractMap;
+import java.util.HashMap;
+import java.util.Map;
 import org.opendaylight.controller.config.spi.ModuleFactory;
+import org.osgi.framework.BundleContext;
 
 public class HardcodedModuleFactoriesResolver implements ModuleFactoriesResolver {
-    private final List<ModuleFactory> list;
+    private final Map<String, Map.Entry<ModuleFactory, BundleContext>> factories;
 
-    public HardcodedModuleFactoriesResolver(ModuleFactory... list) {
-        this.list = Arrays.asList(list);
+    public HardcodedModuleFactoriesResolver(final BundleContext bundleContext, final ModuleFactory... list) {
+        this.factories = new HashMap<>(list.length);
+        for (ModuleFactory moduleFactory : list) {
+            String moduleName = moduleFactory.getImplementationName();
+            if (moduleName == null || moduleName.isEmpty()) {
+                throw new IllegalStateException("Invalid implementation name for " + moduleFactory);
+            }
+            Map.Entry<ModuleFactory, BundleContext> conflicting = factories.get(moduleName);
+            if (conflicting == null) {
+                factories.put(moduleName, new AbstractMap.SimpleEntry<>(moduleFactory, bundleContext));
+            } else {
+                throw new IllegalArgumentException(String.format(
+                        "Module name is not unique. Found two conflicting factories with same name '%s':\n\t%s\n\t%s\n",
+                        moduleName, conflicting.getKey(), moduleFactory));
+            }
+        }
     }
 
     @Override
-    public List<ModuleFactory> getAllFactories() {
-        return list;
+    public Map<String, Map.Entry<ModuleFactory, BundleContext>> getAllFactories() {
+        return factories;
     }
-
 }