BUG-981: improve RuntimeGeneratedMappingServiceImpl clarity 72/6872/4
authorRobert Varga <rovarga@cisco.com>
Sat, 10 May 2014 21:55:10 +0000 (23:55 +0200)
committerRobert Varga <rovarga@cisco.com>
Sun, 11 May 2014 15:27:10 +0000 (17:27 +0200)
This clarifies some of the locking interactions and well as it isolates
privay value.

Change-Id: Ic8961c9f9d32307fdd582191cab55bc633d081c5
Signed-off-by: Robert Varga <rovarga@cisco.com>
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/RuntimeGeneratedMappingServiceImpl.java

index eacbd93eb55e3c65c1b8d3dc03c846093d390ef7..dcf96793215d01b95b62ead03578df7d9e43495a 100644 (file)
@@ -23,7 +23,6 @@ import java.util.concurrent.Future;
 
 import javassist.ClassPool;
 
-import org.eclipse.xtext.xbase.lib.Extension;
 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
 import org.opendaylight.yangtools.binding.generator.util.Types;
@@ -82,83 +81,67 @@ public class RuntimeGeneratedMappingServiceImpl implements BindingIndependentMap
     private final HashMultimap<Type, SettableFuture<Type>> promisedTypes = HashMultimap.create();
     private final ClassLoadingStrategy classLoadingStrategy;
 
-    // FIXME: how is this thread-safe?
+    // FIXME: will become final
     private ClassPool pool;
-
-    // FIXME: how is this thread-safe?
-    @Extension
     private TransformerGenerator binding;
-
-    // FIXME: how is this thread-safe?
-    @Extension
     private LazyGeneratedCodecRegistry registry;
 
-    // FIXME: how is this thread-safe?
     private SchemaContext schemaContext;
 
+    @Deprecated
     public RuntimeGeneratedMappingServiceImpl() {
         this(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy());
     }
 
+    @Deprecated
     public RuntimeGeneratedMappingServiceImpl(final ClassLoadingStrategy strat) {
         classLoadingStrategy = strat;
     }
 
