001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.component.netty; 018 019 import org.apache.camel.Consumer; 020 import org.apache.camel.Exchange; 021 import org.apache.camel.Processor; 022 import org.apache.camel.Producer; 023 import org.apache.camel.impl.DefaultEndpoint; 024 import org.apache.camel.impl.SynchronousDelegateProducer; 025 import org.apache.camel.util.ObjectHelper; 026 import org.jboss.netty.channel.ChannelHandlerContext; 027 import org.jboss.netty.channel.MessageEvent; 028 import org.jboss.netty.util.Timer; 029 030 public class NettyEndpoint extends DefaultEndpoint { 031 private NettyConfiguration configuration; 032 private Timer timer; 033 034 public NettyEndpoint(String endpointUri, NettyComponent component, NettyConfiguration configuration) { 035 super(endpointUri, component); 036 this.configuration = configuration; 037 } 038 039 public Consumer createConsumer(Processor processor) throws Exception { 040 return new NettyConsumer(this, processor, configuration); 041 } 042 043 public Producer createProducer() throws Exception { 044 Producer answer = new NettyProducer(this, configuration); 045 if (isSynchronous()) { 046 return new SynchronousDelegateProducer(answer); 047 } else { 048 return answer; 049 } 050 } 051 052 public Exchange createExchange(ChannelHandlerContext ctx, MessageEvent messageEvent) { 053 Exchange exchange = createExchange(); 054 exchange.getIn().setHeader(NettyConstants.NETTY_CHANNEL_HANDLER_CONTEXT, ctx); 055 exchange.getIn().setHeader(NettyConstants.NETTY_MESSAGE_EVENT, messageEvent); 056 exchange.getIn().setHeader(NettyConstants.NETTY_REMOTE_ADDRESS, messageEvent.getRemoteAddress()); 057 NettyPayloadHelper.setIn(exchange, messageEvent.getMessage()); 058 return exchange; 059 } 060 061 public boolean isSingleton() { 062 return true; 063 } 064 065 public NettyConfiguration getConfiguration() { 066 return configuration; 067 } 068 069 public void setConfiguration(NettyConfiguration configuration) { 070 this.configuration = configuration; 071 } 072 073 public void setTimer(Timer timer) { 074 this.timer = timer; 075 } 076 077 public Timer getTimer() { 078 return timer; 079 } 080 081 @Override 082 public void start() throws Exception { 083 super.start(); 084 ObjectHelper.notNull(timer, "timer"); 085 } 086 087 }