99d94d08e386b2c6df5652d017d781afb5aa08df
[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 import io.netty.util.concurrent.GlobalEventExecutor;
14 import io.netty.util.concurrent.ImmediateEventExecutor;
15 import java.lang.reflect.Method;
16 import java.util.concurrent.TimeUnit;
17
18 public interface AutoCloseableEventExecutor extends EventExecutor, AutoCloseable {
19
20
21     public static class CloseableEventExecutorMixin implements AutoCloseable {
22         public static final int DEFAULT_SHUTDOWN_SECONDS = 1;
23         private final EventExecutor eventExecutor;
24
25         public CloseableEventExecutorMixin(EventExecutor eventExecutor) {
26             this.eventExecutor = eventExecutor;
27         }
28
29         @Override
30         public void close() throws Exception {
31             eventExecutor.shutdownGracefully(0, DEFAULT_SHUTDOWN_SECONDS, TimeUnit.SECONDS);
32         }
33
34
35         private static AutoCloseableEventExecutor createCloseableProxy(
36                 final CloseableEventExecutorMixin closeableEventExecutorMixin) {
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                         closeableEventExecutorMixin.close();
42                         return null;
43                     } else {
44                         return method.invoke(closeableEventExecutorMixin.eventExecutor, args);
45                     }
46                 }
47             });
48         }
49
50         public static AutoCloseableEventExecutor globalEventExecutor() {
51             return createCloseableProxy(new CloseableEventExecutorMixin(GlobalEventExecutor.INSTANCE));
52         }
53
54         public static AutoCloseableEventExecutor immediateEventExecutor() {
55             return createCloseableProxy(new CloseableEventExecutorMixin(ImmediateEventExecutor.INSTANCE));
56         }
57
58         public static AutoCloseableEventExecutor forwardingEventExecutor(final EventExecutor eventExecutor,
59                 final AutoCloseable closeable) {
60             return createCloseableProxy(new CloseableEventExecutorMixin(eventExecutor) {
61                 @Override
62                 public void close() throws Exception {
63                     // Intentional no-op.
64                     closeable.close();
65                 }
66             });
67         }
68     }
69 }