View Javadoc
1   /*
2    * Copyright 2017 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel;
17  
18  import io.netty.util.concurrent.Future;
19  import io.netty.util.concurrent.GenericFutureListener;
20  import io.netty.util.internal.PromiseNotificationUtil;
21  import io.netty.util.internal.UnstableApi;
22  import io.netty.util.internal.logging.InternalLogger;
23  import io.netty.util.internal.logging.InternalLoggerFactory;
24  
25  import java.util.concurrent.ExecutionException;
26  import java.util.concurrent.TimeUnit;
27  import java.util.concurrent.TimeoutException;
28  
29  import static io.netty.util.internal.ObjectUtil.checkNotNull;
30  
31  @UnstableApi
32  public final class DelegatingChannelPromiseNotifier implements ChannelPromise, ChannelFutureListener {
33      private static final InternalLogger logger =
34              InternalLoggerFactory.getInstance(DelegatingChannelPromiseNotifier.class);
35      private final ChannelPromise delegate;
36      private final boolean logNotifyFailure;
37  
38      public DelegatingChannelPromiseNotifier(ChannelPromise delegate) {
39          this(delegate, !(delegate instanceof VoidChannelPromise));
40      }
41  
42      public DelegatingChannelPromiseNotifier(ChannelPromise delegate, boolean logNotifyFailure) {
43          this.delegate = checkNotNull(delegate, "delegate");
44          this.logNotifyFailure = logNotifyFailure;
45      }
46  
47      @Override
48      public void operationComplete(ChannelFuture future) throws Exception {
49          InternalLogger internalLogger = logNotifyFailure ? logger : null;
50          if (future.isSuccess()) {
51              Void result = future.get();
52              PromiseNotificationUtil.trySuccess(delegate, result, internalLogger);
53          } else if (future.isCancelled()) {
54              PromiseNotificationUtil.tryCancel(delegate, internalLogger);
55          } else {
56              Throwable cause = future.cause();
57              PromiseNotificationUtil.tryFailure(delegate, cause, internalLogger);
58          }
59      }
60  
61      @Override
62      public Channel channel() {
63          return delegate.channel();
64      }
65  
66      @Override
67      public ChannelPromise setSuccess(Void result) {
68          delegate.setSuccess(result);
69          return this;
70      }
71  
72      @Override
73      public ChannelPromise setSuccess() {
74          delegate.setSuccess();
75          return this;
76      }
77  
78      @Override
79      public boolean trySuccess() {
80          return delegate.trySuccess();
81      }
82  
83      @Override
84      public boolean trySuccess(Void result) {
85          return delegate.trySuccess(result);
86      }
87  
88      @Override
89      public ChannelPromise setFailure(Throwable cause) {
90          delegate.setFailure(cause);
91          return this;
92      }
93  
94      @Override
95      public ChannelPromise addListener(GenericFutureListener<? extends Future<? super Void>> listener) {
96          delegate.addListener(listener);
97          return this;
98      }
99  
100     @Override
101     public ChannelPromise addListeners(GenericFutureListener<? extends Future<? super Void>>... listeners) {
102         delegate.addListeners(listeners);
103         return this;
104     }
105 
106     @Override
107     public ChannelPromise removeListener(GenericFutureListener<? extends Future<? super Void>> listener) {
108         delegate.removeListener(listener);
109         return this;
110     }
111 
112     @Override
113     public ChannelPromise removeListeners(GenericFutureListener<? extends Future<? super Void>>... listeners) {
114         delegate.removeListeners(listeners);
115         return this;
116     }
117 
118     @Override
119     public boolean tryFailure(Throwable cause) {
120         return delegate.tryFailure(cause);
121     }
122 
123     @Override
124     public boolean setUncancellable() {
125         return delegate.setUncancellable();
126     }
127 
128     @Override
129     public ChannelPromise await() throws InterruptedException {
130         delegate.await();
131         return this;
132     }
133 
134     @Override
135     public ChannelPromise awaitUninterruptibly() {
136         delegate.awaitUninterruptibly();
137         return this;
138     }
139 
140     @Override
141     public boolean isVoid() {
142         return delegate.isVoid();
143     }
144 
145     @Override
146     public ChannelPromise unvoid() {
147         return isVoid() ? new DelegatingChannelPromiseNotifier(delegate.unvoid()) : this;
148     }
149 
150     @Override
151     public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
152         return delegate.await(timeout, unit);
153     }
154 
155     @Override
156     public boolean await(long timeoutMillis) throws InterruptedException {
157         return delegate.await(timeoutMillis);
158     }
159 
160     @Override
161     public boolean awaitUninterruptibly(long timeout, TimeUnit unit) {
162         return delegate.awaitUninterruptibly(timeout, unit);
163     }
164 
165     @Override
166     public boolean awaitUninterruptibly(long timeoutMillis) {
167         return delegate.awaitUninterruptibly(timeoutMillis);
168     }
169 
170     @Override
171     public Void getNow() {
172         return delegate.getNow();
173     }
174 
175     @Override
176     public boolean cancel(boolean mayInterruptIfRunning) {
177         return delegate.cancel(mayInterruptIfRunning);
178     }
179 
180     @Override
181     public boolean isCancelled() {
182         return delegate.isCancelled();
183     }
184 
185     @Override
186     public boolean isDone() {
187         return delegate.isDone();
188     }
189 
190     @Override
191     public Void get() throws InterruptedException, ExecutionException {
192         return delegate.get();
193     }
194 
195     @Override
196     public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
197         return delegate.get(timeout, unit);
198     }
199 
200     @Override
201     public ChannelPromise sync() throws InterruptedException {
202         delegate.sync();
203         return this;
204     }
205 
206     @Override
207     public ChannelPromise syncUninterruptibly() {
208         delegate.syncUninterruptibly();
209         return this;
210     }
211 
212     @Override
213     public boolean isSuccess() {
214         return delegate.isSuccess();
215     }
216 
217     @Override
218     public boolean isCancellable() {
219         return delegate.isCancellable();
220     }
221 
222     @Override
223     public Throwable cause() {
224         return delegate.cause();
225     }
226 }