-    public ClassPool getPool() {
-        return this.pool;
-    }
-
-    public void setPool(final ClassPool pool) {
-        this.pool = pool;
-    }
-
-    @Override
-    public SchemaContext getSchemaContext() {
-        return schemaContext;
-    }
-
-    public void setSchemaContext(final SchemaContext schemaContext) {
-        this.schemaContext = schemaContext;
+    public RuntimeGeneratedMappingServiceImpl(final ClassPool pool) {
+        this(pool, GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy());
     }
 
-    public TransformerGenerator getBinding() {
-        return this.binding;
-    }
-
-    public void setBinding(final TransformerGenerator binding) {
-        this.binding = binding;
-    }
+    public RuntimeGeneratedMappingServiceImpl(final ClassPool pool, final ClassLoadingStrategy strat) {
+        this.pool = Preconditions.checkNotNull(pool);
+        this.classLoadingStrategy = Preconditions.checkNotNull(strat);
 
-    public LazyGeneratedCodecRegistry getRegistry() {
-        return registry;
+        // FIXME: merge into constructor once legacy init() is removed
+        doInit();
     }
 
-    public void setRegistry(final LazyGeneratedCodecRegistry registry) {
-        this.registry = registry;
-    }
+    private void doInit() {
+        binding = new TransformerGenerator(pool);
+        registry = new LazyGeneratedCodecRegistry(this, classLoadingStrategy);
 
-    public ConcurrentMap<Type, GeneratedTypeBuilder> getTypeToDefinition() {
-        return typeToDefinition;
-    }
+        registry.setGenerator(binding);
+        // binding.staticFieldsInitializer = registry
+        binding.setListener(registry);
+        binding.setTypeToDefinition(typeToDefinition);
+        binding.setTypeToSchemaNode(typeToSchemaNode);
+        binding.setTypeDefinitions(typeDefinitions);
 
-    public ConcurrentMap<Type, Type> getTypeDefinitions() {
-        return typeDefinitions;
+        // if (ctx !== null) {
+        // listenerRegistration = ctx.registerService(SchemaServiceListener,
+        // this, new Hashtable<String, String>());
+        // }
     }
 
-    public ConcurrentMap<Type, SchemaNode> getTypeToSchemaNode() {
-        return typeToSchemaNode;
+    @Deprecated
+    public void setPool(final ClassPool pool) {
+        this.pool = pool;
     }
 
-    public ConcurrentMap<Type, Set<QName>> getServiceTypeToRpc() {
-        return serviceTypeToRpc;
+    @Override
+    public synchronized SchemaContext getSchemaContext() {
+        return schemaContext;
     }
 
     @Override
-    public void onGlobalContextUpdated(final SchemaContext arg0) {
-        this.setSchemaContext(arg0);
-        this.recreateBindingContext(arg0);
-        LazyGeneratedCodecRegistry _registry = this.getRegistry();
-        _registry.onGlobalContextUpdated(arg0);
+    public synchronized void onGlobalContextUpdated(final SchemaContext context) {
+        this.schemaContext = context;
+        this.recreateBindingContext(context);
+        this.registry.onGlobalContextUpdated(context);
     }
 
     private void recreateBindingContext(final SchemaContext schemaContext) {
@@ -292,7 +275,7 @@ public class RuntimeGeneratedMappingServiceImpl implements BindingIndependentMap
             this.waitForSchema(arg.getType());
         }
 
-        final InstanceIdentifierCodec c = getRegistry().getInstanceIdentifierCodec();
+        final InstanceIdentifierCodec c = registry.getInstanceIdentifierCodec();
         Preconditions.checkState(c != null, "InstanceIdentifierCodec not present");
         return c.serialize(path);
     }
@@ -310,7 +293,7 @@ public class RuntimeGeneratedMappingServiceImpl implements BindingIndependentMap
             // FIXME: deprecate use without iid
             final org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends DataObject> wildcardedPath = createWildcarded(path);
 
-            final DataContainerCodec<? extends DataContainer> transformer = getRegistry().getCodecForDataObject(container);
+            final DataContainerCodec<? extends DataContainer> transformer = registry.getCodecForDataObject(container);
             Preconditions.checkState(transformer != null, "Failed to find codec for type %s", container);
 
             final ValueWithQName<? extends DataContainer> deserialize = transformer.deserialize(domData, wildcardedPath);
@@ -328,7 +311,7 @@ public class RuntimeGeneratedMappingServiceImpl implements BindingIndependentMap
     @Override
     public org.opendaylight.yangtools.yang.binding.InstanceIdentifier<? extends Object> fromDataDom(final InstanceIdentifier entry) throws DeserializationException {
         try {
-            final InstanceIdentifierCodec c = getRegistry().getInstanceIdentifierCodec();
+            final InstanceIdentifierCodec c = registry.getInstanceIdentifierCodec();
             Preconditions.checkState(c != null, "InstanceIdentifierCodec not present");
             return c.deserialize(entry);
         } catch (Exception e) {
@@ -339,7 +322,7 @@ public class RuntimeGeneratedMappingServiceImpl implements BindingIndependentMap
 
     @Override
     public CodecRegistry getCodecRegistry() {
-        return this.getRegistry();
+        return this.registry;
     }
 
     private void updateBindingFor(final Map<SchemaPath, GeneratedTypeBuilder> map, final SchemaContext module) {
@@ -356,21 +339,9 @@ public class RuntimeGeneratedMappingServiceImpl implements BindingIndependentMap
         }
     }
 
+    @Deprecated
     public void init() {
-        binding = new TransformerGenerator(pool);
-        registry = new LazyGeneratedCodecRegistry(this, classLoadingStrategy);
-
-        registry.setGenerator(binding);
-        // binding.staticFieldsInitializer = registry
-        binding.setListener(registry);
-        binding.setTypeToDefinition(typeToDefinition);
-        binding.setTypeToSchemaNode(typeToSchemaNode);
-        binding.setTypeDefinitions(typeDefinitions);
-
-        // if (ctx !== null) {
-        // listenerRegistration = ctx.registerService(SchemaServiceListener,
-        // this, new Hashtable<String, String>());
-        // }
+        doInit();
     }
 
     @Override
@@ -433,7 +404,7 @@ public class RuntimeGeneratedMappingServiceImpl implements BindingIndependentMap
     }
 
     @Override
-    public Optional<Class<? extends RpcService>> getRpcServiceClassFor(final String namespace, final String revision) {
+    public synchronized Optional<Class<? extends RpcService>> getRpcServiceClassFor(final String namespace, final String revision) {
         Module module = null;
         if (schemaContext != null) {
             module = schemaContext.findModuleByName(namespace, QName.parseRevision(revision));