From 39ec19078c74974ea3775f9a041da64337d58f33 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 4 Mar 2009 10:35:55 +0000 Subject: [PATCH] Code clean up --- .../netty/channel/DefaultChannelPipeline.java | 237 +++++++++--------- 1 file changed, 121 insertions(+), 116 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/DefaultChannelPipeline.java b/src/main/java/org/jboss/netty/channel/DefaultChannelPipeline.java index 66a9163634..3b447aad2a 100644 --- a/src/main/java/org/jboss/netty/channel/DefaultChannelPipeline.java +++ b/src/main/java/org/jboss/netty/channel/DefaultChannelPipeline.java @@ -47,18 +47,8 @@ import org.jboss.netty.logging.InternalLoggerFactory; public class DefaultChannelPipeline implements ChannelPipeline { static final InternalLogger logger = InternalLoggerFactory.getInstance(DefaultChannelPipeline.class); - - private final ChannelSink discardingSink = new ChannelSink() { - public void eventSunk(ChannelPipeline pipeline, ChannelEvent e) { - logger.warn("Not attached yet; discarding: " + e); - } - - public void exceptionCaught(ChannelPipeline pipeline, - ChannelEvent e, ChannelPipelineException cause) throws Exception { - throw cause; - } - }; - + private static final ChannelSink discardingSink = new DiscardingChannelSink(); + private volatile Channel channel; private volatile ChannelSink sink; private volatile DefaultChannelHandlerContext head; @@ -697,112 +687,127 @@ public class DefaultChannelPipeline implements ChannelPipeline { } } - private final class DefaultChannelHandlerContext implements ChannelHandlerContext { - volatile DefaultChannelHandlerContext next; - volatile DefaultChannelHandlerContext prev; - private final String name; - private final ChannelHandler handler; - private final boolean canHandleUpstream; - private final boolean canHandleDownstream; + private final class DefaultChannelHandlerContext implements ChannelHandlerContext { + volatile DefaultChannelHandlerContext next; + volatile DefaultChannelHandlerContext prev; + private final String name; + private final ChannelHandler handler; + private final boolean canHandleUpstream; + private final boolean canHandleDownstream; + + DefaultChannelHandlerContext( + DefaultChannelHandlerContext prev, DefaultChannelHandlerContext next, + String name, ChannelHandler handler) { + + if (name == null) { + throw new NullPointerException("name"); + } + if (handler == null) { + throw new NullPointerException("handler"); + } + canHandleUpstream = handler instanceof ChannelUpstreamHandler; + canHandleDownstream = handler instanceof ChannelDownstreamHandler; + + + if (!canHandleUpstream && !canHandleDownstream) { + throw new IllegalArgumentException( + "handler must be either " + + ChannelUpstreamHandler.class.getName() + " or " + + ChannelDownstreamHandler.class.getName() + '.'); + } + + + ChannelPipelineCoverage coverage = handler.getClass().getAnnotation(ChannelPipelineCoverage.class); + if (coverage == null) { + logger.warn( + "Handler '" + handler.getClass().getName() + + "' does not have a '" + + ChannelPipelineCoverage.class.getSimpleName() + + "' annotation with its class declaration. " + + "It is recommended to add the annotation to tell if " + + "one handler instance can handle more than one pipeline " + + "(\"" + ALL + "\") or not (\"" + ONE + "\")"); + } else { + String coverageValue = coverage.value(); + if (coverageValue == null) { + throw new AnnotationFormatError( + ChannelPipelineCoverage.class.getSimpleName() + + " annotation value is undefined for type: " + + handler.getClass().getName()); + } + + + if (!coverageValue.equals(ALL) && !coverageValue.equals(ONE)) { + throw new AnnotationFormatError( + ChannelPipelineCoverage.class.getSimpleName() + + " annotation value: " + coverageValue + + " (must be either \"" + ALL + "\" or \"" + ONE + ")"); + } + } + + this.prev = prev; + this.next = next; + this.name = name; + this.handler = handler; + } + + public Channel getChannel() { + return getPipeline().getChannel(); + } + + public ChannelPipeline getPipeline() { + return DefaultChannelPipeline.this; + } + + public boolean canHandleDownstream() { + return canHandleDownstream; + } + + public boolean canHandleUpstream() { + return canHandleUpstream; + } + + public ChannelHandler getHandler() { + return handler; + } + + public String getName() { + return name; + } + + public void sendDownstream(ChannelEvent e) { + DefaultChannelHandlerContext prev = getActualDownstreamContext(this.prev); + if (prev == null) { + try { + getSink().eventSunk(DefaultChannelPipeline.this, e); + } catch (Throwable t) { + notifyHandlerException(e, t); + } + } else { + DefaultChannelPipeline.this.sendDownstream(prev, e); + } + } + + public void sendUpstream(ChannelEvent e) { + DefaultChannelHandlerContext next = getActualUpstreamContext(this.next); + if (next != null) { + DefaultChannelPipeline.this.sendUpstream(next, e); + } + } + } - DefaultChannelHandlerContext( - DefaultChannelHandlerContext prev, DefaultChannelHandlerContext next, - String name, ChannelHandler handler) { - - if (name == null) { - throw new NullPointerException("name"); - } - if (handler == null) { - throw new NullPointerException("handler"); - } - canHandleUpstream = handler instanceof ChannelUpstreamHandler; - canHandleDownstream = handler instanceof ChannelDownstreamHandler; - - - if (!canHandleUpstream && !canHandleDownstream) { - throw new IllegalArgumentException( - "handler must be either " + - ChannelUpstreamHandler.class.getName() + " or " + - ChannelDownstreamHandler.class.getName() + '.'); - } - - - ChannelPipelineCoverage coverage = handler.getClass().getAnnotation(ChannelPipelineCoverage.class); - if (coverage == null) { - logger.warn( - "Handler '" + handler.getClass().getName() + - "' does not have a '" + - ChannelPipelineCoverage.class.getSimpleName() + - "' annotation with its class declaration. " + - "It is recommended to add the annotation to tell if " + - "one handler instance can handle more than one pipeline " + - "(\"" + ALL + "\") or not (\"" + ONE + "\")"); - } else { - String coverageValue = coverage.value(); - if (coverageValue == null) { - throw new AnnotationFormatError( - ChannelPipelineCoverage.class.getSimpleName() + - " annotation value is undefined for type: " + - handler.getClass().getName()); - } - - - if (!coverageValue.equals(ALL) && !coverageValue.equals(ONE)) { - throw new AnnotationFormatError( - ChannelPipelineCoverage.class.getSimpleName() + - " annotation value: " + coverageValue + - " (must be either \"" + ALL + "\" or \"" + ONE + ")"); - } - } - - this.prev = prev; - this.next = next; - this.name = name; - this.handler = handler; + private static final class DiscardingChannelSink implements ChannelSink { + DiscardingChannelSink() { + super(); + } + + public void eventSunk(ChannelPipeline pipeline, ChannelEvent e) { + logger.warn("Not attached yet; discarding: " + e); } - public Channel getChannel() { - return getPipeline().getChannel(); + public void exceptionCaught(ChannelPipeline pipeline, + ChannelEvent e, ChannelPipelineException cause) throws Exception { + throw cause; } - - public ChannelPipeline getPipeline() { - return DefaultChannelPipeline.this; - } - - public boolean canHandleDownstream() { - return canHandleDownstream; - } - - public boolean canHandleUpstream() { - return canHandleUpstream; - } - - public ChannelHandler getHandler() { - return handler; - } - - public String getName() { - return name; - } - - public void sendDownstream(ChannelEvent e) { - DefaultChannelHandlerContext prev = getActualDownstreamContext(this.prev); - if (prev == null) { - try { - getSink().eventSunk(DefaultChannelPipeline.this, e); - } catch (Throwable t) { - notifyHandlerException(e, t); - } - } else { - DefaultChannelPipeline.this.sendDownstream(prev, e); - } - } - - public void sendUpstream(ChannelEvent e) { - DefaultChannelHandlerContext next = getActualUpstreamContext(this.next); - if (next != null) { - DefaultChannelPipeline.this.sendUpstream(next, e); - } - } - } + } } \ No newline at end of file