Remove static state and default beans from RuntimeMappingModuleFactory,SchemaServiceI... 96/5496/1
authorTomas Olvecky <tolvecky@cisco.com>
Fri, 28 Feb 2014 10:27:13 +0000 (11:27 +0100)
committerTomas Olvecky <tolvecky@cisco.com>
Fri, 28 Feb 2014 10:45:16 +0000 (11:45 +0100)
Remove static reference from factories as this breaks tests: it is useful to tear down config subsystem
between tests, but there is no way to clean up static fields.
Since default beans are discouraged, rework those factories to enforce instance naming so that only a single
instance can be created.
01-md-sal.xml already creates both modules.

Change-Id: I5efbe12b349ae82bfff12ae9a767505a66df2f90
Signed-off-by: Tomas Olvecky <tolvecky@cisco.com>
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/config/yang/md/sal/binding/impl/RuntimeMappingModuleFactory.java
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/config/yang/md/sal/dom/impl/SchemaServiceImplSingletonModuleFactory.java

index 64e23e887ccdba0f7094733e489d7d58d31d2a1c..407b41d29df1df9afcad7473ead9fd3a9dc464a6 100644 (file)
@@ -7,49 +7,34 @@
  */
 package org.opendaylight.controller.config.yang.md.sal.binding.impl;
 
-import java.util.Collections;
-import java.util.Set;
-
 import org.opendaylight.controller.config.api.DependencyResolver;
-import org.opendaylight.controller.config.api.DependencyResolverFactory;
-import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
-import org.opendaylight.controller.config.api.ModuleIdentifier;
-import org.opendaylight.controller.config.spi.Module;
 import org.osgi.framework.BundleContext;
 
+import static com.google.common.base.Preconditions.checkArgument;
+
 /**
-*
-*/
+ *
+ */
 public class RuntimeMappingModuleFactory extends
         org.opendaylight.controller.config.yang.md.sal.binding.impl.AbstractRuntimeMappingModuleFactory {
 
-    private static RuntimeMappingModule SINGLETON = null;
-    private static ModuleIdentifier IDENTIFIER = new ModuleIdentifier(NAME, "runtime-mapping-singleton");
+    public static final String SINGLETON_NAME = "runtime-mapping-singleton";
 
     @Override
-    public Module createModule(String instanceName, DependencyResolver dependencyResolver, BundleContext bundleContext) {
-        throw new UnsupportedOperationException("Only default instance supported");
-    }
-
-    @Override
-    public Module createModule(String instanceName, DependencyResolver dependencyResolver,
-            DynamicMBeanWithInstance old, BundleContext bundleContext) throws Exception {
-        RuntimeMappingModule module = (RuntimeMappingModule) super.createModule(instanceName, dependencyResolver, old,
-                bundleContext);
+    public RuntimeMappingModule instantiateModule(String instanceName, DependencyResolver dependencyResolver, RuntimeMappingModule  oldModule, AutoCloseable oldInstance, BundleContext bundleContext) {
+        checkArgument(SINGLETON_NAME.equals(instanceName),"Illegal instance name '" + instanceName + "', only allowed name is " + SINGLETON_NAME);
+        RuntimeMappingModule module = super.instantiateModule(instanceName, dependencyResolver, oldModule, oldInstance, bundleContext);
+        // FIXME bundle context should not be passed around
         module.setBundleContext(bundleContext);
         return module;
     }
 
     @Override
-    public Set<RuntimeMappingModule> getDefaultModules(DependencyResolverFactory dependencyResolverFactory,
-            BundleContext bundleContext) {
-        if (SINGLETON == null) {
-            DependencyResolver dependencyResolver = dependencyResolverFactory.createDependencyResolver(IDENTIFIER);
-            SINGLETON = new RuntimeMappingModule(IDENTIFIER, dependencyResolver);
-            SINGLETON.setBundleContext(bundleContext);
-        }
-
-        return Collections.singleton(SINGLETON);
+    public RuntimeMappingModule  instantiateModule(String instanceName, DependencyResolver dependencyResolver, BundleContext bundleContext) {
+        checkArgument(SINGLETON_NAME.equals(instanceName),"Illegal instance name '" + instanceName + "', only allowed name is " + SINGLETON_NAME);
+        RuntimeMappingModule module = super.instantiateModule(instanceName, dependencyResolver, bundleContext);
+        // FIXME bundle context should not be passed around
+        module.setBundleContext(bundleContext);
+        return module;
     }
-
 }
index 22ad3fde0029707302201cf079ce3bdfc12295b1..0908fe573bbf28ba4d285e046f13e14ce5ece55f 100644 (file)
@@ -7,38 +7,31 @@
  */
 package org.opendaylight.controller.config.yang.md.sal.dom.impl;
 
-import java.util.Collections;
-import java.util.Set;
-
 import org.opendaylight.controller.config.api.DependencyResolver;
-import org.opendaylight.controller.config.api.DependencyResolverFactory;
-import org.opendaylight.controller.config.api.ModuleIdentifier;
-import org.opendaylight.controller.config.spi.Module;
 import org.osgi.framework.BundleContext;
 
-/**
-*
-*/
+import static com.google.common.base.Preconditions.checkArgument;
+
 public class SchemaServiceImplSingletonModuleFactory extends
         org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractSchemaServiceImplSingletonModuleFactory {
 
-    private static final ModuleIdentifier IDENTIFIER = new ModuleIdentifier(NAME, "yang-schema-service");
-    public static SchemaServiceImplSingletonModule SINGLETON;
+    public static final String SINGLETON_NAME = "yang-schema-service";
 
     @Override
-    public Module createModule(String instanceName, DependencyResolver dependencyResolver, BundleContext bundleContext) {
-        throw new UnsupportedOperationException("Only default instance supported.");
+    public SchemaServiceImplSingletonModule  instantiateModule(String instanceName, DependencyResolver dependencyResolver, SchemaServiceImplSingletonModule  oldModule, AutoCloseable oldInstance, BundleContext bundleContext) {
+        checkArgument(SINGLETON_NAME.equals(instanceName),"Illegal instance name '" + instanceName + "', only allowed name is " + SINGLETON_NAME);
+        SchemaServiceImplSingletonModule module = super.instantiateModule(instanceName, dependencyResolver, oldModule, oldInstance, bundleContext);
+        // FIXME bundle context should not be passed around
+        module.setBundleContext(bundleContext);
+        return module;
     }
 
     @Override
-    public Set<SchemaServiceImplSingletonModule> getDefaultModules(DependencyResolverFactory dependencyResolverFactory,
-            BundleContext bundleContext) {
-        DependencyResolver dependencyResolver = dependencyResolverFactory.createDependencyResolver(IDENTIFIER);
-
-        if (SINGLETON == null) {
-            SINGLETON = new SchemaServiceImplSingletonModule(IDENTIFIER, dependencyResolver);
-            SINGLETON.setBundleContext(bundleContext);
-        }
-        return Collections.singleton(SINGLETON);
+    public SchemaServiceImplSingletonModule  instantiateModule(String instanceName, DependencyResolver dependencyResolver, BundleContext bundleContext) {
+        checkArgument(SINGLETON_NAME.equals(instanceName),"Illegal instance name '" + instanceName + "', only allowed name is " + SINGLETON_NAME);
+        SchemaServiceImplSingletonModule module = super.instantiateModule(instanceName, dependencyResolver, bundleContext);
+        // FIXME bundle context should not be passed around
+        module.setBundleContext(bundleContext);
+        return module;
     }
 }