private void callAfterAdd(ChannelHandlerContext ctx) { try { ctx.handler().afterAdd(ctx); } catch (Throwable t) { boolean removed = false; try { remove((DefaultChannelHandlerContext) ctx, false); removed = true; } catch (Throwable t2) { if (logger.isWarnEnabled()) { logger.warn("Failed to remove a handler: " + ctx.name(), t2); } } if (removed) { throw new ChannelPipelineException( ctx.handler().getClass().getName() + ".afterAdd() has thrown an exception; removed.", t); } else { throw new ChannelPipelineException( ctx.handler().getClass().getName() + ".afterAdd() has thrown an exception; also failed to remove.", t); } } }
private static void callBeforeRemove(ChannelHandlerContext ctx) { try { ctx.handler().beforeRemove(ctx); } catch (Throwable t) { throw new ChannelPipelineException( ctx.handler().getClass().getName() + ".beforeRemove() has thrown an exception; not removing.", t); } }
@Override public ChannelHandler get(String name) { ChannelHandlerContext ctx = context(name); if (ctx == null) { return null; } else { return ctx.handler(); } }
@SuppressWarnings("unchecked") @Override public <T extends ChannelHandler> T get(Class<T> handlerType) { ChannelHandlerContext ctx = context(handlerType); if (ctx == null) { return null; } else { return (T) ctx.handler(); } }
private static void callBeforeAdd(ChannelHandlerContext ctx) { ChannelHandler handler = ctx.handler(); if (handler instanceof ChannelStateHandlerAdapter) { ChannelStateHandlerAdapter h = (ChannelStateHandlerAdapter) handler; if (!h.isSharable() && h.added) { throw new ChannelPipelineException( h.getClass().getName() + " is not a @Sharable handler, so can't be added or removed multiple times."); } h.added = true; } try { handler.beforeAdd(ctx); } catch (Throwable t) { throw new ChannelPipelineException( handler.getClass().getName() + ".beforeAdd() has thrown an exception; not adding.", t); } }