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.spring.integration;
018
019 import org.apache.camel.Exchange;
020 import org.apache.camel.ExchangePattern;
021 import org.apache.camel.Processor;
022 import org.apache.camel.RuntimeCamelException;
023 import org.apache.camel.impl.DefaultConsumer;
024 import org.apache.camel.spring.SpringCamelContext;
025 import org.apache.camel.util.ObjectHelper;
026 import org.springframework.integration.channel.BeanFactoryChannelResolver;
027 import org.springframework.integration.channel.ChannelResolver;
028 import org.springframework.integration.channel.DirectChannel;
029 import org.springframework.integration.core.MessageChannel;
030 import org.springframework.integration.message.MessageHandler;
031
032 /**
033 * A consumer of exchanges for the Spring Integration
034 * Please specify the inputChannel in the endpoint url for this consumer.
035 * If the message pattern is inOut, the outputChannel property
036 * should be set for the outgoing message.
037 *
038 * @version $Revision: 787831 $
039 */
040 public class SpringIntegrationConsumer extends DefaultConsumer implements MessageHandler {
041 private SpringCamelContext context;
042 private DirectChannel inputChannel;
043 private MessageChannel outputChannel;
044 private String inputChannelName;
045 private ChannelResolver channelResolver;
046 private SpringIntegrationEndpoint endpoint;
047
048 public SpringIntegrationConsumer(SpringIntegrationEndpoint endpoint, Processor processor) {
049 super(endpoint, processor);
050 this.endpoint = endpoint;
051 context = (SpringCamelContext) endpoint.getCamelContext();
052 if (context != null && endpoint.getMessageChannel() == null) {
053 channelResolver = new BeanFactoryChannelResolver(context.getApplicationContext());
054 inputChannelName = endpoint.getDefaultChannel();
055 if (ObjectHelper.isEmpty(inputChannelName)) {
056 inputChannelName = endpoint.getInputChannel();
057 }
058 if (!ObjectHelper.isEmpty(inputChannelName)) {
059 inputChannel = (DirectChannel) channelResolver.resolveChannelName(inputChannelName);
060 ObjectHelper.notNull(inputChannel, "The inputChannel with the name [" + inputChannelName + "]");
061 } else {
062 throw new RuntimeCamelException("Can't find the right inputChannelName, please check your configuration.");
063 }
064 } else {
065 if (endpoint.getMessageChannel() != null) {
066 inputChannel = (DirectChannel)endpoint.getMessageChannel();
067 } else {
068 throw new RuntimeCamelException("Can't find the right message channel, please check your configuration.");
069 }
070 }
071 if (endpoint.isInOut()) {
072 endpoint.setExchangePattern(ExchangePattern.InOut);
073 }
074
075 }
076
077 protected void doStop() throws Exception {
078 inputChannel.unsubscribe(this);
079 super.doStop();
080 }
081
082 protected void doStart() throws Exception {
083 super.doStart();
084 inputChannel.subscribe(this);
085 }
086
087 public void handleMessage(org.springframework.integration.core.Message<?> siInMessage) {
088 Exchange exchange = getEndpoint().createExchange();
089 exchange.setIn(new SpringIntegrationMessage(siInMessage));
090 try {
091 getProcessor().process(exchange);
092 } catch (Exception e) {
093 //TODO need to find a way to deal with this exception
094 throw ObjectHelper.wrapRuntimeCamelException(e);
095 }
096 if (endpoint.isInOut()) {
097 // get the output channel from message header
098 Object returnAddress = siInMessage.getHeaders().getReplyChannel();
099 MessageChannel reply = null;
100
101 if (returnAddress != null) {
102 if (returnAddress instanceof String) {
103 reply = (MessageChannel)context.getApplicationContext().getBean((String)returnAddress);
104 } else if (returnAddress instanceof MessageChannel) {
105 reply = (MessageChannel) returnAddress;
106 }
107 } else {
108 if (outputChannel != null) {
109 // using the outputChannel
110 reply = outputChannel;
111 } else {
112 if (ObjectHelper.isEmpty(endpoint.getOutputChannel())) {
113 outputChannel = (MessageChannel) channelResolver.resolveChannelName(endpoint.getOutputChannel());
114 ObjectHelper.notNull(inputChannel, "The outputChannel with the name [" + endpoint.getOutputChannel() + "]");
115 reply = outputChannel;
116 } else {
117 throw new RuntimeCamelException("Can't find the right outputChannelName");
118 }
119 }
120 }
121 // put the message back the outputChannel if we need
122 org.springframework.integration.core.Message siOutMessage =
123 SpringIntegrationBinding.storeToSpringIntegrationMessage(exchange.getOut());
124 reply.send(siOutMessage);
125 }
126 }
127
128 }