Add missing copyright text
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / impl / AbstractRuntimeCodeGenerator.java
index 86003b2aedc2344e86244134ea7870e83cfcd7c6..6c02001fec78baf5a2790990d9bb9419bcc9ba00 100644 (file)
@@ -8,35 +8,29 @@
 package org.opendaylight.controller.sal.binding.codegen.impl;
 
 import com.google.common.base.Supplier;
-
+import com.google.common.collect.Iterables;
 import java.util.Map;
 import java.util.WeakHashMap;
-
 import javassist.ClassPool;
 import javassist.CtClass;
 import javassist.CtMethod;
 import javassist.NotFoundException;
-
 import javax.annotation.concurrent.GuardedBy;
-
-import org.eclipse.xtext.xbase.lib.Extension;
 import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
 import org.opendaylight.controller.sal.binding.codegen.RpcIsNotRoutedException;
 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory;
 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
+import org.opendaylight.yangtools.util.ClassLoaderUtils;
 import org.opendaylight.yangtools.yang.binding.BindingMapping;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.NotificationListener;
 import org.opendaylight.yangtools.yang.binding.RpcService;
 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
-import org.opendaylight.yangtools.yang.binding.util.ClassLoaderUtils;
 
 abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator, NotificationInvokerFactory {
     @GuardedBy("this")
     private final Map<Class<? extends NotificationListener>, RuntimeGeneratedInvokerPrototype> invokerClasses = new WeakHashMap<>();
     private final CtClass brokerNotificationListener;
-
-    @Extension
     protected final JavassistUtils utils;
 
     protected AbstractRuntimeCodeGenerator(final ClassPool pool) {
@@ -84,7 +78,7 @@ abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controll
                      */
                     Thread.currentThread().getContextClassLoader().loadClass(routingPair.getInputType().getName());
                 } else {
-                    throw new RpcIsNotRoutedException("RPC " + method.getName() + " from "+ iface.getName() +" is not routed");
+                    throw new RpcIsNotRoutedException(String.format("RPC %s from %s is not routed", method.getName(), iface.getName()));
                 }
             }
         }
@@ -136,22 +130,17 @@ abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controll
             return invoker;
         }
 
-        utils.getLock().lock();
-        try {
-            synchronized (utils) {
-                invoker = ClassLoaderUtils.withClassLoader(cls.getClassLoader(), new Supplier<RuntimeGeneratedInvokerPrototype>() {
-                    @Override
-                    public RuntimeGeneratedInvokerPrototype get() {
-                        return generateListenerInvoker(cls);
-                    }
-                });
-            }
-
-            invokerClasses.put(cls, invoker);
-            return invoker;
-        } finally {
-            utils.getLock().unlock();
+        synchronized (utils) {
+            invoker = ClassLoaderUtils.withClassLoader(cls.getClassLoader(), new Supplier<RuntimeGeneratedInvokerPrototype>() {
+                @Override
+                public RuntimeGeneratedInvokerPrototype get() {
+                    return generateListenerInvoker(cls);
+                }
+            });
         }
+
+        invokerClasses.put(cls, invoker);
+        return invoker;
     }
 
     @Override
@@ -161,13 +150,8 @@ abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controll
 
     @Override
     public final <T extends RpcService> T getDirectProxyFor(final Class<T> serviceType) {
-        utils.getLock().lock();
-        try {
-            synchronized (utils) {
-                return ClassLoaderUtils.withClassLoader(serviceType.getClassLoader(), directProxySupplier(serviceType));
-            }
-        } finally {
-            utils.getLock().unlock();
+        synchronized (utils) {
+            return ClassLoaderUtils.withClassLoader(serviceType.getClassLoader(), directProxySupplier(serviceType));
         }
     }
 
@@ -179,19 +163,18 @@ abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controll
                 try {
                     return getRpcMetadata(utils.asCtClass(serviceType));
                 } catch (ClassNotFoundException | NotFoundException e) {
-                    throw new IllegalStateException(String.format("Failed to load metadata for class {}", serviceType), e);
+                    throw new IllegalStateException(String.format("Failed to load metadata for class %s", serviceType), e);
                 }
             }
         });
 
-        utils.getLock().lock();
-        try {
-            synchronized (utils) {
-                final T instance = ClassLoaderUtils.withClassLoader(serviceType.getClassLoader(), routerSupplier(serviceType, metadata));
-                return new RpcRouterCodegenInstance<T>(name, serviceType, instance, metadata.getContexts());
-            }
-        } finally {
-            utils.getLock().unlock();
+        if (Iterables.isEmpty(metadata.getContexts())) {
+            throw new RpcIsNotRoutedException("Service doesn't have routing context associated.");
+        }
+
+        synchronized (utils) {
+            final T instance = ClassLoaderUtils.withClassLoader(serviceType.getClassLoader(), routerSupplier(serviceType, metadata));
+            return new RpcRouterCodegenInstance<T>(name, serviceType, instance, metadata.getContexts());
         }
     }