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