1 /* 2 * Copyright 2012 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 org.jboss.netty.channel; 17 18 import static org.jboss.netty.channel.Channels.*; 19 20 /** 21 * A skeletal {@link ChannelSink} implementation. 22 */ 23 public abstract class AbstractChannelSink implements ChannelSink { 24 25 /** 26 * Creates a new instance. 27 */ 28 protected AbstractChannelSink() { 29 super(); 30 } 31 32 /** 33 * Sends an {@link ExceptionEvent} upstream with the specified 34 * {@code cause}. 35 * 36 * @param event the {@link ChannelEvent} which caused a 37 * {@link ChannelHandler} to raise an exception 38 * @param cause the exception raised by a {@link ChannelHandler} 39 */ 40 public void exceptionCaught(ChannelPipeline pipeline, 41 ChannelEvent event, ChannelPipelineException cause) throws Exception { 42 Throwable actualCause = cause.getCause(); 43 if (actualCause == null) { 44 actualCause = cause; 45 } 46 if (isFireExceptionCaughtLater(event, actualCause)) { 47 fireExceptionCaughtLater(event.getChannel(), actualCause); 48 } else { 49 fireExceptionCaught(event.getChannel(), actualCause); 50 } 51 } 52 53 /** 54 * Returns {@code true} if and only if the specified {@code actualCause}, which was raised while 55 * handling the specified {@code event}, must trigger an {@code exceptionCaught()} event in 56 * an I/O thread. 57 * 58 * @param event the event which raised exception 59 * @param actualCause the raised exception 60 */ 61 protected boolean isFireExceptionCaughtLater(ChannelEvent event, Throwable actualCause) { 62 return false; 63 } 64 65 /** 66 * This implementation just directly call {@link Runnable#run()}. 67 * Sub-classes should override this if they can handle it in a better way 68 */ 69 public ChannelFuture execute(ChannelPipeline pipeline, Runnable task) { 70 try { 71 task.run(); 72 return Channels.succeededFuture(pipeline.getChannel()); 73 } catch (Throwable t) { 74 return Channels.failedFuture(pipeline.getChannel(), t); 75 } 76 } 77 }