Merge "included the nodetype in the congruence check. also handled the condition...
[controller.git] / opendaylight / config / netty-event-executor-config / src / main / java / org / opendaylight / controller / config / yang / netty / eventexecutor / AutoCloseableEventExecutor.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.yang.netty.eventexecutor;
9
10 import com.google.common.reflect.AbstractInvocationHandler;
11 import com.google.common.reflect.Reflection;
12 import io.netty.util.concurrent.EventExecutor;
13
14 import java.lang.reflect.Method;
15 import java.util.concurrent.TimeUnit;
16
17 public interface AutoCloseableEventExecutor extends EventExecutor, AutoCloseable {
18
19
20     public static class CloseableEventExecutorMixin implements AutoCloseable {
21         public static final int DEFAULT_SHUTDOWN_SECONDS = 1;
22         private final EventExecutor eventExecutor;
23
24         public CloseableEventExecutorMixin(EventExecutor eventExecutor) {
25             this.eventExecutor = eventExecutor;
26         }
27
28         @Override
29         public void close() {
30             eventExecutor.shutdownGracefully(0, DEFAULT_SHUTDOWN_SECONDS, TimeUnit.SECONDS);
31         }
32
33
34         public static AutoCloseable createCloseableProxy(final EventExecutor eventExecutor) {
35             final CloseableEventExecutorMixin closeableGlobalEventExecutorMixin =
36                     new CloseableEventExecutorMixin(eventExecutor);
37             return Reflection.newProxy(AutoCloseableEventExecutor.class, new AbstractInvocationHandler() {
38                 @Override
39                 protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
40                     if (method.getName().equals("close")) {
41                         closeableGlobalEventExecutorMixin.close();
42                         return null;
43                     } else {
44                         return method.invoke(eventExecutor, args);
45                     }
46                 }
47             });
48         }
49
50
51     }
52 }