Mechanical code cleanup (config)
[controller.git] / opendaylight / config / netty-threadgroup-config / src / main / java / org / opendaylight / controller / config / yang / netty / threadgroup / NettyThreadgroupModule.java
index c5b726cd3c6bdc7bbb8a22dac88ab303449106b9..2ae57156b412f2e19ed3040acce8fcdf2132b259 100644 (file)
 */
 package org.opendaylight.controller.config.yang.netty.threadgroup;
 
-import io.netty.channel.nio.NioEventLoopGroup;
-import java.util.concurrent.TimeUnit;
+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
 {
+    private BundleContext bundleContext;
+
     public NettyThreadgroupModule(org.opendaylight.controller.config.api.ModuleIdentifier name, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
         super(name, dependencyResolver);
     }
@@ -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());
-    }
-
-
-    private class NioEventLoopGroupCloseable extends NioEventLoopGroup implements AutoCloseable {
+    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);
 
+        return Reflection.newProxy(AutoCloseableEventLoopGroupInterface.class, new AbstractInvocationHandler() {
+            @Override
+            protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
+                if (method.getName().equals("close")) {
+                    tracker.close();
+                    return null;
+                } else {
+                    return method.invoke(group, args);
+                }
+            }
+        });
+    }
 
-        public NioEventLoopGroupCloseable(int threadCount) {
-            super(threadCount);
-        }
-
-        public NioEventLoopGroupCloseable() {
-            super();
-        }
+    public void setBundleContext(BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+    }
 
-        @Override
-        public void close() throws Exception {
-            shutdownGracefully(0, 1, TimeUnit.SECONDS);
-        }
+    private interface AutoCloseableEventLoopGroupInterface extends EventLoopGroup, AutoCloseable {
     }
 }