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.Processor;
021 import org.apache.camel.impl.DefaultProducer;
022 import org.apache.camel.spring.SpringCamelContext;
023 import org.apache.camel.util.ObjectHelper;
024 import org.springframework.integration.channel.BeanFactoryChannelResolver;
025 import org.springframework.integration.channel.ChannelResolver;
026 import org.springframework.integration.channel.DirectChannel;
027 import org.springframework.integration.core.Message;
028 import org.springframework.integration.core.MessageChannel;
029 import org.springframework.integration.core.MessageHeaders;
030 import org.springframework.integration.message.MessageHandler;
031
032 /**
033 * A producer of exchanges for the Spring Integration
034 * Please specify the outputChannel in the endpoint url for this producer.
035 * If the message pattern is inOut, the inputChannel property
036 * should be set for receiving the response message.
037 * @version $Revision: 1040056 $
038 */
039 public class SpringIntegrationProducer extends DefaultProducer implements Processor {
040 private final ChannelResolver channelResolver;
041 private DirectChannel inputChannel;
042 private MessageChannel outputChannel;
043
044 public SpringIntegrationProducer(SpringCamelContext context, SpringIntegrationEndpoint endpoint) {
045 super(endpoint);
046 this.channelResolver = new BeanFactoryChannelResolver(context.getApplicationContext());
047 }
048
049 @Override
050 public SpringIntegrationEndpoint getEndpoint() {
051 return (SpringIntegrationEndpoint) super.getEndpoint();
052 }
053
054 @Override
055 protected void doStart() throws Exception {
056 super.doStart();
057
058 if (getEndpoint().getMessageChannel() == null) {
059 String outputChannelName = getEndpoint().getDefaultChannel();
060 if (ObjectHelper.isEmpty(outputChannelName)) {
061 outputChannelName = getEndpoint().getInputChannel();
062 }
063
064 ObjectHelper.notEmpty(outputChannelName, "OutputChannelName", getEndpoint());
065 outputChannel = channelResolver.resolveChannelName(outputChannelName);
066 } else {
067 outputChannel = getEndpoint().getMessageChannel();
068 }
069
070 if (outputChannel == null) {
071 throw new IllegalArgumentException("Cannot resolve OutputChannel on " + getEndpoint());
072 }
073
074 // if we do in-out we need to setup the input channel as well
075 if (getEndpoint().isInOut()) {
076 // we need to setup right inputChannel for further processing
077 ObjectHelper.notEmpty(getEndpoint().getInputChannel(), "InputChannel", getEndpoint());
078 inputChannel = (DirectChannel)channelResolver.resolveChannelName(getEndpoint().getInputChannel());
079
080 if (inputChannel == null) {
081 throw new IllegalArgumentException("Cannot resolve InputChannel on " + getEndpoint());
082 }
083 }
084 }
085
086 public void process(final Exchange exchange) throws Exception {
087 if (exchange.getPattern().isOutCapable()) {
088
089 // we want to do in-out so the inputChannel is mandatory (used to receive reply from spring integration)
090 if (inputChannel == null) {
091 throw new IllegalArgumentException("InputChannel has not been configured on " + getEndpoint());
092 }
093 exchange.getIn().getHeaders().put(MessageHeaders.REPLY_CHANNEL , inputChannel);
094
095 // subscribe so we can receive the reply from spring integration
096 inputChannel.subscribe(new MessageHandler() {
097 public void handleMessage(Message<?> message) {
098 if (log.isDebugEnabled()) {
099 log.debug("Received " + message + " from InputChannel: " + inputChannel);
100 }
101 SpringIntegrationBinding.storeToCamelMessage(message, exchange.getOut());
102 }
103 });
104 }
105 org.springframework.integration.core.Message siOutmessage = SpringIntegrationBinding.createSpringIntegrationMessage(exchange);
106
107 // send the message to spring integration
108 if (log.isDebugEnabled()) {
109 log.debug("Sending " + siOutmessage + " to OutputChannel: " + outputChannel);
110 }
111 outputChannel.send(siOutmessage);
112 }
113
114 }