Remove global BindingToNormalizedNodeCodec instance
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / BindingToNormalizedNodeCodecFactory.java
index 180f7b8f45a698bc5e847cd020074a8ae28263bb..bb123a15f1cb6461d8f739b3ccea5e8417c183d8 100644 (file)
@@ -7,51 +7,59 @@
  */
 package org.opendaylight.controller.md.sal.binding.impl;
 
-import java.util.concurrent.atomic.AtomicBoolean;
 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
 import org.opendaylight.controller.sal.core.api.model.SchemaService;
 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.sal.binding.generator.api.ClassLoadingStrategy;
+import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
 
 /**
- * Factory class for creating and initializing the global BindingToNormalizedNodeCodec instance.
+ * Factory class for creating and initializing the BindingToNormalizedNodeCodec instances.
  *
  * @author Thomas Pantelis
  */
 public class BindingToNormalizedNodeCodecFactory {
-    private static final AtomicBoolean INSTANCE_CREATED = new AtomicBoolean();
-    private static volatile BindingToNormalizedNodeCodec instance;
-
     /**
-     * Returns the global BindingToNormalizedNodeCodec instance, creating if necessary. The returned instance
-     * is registered with tthe SchemaService as a SchemaContextListener.
+     * This method is deprecated in favor of newInstance/registerInstance.
      *
      * @param classLoadingStrategy
      * @param schemaService
-     * @return the BindingToNormalizedNodeCodec instance
+     * @return BindingToNormalizedNodeCodec instance
      */
+    @Deprecated
     public static BindingToNormalizedNodeCodec getOrCreateInstance(ClassLoadingStrategy classLoadingStrategy,
-            SchemaService schemaService) {
-        if(!INSTANCE_CREATED.compareAndSet(false, true)) {
-            return instance;
-        }
-
+                            SchemaService schemaService) {
         BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(
                 StreamWriterGenerator.create(SingletonHolder.JAVASSIST));
-        BindingToNormalizedNodeCodec localInstance = new BindingToNormalizedNodeCodec(
-                classLoadingStrategy, codecRegistry, true);
-
-        schemaService.registerSchemaContextListener(localInstance);
-
-        // Publish the BindingToNormalizedNodeCodec instance after we've registered it as a
-        // SchemaContextListener to avoid a race condition by publishing it too early when it isn't
-        // fully initialized.
-        instance = localInstance;
+        BindingToNormalizedNodeCodec instance = new BindingToNormalizedNodeCodec(
+                               classLoadingStrategy, codecRegistry, true);
+        schemaService.registerSchemaContextListener(instance);
         return instance;
     }
 
-    public static BindingToNormalizedNodeCodec getInstance() {
-        return instance;
+    /**
+     * Creates a new BindingToNormalizedNodeCodec instance.
+     *
+     * @param classLoadingStrategy
+     * @return the BindingToNormalizedNodeCodec instance
+     */
+    public static BindingToNormalizedNodeCodec newInstance(ClassLoadingStrategy classLoadingStrategy) {
+        BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(
+                StreamWriterGenerator.create(SingletonHolder.JAVASSIST));
+        return new BindingToNormalizedNodeCodec(classLoadingStrategy, codecRegistry, true);
+    }
+
+    /**
+     * Registers the given instance with the SchemaService as a SchemaContextListener.
+     *
+     * @param instance the BindingToNormalizedNodeCodec instance
+     * @param schemaService the SchemaService.
+     * @return the ListenerRegistration
+     */
+    public static ListenerRegistration<SchemaContextListener> registerInstance(BindingToNormalizedNodeCodec instance,
+            SchemaService schemaService) {
+        return schemaService.registerSchemaContextListener(instance);
     }
 }