Move validatePromise(...) to ChannelHandlerInvokerUtil. Related to [#2398]
Motivation: Once a user implement a custom ChannelHandlerInvoker it is needed to validate the ChannelPromise. We should expose a utility method for this. Modifications: Move validatePromise(...) from DefaultChannelHandlerInvoker to ChannelHandlerInvokerUtil and make it public. Result: User is able to reuse code
This commit is contained in:
parent
18e2a45d7c
commit
9e02a9bbee
@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package io.netty.channel;
|
package io.netty.channel;
|
||||||
|
|
||||||
|
import io.netty.util.internal.StringUtil;
|
||||||
|
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
|
|
||||||
import static io.netty.channel.DefaultChannelPipeline.*;
|
import static io.netty.channel.DefaultChannelPipeline.*;
|
||||||
@ -174,6 +176,44 @@ public final class ChannelHandlerInvokerUtil {
|
|||||||
invokeFlushNow(ctx);
|
invokeFlushNow(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean validatePromise(
|
||||||
|
ChannelHandlerContext ctx, ChannelPromise promise, boolean allowVoidPromise) {
|
||||||
|
if (ctx == null) {
|
||||||
|
throw new NullPointerException("ctx");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (promise == null) {
|
||||||
|
throw new NullPointerException("promise");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (promise.isDone()) {
|
||||||
|
if (promise.isCancelled()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException("promise already done: " + promise);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (promise.channel() != ctx.channel()) {
|
||||||
|
throw new IllegalArgumentException(String.format(
|
||||||
|
"promise.channel does not match: %s (expected: %s)", promise.channel(), ctx.channel()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (promise.getClass() == DefaultChannelPromise.class) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!allowVoidPromise && promise instanceof VoidChannelPromise) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
StringUtil.simpleClassName(VoidChannelPromise.class) + " not allowed for this operation");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (promise instanceof AbstractChannel.CloseFuture) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
StringUtil.simpleClassName(AbstractChannel.CloseFuture.class) + " not allowed in a pipeline");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private static void notifyHandlerException(ChannelHandlerContext ctx, Throwable cause) {
|
private static void notifyHandlerException(ChannelHandlerContext ctx, Throwable cause) {
|
||||||
if (inExceptionCaught(cause)) {
|
if (inExceptionCaught(cause)) {
|
||||||
if (logger.isWarnEnabled()) {
|
if (logger.isWarnEnabled()) {
|
||||||
|
@ -20,7 +20,6 @@ import io.netty.util.Recycler;
|
|||||||
import io.netty.util.ReferenceCountUtil;
|
import io.netty.util.ReferenceCountUtil;
|
||||||
import io.netty.util.concurrent.EventExecutor;
|
import io.netty.util.concurrent.EventExecutor;
|
||||||
import io.netty.util.internal.OneTimeTask;
|
import io.netty.util.internal.OneTimeTask;
|
||||||
import io.netty.util.internal.StringUtil;
|
|
||||||
|
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
|
|
||||||
@ -397,44 +396,6 @@ public class DefaultChannelHandlerInvoker implements ChannelHandlerInvoker {
|
|||||||
invokeWrite(ctx, msg, true, promise);
|
invokeWrite(ctx, msg, true, promise);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean validatePromise(
|
|
||||||
ChannelHandlerContext ctx, ChannelPromise promise, boolean allowVoidPromise) {
|
|
||||||
if (ctx == null) {
|
|
||||||
throw new NullPointerException("ctx");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (promise == null) {
|
|
||||||
throw new NullPointerException("promise");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (promise.isDone()) {
|
|
||||||
if (promise.isCancelled()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
throw new IllegalArgumentException("promise already done: " + promise);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (promise.channel() != ctx.channel()) {
|
|
||||||
throw new IllegalArgumentException(String.format(
|
|
||||||
"promise.channel does not match: %s (expected: %s)", promise.channel(), ctx.channel()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (promise.getClass() == DefaultChannelPromise.class) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!allowVoidPromise && promise instanceof VoidChannelPromise) {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
StringUtil.simpleClassName(VoidChannelPromise.class) + " not allowed for this operation");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (promise instanceof AbstractChannel.CloseFuture) {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
StringUtil.simpleClassName(AbstractChannel.CloseFuture.class) + " not allowed in a pipeline");
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void safeExecuteInbound(Runnable task, Object msg) {
|
private void safeExecuteInbound(Runnable task, Object msg) {
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user