Fix pax exam issue
[controller.git] / opendaylight / config / netty-threadgroup-config / src / main / java / org / opendaylight / controller / config / yang / netty / threadgroup / NettyThreadgroupModule.java
index 3d5a3bf4adf00e78ce5537a884eeb3ad9142d42c..d81a9dcb3067d4abdb2990c2442951657bc96b04 100644 (file)
 */
 package org.opendaylight.controller.config.yang.netty.threadgroup;
 
-import io.netty.channel.nio.NioEventLoopGroup;
-
+import com.google.common.reflect.AbstractInvocationHandler;
+import com.google.common.reflect.Reflection;
+import io.netty.channel.EventLoopGroup;
+import java.lang.reflect.Method;
 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
+import org.opendaylight.controller.config.api.osgi.WaitingServiceTracker;
+import org.osgi.framework.BundleContext;
 
 /**
-*
-*/
+ * @deprecated Replaced by blueprint wiring
+ */
+@Deprecated
 public final class NettyThreadgroupModule extends org.opendaylight.controller.config.yang.netty.threadgroup.AbstractNettyThreadgroupModule
 {
-    public NettyThreadgroupModule(org.opendaylight.controller.config.api.ModuleIdentifier name, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
+    private BundleContext bundleContext;
+
+    public NettyThreadgroupModule(final org.opendaylight.controller.config.api.ModuleIdentifier name, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
         super(name, dependencyResolver);
     }
 
-    public NettyThreadgroupModule(org.opendaylight.controller.config.api.ModuleIdentifier name, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, NettyThreadgroupModule oldModule, java.lang.AutoCloseable oldInstance) {
+    public NettyThreadgroupModule(final org.opendaylight.controller.config.api.ModuleIdentifier name, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final NettyThreadgroupModule oldModule, final java.lang.AutoCloseable oldInstance) {
         super(name, dependencyResolver, oldModule, oldInstance);
     }
 
@@ -43,25 +50,30 @@ public final class NettyThreadgroupModule extends org.opendaylight.controller.co
     }
 
     @Override
-    public java.lang.AutoCloseable createInstance() {
-        return getThreadCount()==null ? new NioEventLoopGroupCloseable() : new NioEventLoopGroupCloseable(getThreadCount());
-    }
-
+    public AutoCloseable createInstance() {
+        // The service is provided via blueprint so wait for and return it here for backwards compatibility.
+        String typeFilter = String.format("(type=%s)", getIdentifier().getInstanceName());
+        final WaitingServiceTracker<EventLoopGroup> tracker = WaitingServiceTracker.create(
+                EventLoopGroup.class, bundleContext, typeFilter);
+        final EventLoopGroup group = tracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
 
-    private class NioEventLoopGroupCloseable extends NioEventLoopGroup implements AutoCloseable {
-
-
-        public NioEventLoopGroupCloseable(int threadCount) {
-            super(threadCount);
-        }
+        return Reflection.newProxy(AutoCloseableEventLoopGroupInterface.class, new AbstractInvocationHandler() {
+            @Override
+            protected Object handleInvocation(final Object proxy, final Method method, final Object[] args) throws Throwable {
+                if (method.getName().equals("close")) {
+                    tracker.close();
+                    return null;
+                } else {
+                    return method.invoke(group, args);
+                }
+            }
+        });
+    }
 
-        public NioEventLoopGroupCloseable() {
-            super();
-        }
+    public void setBundleContext(final BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+    }
 
-        @Override
-        public void close() throws Exception {
-            shutdownGracefully();
-        }
+    private interface AutoCloseableEventLoopGroupInterface extends EventLoopGroup, AutoCloseable {
     }
 }