use of org.jboss.netty.channel.ChannelHandler in project camel by apache.
the class NettyConfiguration method validateConfiguration.
public void validateConfiguration() {
// validate that the encoders is either shareable or is a handler factory
for (ChannelHandler encoder : encoders) {
if (encoder instanceof ChannelHandlerFactory) {
continue;
}
if (ObjectHelper.getAnnotation(encoder, ChannelHandler.Sharable.class) != null) {
continue;
}
LOG.warn("The encoder {} is not @Shareable or an ChannelHandlerFactory instance. The encoder cannot safely be used.", encoder);
}
// validate that the decoders is either shareable or is a handler factory
for (ChannelHandler decoder : decoders) {
if (decoder instanceof ChannelHandlerFactory) {
continue;
}
if (ObjectHelper.getAnnotation(decoder, ChannelHandler.Sharable.class) != null) {
continue;
}
LOG.warn("The decoder {} is not @Shareable or an ChannelHandlerFactory instance. The decoder cannot safely be used.", decoder);
}
if (sslHandler != null) {
boolean factory = sslHandler instanceof ChannelHandlerFactory;
boolean shareable = ObjectHelper.getAnnotation(sslHandler, ChannelHandler.Sharable.class) != null;
if (!factory && !shareable) {
LOG.warn("The sslHandler {} is not @Shareable or an ChannelHandlerFactory instance. The sslHandler cannot safely be used.", sslHandler);
}
}
}
use of org.jboss.netty.channel.ChannelHandler in project camel by apache.
the class NettyConfiguration method copy.
/**
* Returns a copy of this configuration
*/
public NettyConfiguration copy() {
try {
NettyConfiguration answer = (NettyConfiguration) clone();
// make sure the lists is copied in its own instance
List<ChannelHandler> encodersCopy = new ArrayList<ChannelHandler>(encoders);
answer.setEncoders(encodersCopy);
List<ChannelHandler> decodersCopy = new ArrayList<ChannelHandler>(decoders);
answer.setDecoders(decodersCopy);
return answer;
} catch (CloneNotSupportedException e) {
throw new RuntimeCamelException(e);
}
}
use of org.jboss.netty.channel.ChannelHandler in project camel by apache.
the class NettyConfiguration method parseURI.
public void parseURI(URI uri, Map<String, Object> parameters, NettyComponent component, String... supportedProtocols) throws Exception {
protocol = uri.getScheme();
boolean found = false;
for (String supportedProtocol : supportedProtocols) {
if (protocol != null && protocol.equalsIgnoreCase(supportedProtocol)) {
found = true;
break;
}
}
if (!found) {
throw new IllegalArgumentException("Unrecognized Netty protocol: " + protocol + " for uri: " + uri);
}
setHost(uri.getHost());
if (uri.getPort() != -1) {
setPort(uri.getPort());
}
ssl = component.getAndRemoveOrResolveReferenceParameter(parameters, "ssl", boolean.class, false);
sslHandler = component.getAndRemoveOrResolveReferenceParameter(parameters, "sslHandler", SslHandler.class, sslHandler);
passphrase = component.getAndRemoveOrResolveReferenceParameter(parameters, "passphrase", String.class, passphrase);
keyStoreFormat = component.getAndRemoveOrResolveReferenceParameter(parameters, "keyStoreFormat", String.class, keyStoreFormat == null ? "JKS" : keyStoreFormat);
securityProvider = component.getAndRemoveOrResolveReferenceParameter(parameters, "securityProvider", String.class, securityProvider == null ? "SunX509" : securityProvider);
keyStoreFile = component.getAndRemoveOrResolveReferenceParameter(parameters, "keyStoreFile", File.class, keyStoreFile);
trustStoreFile = component.getAndRemoveOrResolveReferenceParameter(parameters, "trustStoreFile", File.class, trustStoreFile);
keyStoreResource = component.getAndRemoveOrResolveReferenceParameter(parameters, "keyStoreResource", String.class, keyStoreResource);
trustStoreResource = component.getAndRemoveOrResolveReferenceParameter(parameters, "trustStoreResource", String.class, trustStoreResource);
clientPipelineFactory = component.getAndRemoveOrResolveReferenceParameter(parameters, "clientPipelineFactory", ClientPipelineFactory.class, clientPipelineFactory);
serverPipelineFactory = component.getAndRemoveOrResolveReferenceParameter(parameters, "serverPipelineFactory", ServerPipelineFactory.class, serverPipelineFactory);
// set custom encoders and decoders first
List<ChannelHandler> referencedEncoders = component.resolveAndRemoveReferenceListParameter(parameters, "encoders", ChannelHandler.class, null);
addToHandlersList(encoders, referencedEncoders, ChannelHandler.class);
List<ChannelHandler> referencedDecoders = component.resolveAndRemoveReferenceListParameter(parameters, "decoders", ChannelHandler.class, null);
addToHandlersList(decoders, referencedDecoders, ChannelHandler.class);
// then set parameters with the help of the camel context type converters
EndpointHelper.setReferenceProperties(component.getCamelContext(), this, parameters);
EndpointHelper.setProperties(component.getCamelContext(), this, parameters);
// additional netty options, we don't want to store an empty map, so set it as null if empty
options = IntrospectionSupport.extractProperties(parameters, "option.");
if (options != null && options.isEmpty()) {
options = null;
}
// add default encoders and decoders
if (encoders.isEmpty() && decoders.isEmpty()) {
if (isAllowDefaultCodec()) {
// are we textline or object?
if (isTextline()) {
Charset charset = getEncoding() != null ? Charset.forName(getEncoding()) : CharsetUtil.UTF_8;
encoders.add(ChannelHandlerFactories.newStringEncoder(charset));
ChannelBuffer[] delimiters = delimiter == TextLineDelimiter.LINE ? Delimiters.lineDelimiter() : Delimiters.nulDelimiter();
decoders.add(ChannelHandlerFactories.newDelimiterBasedFrameDecoder(decoderMaxLineLength, delimiters));
decoders.add(ChannelHandlerFactories.newStringDecoder(charset));
if (LOG.isDebugEnabled()) {
LOG.debug("Using textline encoders and decoders with charset: {}, delimiter: {} and decoderMaxLineLength: {}", new Object[] { charset, delimiter, decoderMaxLineLength });
}
} else {
// object serializable is then used
encoders.add(ChannelHandlerFactories.newObjectEncoder());
decoders.add(ChannelHandlerFactories.newObjectDecoder());
LOG.debug("Using object encoders and decoders");
}
} else {
LOG.debug("No encoders and decoders will be used");
}
} else {
LOG.debug("Using configured encoders and/or decoders");
}
}
use of org.jboss.netty.channel.ChannelHandler in project camel by apache.
the class NettyProducer method process.
public boolean process(final Exchange exchange, AsyncCallback callback) {
if (!isRunAllowed()) {
if (exchange.getException() == null) {
exchange.setException(new RejectedExecutionException());
}
callback.done(true);
return true;
}
Object body;
try {
body = getRequestBody(exchange);
if (body == null) {
noReplyLogger.log("No payload to send for exchange: " + exchange);
callback.done(true);
return true;
}
} catch (Exception e) {
exchange.setException(e);
callback.done(true);
return true;
}
// set the exchange encoding property
if (getConfiguration().getCharsetName() != null) {
exchange.setProperty(Exchange.CHARSET_NAME, IOHelper.normalizeCharset(getConfiguration().getCharsetName()));
}
if (LOG.isTraceEnabled()) {
LOG.trace("Pool[active={}, idle={}]", pool.getNumActive(), pool.getNumIdle());
}
// get a channel from the pool
Channel existing;
try {
existing = pool.borrowObject();
if (existing != null) {
LOG.trace("Got channel from pool {}", existing);
}
} catch (Exception e) {
exchange.setException(e);
callback.done(true);
return true;
}
// we must have a channel
if (existing == null) {
exchange.setException(new CamelExchangeException("Cannot get channel from pool", exchange));
callback.done(true);
return true;
}
if (exchange.getIn().getHeader(NettyConstants.NETTY_REQUEST_TIMEOUT) != null) {
long timeoutInMs = exchange.getIn().getHeader(NettyConstants.NETTY_REQUEST_TIMEOUT, Long.class);
ChannelHandler oldHandler = existing.getPipeline().get("timeout");
ReadTimeoutHandler newHandler = new ReadTimeoutHandler(getEndpoint().getTimer(), timeoutInMs, TimeUnit.MILLISECONDS);
if (oldHandler == null) {
existing.getPipeline().addBefore("handler", "timeout", newHandler);
} else {
existing.getPipeline().replace(oldHandler, "timeout", newHandler);
}
}
// need to declare as final
final Channel channel = existing;
final AsyncCallback producerCallback = new NettyProducerCallback(channel, callback);
// setup state as attachment on the channel, so we can access the state later when needed
channel.setAttachment(new NettyCamelState(producerCallback, exchange));
InetSocketAddress remoteAddress = null;
if (!isTcp()) {
// Need to specify the remoteAddress for udp connection
remoteAddress = new InetSocketAddress(configuration.getHost(), configuration.getPort());
}
// write body
NettyHelper.writeBodyAsync(LOG, channel, remoteAddress, body, exchange, new ChannelFutureListener() {
public void operationComplete(ChannelFuture channelFuture) throws Exception {
LOG.trace("Operation complete {}", channelFuture);
if (!channelFuture.isSuccess()) {
// no success then exit, (any exception has been handled by ClientChannelHandler#exceptionCaught)
return;
}
// if we do not expect any reply then signal callback to continue routing
if (!configuration.isSync()) {
try {
// should channel be closed after complete?
Boolean close;
if (ExchangeHelper.isOutCapable(exchange)) {
close = exchange.getOut().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
} else {
close = exchange.getIn().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
}
// should we disconnect, the header can override the configuration
boolean disconnect = getConfiguration().isDisconnect();
if (close != null) {
disconnect = close;
}
if (disconnect) {
if (LOG.isTraceEnabled()) {
LOG.trace("Closing channel when complete at address: {}", getEndpoint().getConfiguration().getAddress());
}
NettyHelper.close(channel);
}
} finally {
// signal callback to continue routing
producerCallback.done(false);
}
}
}
});
// continue routing asynchronously
return false;
}
use of org.jboss.netty.channel.ChannelHandler in project camel by apache.
the class ClientChannelHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent messageEvent) throws Exception {
messageReceived = true;
if (LOG.isTraceEnabled()) {
LOG.trace("Message received: {}", messageEvent);
}
ChannelHandler handler = ctx.getPipeline().get("timeout");
if (handler != null) {
LOG.trace("Removing timeout channel as we received message");
ctx.getPipeline().remove(handler);
}
Exchange exchange = getExchange(ctx);
if (exchange == null) {
// we just ignore the received message as the channel is closed
return;
}
AsyncCallback callback = getAsyncCallback(ctx);
Message message;
try {
message = getResponseMessage(exchange, messageEvent);
} catch (Exception e) {
exchange.setException(e);
callback.done(false);
return;
}
// set the result on either IN or OUT on the original exchange depending on its pattern
if (ExchangeHelper.isOutCapable(exchange)) {
exchange.setOut(message);
} else {
exchange.setIn(message);
}
try {
// should channel be closed after complete?
Boolean close;
if (ExchangeHelper.isOutCapable(exchange)) {
close = exchange.getOut().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
} else {
close = exchange.getIn().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
}
// check the setting on the exchange property
if (close == null) {
close = exchange.getProperty(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
}
// should we disconnect, the header can override the configuration
boolean disconnect = producer.getConfiguration().isDisconnect();
if (close != null) {
disconnect = close;
}
if (disconnect) {
if (LOG.isTraceEnabled()) {
LOG.trace("Closing channel when complete at address: {}", producer.getConfiguration().getAddress());
}
NettyHelper.close(ctx.getChannel());
}
} finally {
// signal callback
callback.done(false);
}
}
Aggregations