2008-08-08 00:37:18 +00:00
|
|
|
/*
|
2009-08-28 07:15:49 +00:00
|
|
|
* Copyright 2009 Red Hat, Inc.
|
2008-08-08 00:37:18 +00:00
|
|
|
*
|
2009-08-28 07:15:49 +00:00
|
|
|
* Red Hat licenses this file to you under the Apache License, version 2.0
|
|
|
|
* (the "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at:
|
2008-08-08 00:37:18 +00:00
|
|
|
*
|
2009-08-28 07:15:49 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-08-08 01:27:24 +00:00
|
|
|
*
|
2009-08-28 07:15:49 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-08-08 00:37:18 +00:00
|
|
|
*/
|
2008-08-08 01:40:10 +00:00
|
|
|
package org.jboss.netty.channel;
|
2008-08-08 00:37:18 +00:00
|
|
|
|
2011-08-02 08:43:10 +09:00
|
|
|
import java.util.ArrayList;
|
2008-08-08 00:37:18 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.LinkedHashMap;
|
2011-08-02 08:43:10 +09:00
|
|
|
import java.util.List;
|
2008-08-08 00:37:18 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.NoSuchElementException;
|
|
|
|
|
2008-08-09 14:52:19 +00:00
|
|
|
import org.jboss.netty.logging.InternalLogger;
|
2008-08-09 15:05:53 +00:00
|
|
|
import org.jboss.netty.logging.InternalLoggerFactory;
|
2008-08-08 00:37:18 +00:00
|
|
|
|
2008-09-02 07:13:20 +00:00
|
|
|
/**
|
|
|
|
* The default {@link ChannelPipeline} implementation. It is recommended
|
|
|
|
* to use {@link Channels#pipeline()} to create a new {@link ChannelPipeline}
|
|
|
|
* instance rather than calling the constructor directly.
|
|
|
|
*
|
2010-01-26 09:04:19 +00:00
|
|
|
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
|
|
|
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
2008-09-02 07:13:20 +00:00
|
|
|
*
|
|
|
|
* @version $Rev$, $Date$
|
|
|
|
*
|
|
|
|
*/
|
2008-08-08 00:37:18 +00:00
|
|
|
public class DefaultChannelPipeline implements ChannelPipeline {
|
|
|
|
|
2008-08-09 15:05:53 +00:00
|
|
|
static final InternalLogger logger = InternalLoggerFactory.getInstance(DefaultChannelPipeline.class);
|
2009-12-17 10:57:57 +00:00
|
|
|
static final ChannelSink discardingSink = new DiscardingChannelSink();
|
2009-03-11 10:45:55 +00:00
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
private volatile Channel channel;
|
|
|
|
private volatile ChannelSink sink;
|
|
|
|
private volatile DefaultChannelHandlerContext head;
|
|
|
|
private volatile DefaultChannelHandlerContext tail;
|
|
|
|
private final Map<String, DefaultChannelHandlerContext> name2ctx =
|
|
|
|
new HashMap<String, DefaultChannelHandlerContext>(4);
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public Channel getChannel() {
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public ChannelSink getSink() {
|
|
|
|
ChannelSink sink = this.sink;
|
|
|
|
if (sink == null) {
|
|
|
|
return discardingSink;
|
|
|
|
}
|
|
|
|
return sink;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public void attach(Channel channel, ChannelSink sink) {
|
|
|
|
if (channel == null) {
|
|
|
|
throw new NullPointerException("channel");
|
|
|
|
}
|
|
|
|
if (sink == null) {
|
|
|
|
throw new NullPointerException("sink");
|
|
|
|
}
|
|
|
|
if (this.channel != null || this.sink != null) {
|
|
|
|
throw new IllegalStateException("attached already");
|
|
|
|
}
|
|
|
|
this.channel = channel;
|
|
|
|
this.sink = sink;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-12-03 07:14:22 +00:00
|
|
|
public boolean isAttached() {
|
2008-12-03 07:20:46 +00:00
|
|
|
return sink != null;
|
2008-12-03 07:14:22 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized void addFirst(String name, ChannelHandler handler) {
|
|
|
|
if (name2ctx.isEmpty()) {
|
|
|
|
init(name, handler);
|
|
|
|
} else {
|
|
|
|
checkDuplicateName(name);
|
|
|
|
DefaultChannelHandlerContext oldHead = head;
|
|
|
|
DefaultChannelHandlerContext newHead = new DefaultChannelHandlerContext(null, oldHead, name, handler);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeAdd(newHead);
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
oldHead.prev = newHead;
|
|
|
|
head = newHead;
|
|
|
|
name2ctx.put(name, newHead);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callAfterAdd(newHead);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized void addLast(String name, ChannelHandler handler) {
|
|
|
|
if (name2ctx.isEmpty()) {
|
|
|
|
init(name, handler);
|
|
|
|
} else {
|
|
|
|
checkDuplicateName(name);
|
|
|
|
DefaultChannelHandlerContext oldTail = tail;
|
|
|
|
DefaultChannelHandlerContext newTail = new DefaultChannelHandlerContext(oldTail, null, name, handler);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeAdd(newTail);
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
oldTail.next = newTail;
|
|
|
|
tail = newTail;
|
|
|
|
name2ctx.put(name, newTail);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callAfterAdd(newTail);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized void addBefore(String baseName, String name, ChannelHandler handler) {
|
|
|
|
DefaultChannelHandlerContext ctx = getContextOrDie(baseName);
|
|
|
|
if (ctx == head) {
|
|
|
|
addFirst(name, handler);
|
|
|
|
} else {
|
|
|
|
checkDuplicateName(name);
|
|
|
|
DefaultChannelHandlerContext newCtx = new DefaultChannelHandlerContext(ctx.prev, ctx, name, handler);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeAdd(newCtx);
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
ctx.prev.next = newCtx;
|
|
|
|
ctx.prev = newCtx;
|
|
|
|
name2ctx.put(name, newCtx);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callAfterAdd(newCtx);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized void addAfter(String baseName, String name, ChannelHandler handler) {
|
|
|
|
DefaultChannelHandlerContext ctx = getContextOrDie(baseName);
|
|
|
|
if (ctx == tail) {
|
|
|
|
addLast(name, handler);
|
|
|
|
} else {
|
|
|
|
checkDuplicateName(name);
|
|
|
|
DefaultChannelHandlerContext newCtx = new DefaultChannelHandlerContext(ctx, ctx.next, name, handler);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeAdd(newCtx);
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
ctx.next.prev = newCtx;
|
|
|
|
ctx.next = newCtx;
|
|
|
|
name2ctx.put(name, newCtx);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callAfterAdd(newCtx);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized void remove(ChannelHandler handler) {
|
|
|
|
remove(getContextOrDie(handler));
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandler remove(String name) {
|
|
|
|
return remove(getContextOrDie(name)).getHandler();
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public synchronized <T extends ChannelHandler> T remove(Class<T> handlerType) {
|
|
|
|
return (T) remove(getContextOrDie(handlerType)).getHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
private DefaultChannelHandlerContext remove(DefaultChannelHandlerContext ctx) {
|
|
|
|
if (head == tail) {
|
|
|
|
head = tail = null;
|
|
|
|
name2ctx.clear();
|
|
|
|
} else if (ctx == head) {
|
|
|
|
removeFirst();
|
|
|
|
} else if (ctx == tail) {
|
|
|
|
removeLast();
|
|
|
|
} else {
|
2008-12-01 10:07:54 +00:00
|
|
|
callBeforeRemove(ctx);
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
DefaultChannelHandlerContext prev = ctx.prev;
|
|
|
|
DefaultChannelHandlerContext next = ctx.next;
|
|
|
|
prev.next = next;
|
|
|
|
next.prev = prev;
|
|
|
|
name2ctx.remove(ctx.getName());
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callAfterRemove(ctx);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandler removeFirst() {
|
|
|
|
if (name2ctx.isEmpty()) {
|
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext oldHead = head;
|
2008-08-18 02:38:54 +00:00
|
|
|
if (oldHead == null) {
|
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeRemove(oldHead);
|
|
|
|
|
2008-08-18 02:38:54 +00:00
|
|
|
if (oldHead.next == null) {
|
|
|
|
head = tail = null;
|
|
|
|
name2ctx.clear();
|
|
|
|
} else {
|
|
|
|
oldHead.next.prev = null;
|
|
|
|
head = oldHead.next;
|
|
|
|
name2ctx.remove(oldHead.getName());
|
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callAfterRemove(oldHead);
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
return oldHead.getHandler();
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandler removeLast() {
|
|
|
|
if (name2ctx.isEmpty()) {
|
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext oldTail = tail;
|
2008-08-18 02:38:54 +00:00
|
|
|
if (oldTail == null) {
|
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeRemove(oldTail);
|
|
|
|
|
2008-08-18 02:38:54 +00:00
|
|
|
if (oldTail.prev == null) {
|
|
|
|
head = tail = null;
|
|
|
|
name2ctx.clear();
|
|
|
|
} else {
|
|
|
|
oldTail.prev.next = null;
|
|
|
|
tail = oldTail.prev;
|
|
|
|
name2ctx.remove(oldTail.getName());
|
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeRemove(oldTail);
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
return oldTail.getHandler();
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized void replace(ChannelHandler oldHandler, String newName, ChannelHandler newHandler) {
|
|
|
|
replace(getContextOrDie(oldHandler), newName, newHandler);
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandler replace(String oldName, String newName, ChannelHandler newHandler) {
|
|
|
|
return replace(getContextOrDie(oldName), newName, newHandler);
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public synchronized <T extends ChannelHandler> T replace(
|
|
|
|
Class<T> oldHandlerType, String newName, ChannelHandler newHandler) {
|
|
|
|
return (T) replace(getContextOrDie(oldHandlerType), newName, newHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ChannelHandler replace(DefaultChannelHandlerContext ctx, String newName, ChannelHandler newHandler) {
|
|
|
|
if (ctx == head) {
|
|
|
|
removeFirst();
|
|
|
|
addFirst(newName, newHandler);
|
|
|
|
} else if (ctx == tail) {
|
|
|
|
removeLast();
|
|
|
|
addLast(newName, newHandler);
|
|
|
|
} else {
|
|
|
|
boolean sameName = ctx.getName().equals(newName);
|
|
|
|
if (!sameName) {
|
|
|
|
checkDuplicateName(newName);
|
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
DefaultChannelHandlerContext prev = ctx.prev;
|
|
|
|
DefaultChannelHandlerContext next = ctx.next;
|
|
|
|
DefaultChannelHandlerContext newCtx = new DefaultChannelHandlerContext(prev, next, newName, newHandler);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
callBeforeRemove(ctx);
|
|
|
|
callBeforeAdd(newCtx);
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
prev.next = newCtx;
|
|
|
|
next.prev = newCtx;
|
2008-12-01 10:07:54 +00:00
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
if (!sameName) {
|
|
|
|
name2ctx.remove(ctx.getName());
|
|
|
|
name2ctx.put(newName, newCtx);
|
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
|
|
|
|
ChannelHandlerLifeCycleException removeException = null;
|
|
|
|
ChannelHandlerLifeCycleException addException = null;
|
|
|
|
boolean removed = false;
|
|
|
|
try {
|
|
|
|
callAfterRemove(ctx);
|
|
|
|
removed = true;
|
|
|
|
} catch (ChannelHandlerLifeCycleException e) {
|
|
|
|
removeException = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean added = false;
|
|
|
|
try {
|
|
|
|
callAfterAdd(newCtx);
|
|
|
|
added = true;
|
|
|
|
} catch (ChannelHandlerLifeCycleException e) {
|
|
|
|
addException = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!removed && !added) {
|
|
|
|
logger.warn(removeException.getMessage(), removeException);
|
|
|
|
logger.warn(addException.getMessage(), addException);
|
|
|
|
throw new ChannelHandlerLifeCycleException(
|
|
|
|
"Both " + ctx.getHandler().getClass().getName() +
|
|
|
|
".afterRemove() and " + newCtx.getHandler().getClass().getName() +
|
|
|
|
".afterAdd() failed; see logs.");
|
|
|
|
} else if (!removed) {
|
|
|
|
throw removeException;
|
|
|
|
} else if (!added) {
|
|
|
|
throw addException;
|
|
|
|
}
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
return ctx.getHandler();
|
|
|
|
}
|
|
|
|
|
2008-12-01 10:07:54 +00:00
|
|
|
private void callBeforeAdd(ChannelHandlerContext ctx) {
|
|
|
|
if (!(ctx.getHandler() instanceof LifeCycleAwareChannelHandler)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LifeCycleAwareChannelHandler h =
|
|
|
|
(LifeCycleAwareChannelHandler) ctx.getHandler();
|
|
|
|
|
|
|
|
try {
|
|
|
|
h.beforeAdd(ctx);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
throw new ChannelHandlerLifeCycleException(
|
2008-12-09 07:17:37 +00:00
|
|
|
h.getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".beforeAdd() has thrown an exception; not adding.", t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void callAfterAdd(ChannelHandlerContext ctx) {
|
|
|
|
if (!(ctx.getHandler() instanceof LifeCycleAwareChannelHandler)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LifeCycleAwareChannelHandler h =
|
|
|
|
(LifeCycleAwareChannelHandler) ctx.getHandler();
|
|
|
|
|
|
|
|
try {
|
|
|
|
h.afterAdd(ctx);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
boolean removed = false;
|
|
|
|
try {
|
|
|
|
remove((DefaultChannelHandlerContext) ctx);
|
|
|
|
removed = true;
|
|
|
|
} catch (Throwable t2) {
|
|
|
|
logger.warn("Failed to remove a handler: " + ctx.getName(), t2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (removed) {
|
|
|
|
throw new ChannelHandlerLifeCycleException(
|
2008-12-09 07:17:37 +00:00
|
|
|
h.getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".afterAdd() has thrown an exception; removed.", t);
|
|
|
|
} else {
|
|
|
|
throw new ChannelHandlerLifeCycleException(
|
2008-12-09 07:17:37 +00:00
|
|
|
h.getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".afterAdd() has thrown an exception; also failed to remove.", t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void callBeforeRemove(ChannelHandlerContext ctx) {
|
|
|
|
if (!(ctx.getHandler() instanceof LifeCycleAwareChannelHandler)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LifeCycleAwareChannelHandler h =
|
|
|
|
(LifeCycleAwareChannelHandler) ctx.getHandler();
|
|
|
|
|
|
|
|
try {
|
|
|
|
h.beforeRemove(ctx);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
throw new ChannelHandlerLifeCycleException(
|
2008-12-09 07:17:37 +00:00
|
|
|
h.getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".beforeRemove() has thrown an exception; not removing.", t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void callAfterRemove(ChannelHandlerContext ctx) {
|
|
|
|
if (!(ctx.getHandler() instanceof LifeCycleAwareChannelHandler)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LifeCycleAwareChannelHandler h =
|
|
|
|
(LifeCycleAwareChannelHandler) ctx.getHandler();
|
|
|
|
|
|
|
|
try {
|
|
|
|
h.afterRemove(ctx);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
throw new ChannelHandlerLifeCycleException(
|
2008-12-09 07:17:37 +00:00
|
|
|
h.getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".afterRemove() has thrown an exception.", t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandler getFirst() {
|
2008-08-18 02:27:11 +00:00
|
|
|
DefaultChannelHandlerContext head = this.head;
|
|
|
|
if (head == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2008-08-08 00:37:18 +00:00
|
|
|
return head.getHandler();
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandler getLast() {
|
2008-08-18 02:27:11 +00:00
|
|
|
DefaultChannelHandlerContext tail = this.tail;
|
|
|
|
if (tail == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2008-08-08 00:37:18 +00:00
|
|
|
return tail.getHandler();
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandler get(String name) {
|
|
|
|
DefaultChannelHandlerContext ctx = name2ctx.get(name);
|
|
|
|
if (ctx == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return ctx.getHandler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public synchronized <T extends ChannelHandler> T get(Class<T> handlerType) {
|
|
|
|
ChannelHandlerContext ctx = getContext(handlerType);
|
|
|
|
if (ctx == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return (T) ctx.getHandler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandlerContext getContext(String name) {
|
|
|
|
if (name == null) {
|
|
|
|
throw new NullPointerException("name");
|
|
|
|
}
|
|
|
|
return name2ctx.get(name);
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandlerContext getContext(ChannelHandler handler) {
|
|
|
|
if (handler == null) {
|
|
|
|
throw new NullPointerException("handler");
|
|
|
|
}
|
|
|
|
if (name2ctx.isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
DefaultChannelHandlerContext ctx = head;
|
|
|
|
for (;;) {
|
|
|
|
if (ctx.getHandler() == handler) {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = ctx.next;
|
|
|
|
if (ctx == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public synchronized ChannelHandlerContext getContext(
|
|
|
|
Class<? extends ChannelHandler> handlerType) {
|
2009-12-17 10:57:57 +00:00
|
|
|
if (handlerType == null) {
|
|
|
|
throw new NullPointerException("handlerType");
|
|
|
|
}
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
if (name2ctx.isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
DefaultChannelHandlerContext ctx = head;
|
|
|
|
for (;;) {
|
|
|
|
if (handlerType.isAssignableFrom(ctx.getHandler().getClass())) {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = ctx.next;
|
|
|
|
if (ctx == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-08-02 08:43:10 +09:00
|
|
|
@Override
|
|
|
|
public List<String> getNames() {
|
|
|
|
List<String> list = new ArrayList<String>();
|
|
|
|
if (name2ctx.isEmpty()) {
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext ctx = head;
|
|
|
|
for (;;) {
|
|
|
|
list.add(ctx.getName());
|
|
|
|
ctx = ctx.next;
|
|
|
|
if (ctx == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public Map<String, ChannelHandler> toMap() {
|
|
|
|
Map<String, ChannelHandler> map = new LinkedHashMap<String, ChannelHandler>();
|
|
|
|
if (name2ctx.isEmpty()) {
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext ctx = head;
|
|
|
|
for (;;) {
|
|
|
|
map.put(ctx.getName(), ctx.getHandler());
|
|
|
|
ctx = ctx.next;
|
|
|
|
if (ctx == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2008-09-03 04:09:46 +00:00
|
|
|
/**
|
|
|
|
* Returns the {@link String} representation of this pipeline.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
StringBuilder buf = new StringBuilder();
|
|
|
|
buf.append(getClass().getSimpleName());
|
|
|
|
buf.append('{');
|
|
|
|
DefaultChannelHandlerContext ctx = head;
|
|
|
|
for (;;) {
|
|
|
|
buf.append('(');
|
|
|
|
buf.append(ctx.getName());
|
|
|
|
buf.append(" = ");
|
|
|
|
buf.append(ctx.getHandler().getClass().getName());
|
|
|
|
buf.append(')');
|
|
|
|
ctx = ctx.next;
|
|
|
|
if (ctx == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf.append(", ");
|
|
|
|
}
|
|
|
|
buf.append('}');
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public void sendUpstream(ChannelEvent e) {
|
|
|
|
DefaultChannelHandlerContext head = getActualUpstreamContext(this.head);
|
|
|
|
if (head == null) {
|
|
|
|
logger.warn(
|
|
|
|
"The pipeline contains no upstream handlers; discarding: " + e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendUpstream(head, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sendUpstream(DefaultChannelHandlerContext ctx, ChannelEvent e) {
|
|
|
|
try {
|
|
|
|
((ChannelUpstreamHandler) ctx.getHandler()).handleUpstream(ctx, e);
|
|
|
|
} catch (Throwable t) {
|
2008-08-18 11:11:55 +00:00
|
|
|
notifyHandlerException(e, t);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public void sendDownstream(ChannelEvent e) {
|
|
|
|
DefaultChannelHandlerContext tail = getActualDownstreamContext(this.tail);
|
|
|
|
if (tail == null) {
|
|
|
|
try {
|
|
|
|
getSink().eventSunk(this, e);
|
|
|
|
return;
|
|
|
|
} catch (Throwable t) {
|
2008-08-18 11:11:55 +00:00
|
|
|
notifyHandlerException(e, t);
|
2008-11-07 02:26:21 +00:00
|
|
|
return;
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendDownstream(tail, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sendDownstream(DefaultChannelHandlerContext ctx, ChannelEvent e) {
|
2011-08-01 04:16:02 +09:00
|
|
|
if (e instanceof UpstreamMessageEvent) {
|
|
|
|
throw new IllegalArgumentException("cannot send an upstream event to downstream");
|
|
|
|
}
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
try {
|
|
|
|
((ChannelDownstreamHandler) ctx.getHandler()).handleDownstream(ctx, e);
|
|
|
|
} catch (Throwable t) {
|
2011-08-02 06:35:38 +09:00
|
|
|
// Unlike an upstream event, a downstream event usually has an
|
|
|
|
// incomplete future which is supposed to be updated by ChannelSink.
|
|
|
|
// However, if an exception is raised before the event reaches at
|
|
|
|
// ChannelSink, the future is not going to be updated, so we update
|
|
|
|
// here.
|
|
|
|
e.getFuture().setFailure(t);
|
2008-08-18 11:11:55 +00:00
|
|
|
notifyHandlerException(e, t);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext getActualUpstreamContext(DefaultChannelHandlerContext ctx) {
|
|
|
|
if (ctx == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext realCtx = ctx;
|
|
|
|
while (!realCtx.canHandleUpstream()) {
|
|
|
|
realCtx = realCtx.next;
|
|
|
|
if (realCtx == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return realCtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext getActualDownstreamContext(DefaultChannelHandlerContext ctx) {
|
|
|
|
if (ctx == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext realCtx = ctx;
|
|
|
|
while (!realCtx.canHandleDownstream()) {
|
|
|
|
realCtx = realCtx.prev;
|
|
|
|
if (realCtx == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return realCtx;
|
|
|
|
}
|
|
|
|
|
2009-04-08 07:03:53 +00:00
|
|
|
protected void notifyHandlerException(ChannelEvent e, Throwable t) {
|
2008-08-18 11:11:55 +00:00
|
|
|
if (e instanceof ExceptionEvent) {
|
|
|
|
logger.warn(
|
|
|
|
"An exception was thrown by a user handler " +
|
|
|
|
"while handling an exception event (" + e + ")", t);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
ChannelPipelineException pe;
|
|
|
|
if (t instanceof ChannelPipelineException) {
|
|
|
|
pe = (ChannelPipelineException) t;
|
|
|
|
} else {
|
|
|
|
pe = new ChannelPipelineException(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
sink.exceptionCaught(this, e, pe);
|
|
|
|
} catch (Exception e1) {
|
|
|
|
logger.warn("An exception was thrown by an exception handler.", e1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void init(String name, ChannelHandler handler) {
|
|
|
|
DefaultChannelHandlerContext ctx = new DefaultChannelHandlerContext(null, null, name, handler);
|
2009-12-17 09:05:46 +00:00
|
|
|
callBeforeAdd(ctx);
|
2008-08-08 00:37:18 +00:00
|
|
|
head = tail = ctx;
|
|
|
|
name2ctx.clear();
|
|
|
|
name2ctx.put(name, ctx);
|
2009-12-17 09:05:46 +00:00
|
|
|
callAfterAdd(ctx);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void checkDuplicateName(String name) {
|
|
|
|
if (name2ctx.containsKey(name)) {
|
2011-11-22 17:29:54 +01:00
|
|
|
throw new IllegalArgumentException("Duplicate handler name " + name);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DefaultChannelHandlerContext getContextOrDie(String name) {
|
|
|
|
DefaultChannelHandlerContext ctx = (DefaultChannelHandlerContext) getContext(name);
|
|
|
|
if (ctx == null) {
|
|
|
|
throw new NoSuchElementException(name);
|
|
|
|
} else {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DefaultChannelHandlerContext getContextOrDie(ChannelHandler handler) {
|
|
|
|
DefaultChannelHandlerContext ctx = (DefaultChannelHandlerContext) getContext(handler);
|
|
|
|
if (ctx == null) {
|
|
|
|
throw new NoSuchElementException(handler.getClass().getName());
|
|
|
|
} else {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DefaultChannelHandlerContext getContextOrDie(Class<? extends ChannelHandler> handlerType) {
|
|
|
|
DefaultChannelHandlerContext ctx = (DefaultChannelHandlerContext) getContext(handlerType);
|
|
|
|
if (ctx == null) {
|
|
|
|
throw new NoSuchElementException(handlerType.getName());
|
|
|
|
} else {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-11 10:45:55 +00:00
|
|
|
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;
|
2009-03-11 10:53:52 +00:00
|
|
|
private volatile Object attachment;
|
2009-03-11 10:45:55 +00:00
|
|
|
|
|
|
|
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() + '.');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.prev = prev;
|
|
|
|
this.next = next;
|
|
|
|
this.name = name;
|
|
|
|
this.handler = handler;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2009-03-11 10:45:55 +00:00
|
|
|
public Channel getChannel() {
|
|
|
|
return getPipeline().getChannel();
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2009-03-11 10:45:55 +00:00
|
|
|
public ChannelPipeline getPipeline() {
|
|
|
|
return DefaultChannelPipeline.this;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2009-03-11 10:45:55 +00:00
|
|
|
public boolean canHandleDownstream() {
|
|
|
|
return canHandleDownstream;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2009-03-11 10:45:55 +00:00
|
|
|
public boolean canHandleUpstream() {
|
|
|
|
return canHandleUpstream;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2009-03-11 10:45:55 +00:00
|
|
|
public ChannelHandler getHandler() {
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2009-03-11 10:45:55 +00:00
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2009-03-11 10:53:52 +00:00
|
|
|
public Object getAttachment() {
|
|
|
|
return attachment;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2009-03-11 10:53:52 +00:00
|
|
|
public void setAttachment(Object attachment) {
|
|
|
|
this.attachment = attachment;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2009-03-11 10:45:55 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2009-03-11 10:45:55 +00:00
|
|
|
public void sendUpstream(ChannelEvent e) {
|
|
|
|
DefaultChannelHandlerContext next = getActualUpstreamContext(this.next);
|
|
|
|
if (next != null) {
|
|
|
|
DefaultChannelPipeline.this.sendUpstream(next, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final class DiscardingChannelSink implements ChannelSink {
|
|
|
|
DiscardingChannelSink() {
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2009-03-11 10:45:55 +00:00
|
|
|
public void eventSunk(ChannelPipeline pipeline, ChannelEvent e) {
|
2009-03-04 10:35:55 +00:00
|
|
|
logger.warn("Not attached yet; discarding: " + e);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2009-03-11 10:45:55 +00:00
|
|
|
public void exceptionCaught(ChannelPipeline pipeline,
|
2009-03-04 10:35:55 +00:00
|
|
|
ChannelEvent e, ChannelPipelineException cause) throws Exception {
|
|
|
|
throw cause;
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2009-03-11 10:45:55 +00:00
|
|
|
}
|
2009-08-28 07:15:49 +00:00
|
|
|
}
|