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.DefaultProducer;
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.Message;
030    import org.springframework.integration.core.MessageChannel;
031    import org.springframework.integration.core.MessageHeaders;
032    import org.springframework.integration.message.MessageHandler;
033    
034    /**
035     * A producer of exchanges for the Spring Integration
036     * Please specify the outputChannel in the endpoint url for this producer.
037     * If the message pattern is inOut, the inputChannel property
038     * should be set for receiving the response message.
039     * @version $Revision: 938760 $
040     */
041    public class SpringIntegrationProducer extends DefaultProducer implements Processor {    
042    
043        private SpringCamelContext context;
044        private DirectChannel inputChannel;
045        private MessageChannel outputChannel;
046        private String outputChannelName;
047        private ChannelResolver channelResolver;
048    
049    
050        public SpringIntegrationProducer(SpringIntegrationEndpoint endpoint) {
051            super(endpoint);
052            context = (SpringCamelContext) endpoint.getCamelContext();
053            if (context != null && endpoint.getMessageChannel() == null) {
054                outputChannelName = endpoint.getDefaultChannel();
055                channelResolver = new BeanFactoryChannelResolver(context.getApplicationContext());
056                if (ObjectHelper.isEmpty(outputChannelName)) {
057                    outputChannelName = endpoint.getInputChannel();
058                }
059                if (ObjectHelper.isEmpty(outputChannelName)) {
060                    throw new RuntimeCamelException("Cannot find outputChannelName, please check the endpoint uri outputChannel part!");
061                } else {
062                    outputChannel = channelResolver.resolveChannelName(outputChannelName);
063                }
064            } else {
065                if (endpoint.getMessageChannel() != null) {
066                    outputChannel = endpoint.getMessageChannel();
067                } else {
068                    throw new RuntimeCamelException("Cannot find message channel, please check your configuration.");
069                }
070            }
071            if (endpoint.isInOut()) {
072                endpoint.setExchangePattern(ExchangePattern.InOut);
073                // we need to setup right inputChannel for further processing
074                if (ObjectHelper.isEmpty(endpoint.getInputChannel())) {
075                    throw new RuntimeCamelException("Cannot find inputChannel, please check the endpoint uri inputChannel part!");
076                } else {
077                    inputChannel = (DirectChannel)channelResolver.resolveChannelName(endpoint.getInputChannel());
078                }
079            } else {
080                endpoint.setExchangePattern(ExchangePattern.InOnly);
081            }
082        }
083    
084        public void process(final Exchange exchange) throws Exception {
085            if (exchange.getPattern().isOutCapable()) {
086                exchange.getIn().getHeaders().put(MessageHeaders.REPLY_CHANNEL , inputChannel);
087                inputChannel.subscribe(new MessageHandler() {
088                    public void handleMessage(Message<?> message) {                    
089                        SpringIntegrationBinding.storeToCamelMessage(message, exchange.getOut());
090                    }
091                });
092            }
093            org.springframework.integration.core.Message siOutmessage = SpringIntegrationBinding.createSpringIntegrationMessage(exchange);
094            
095            outputChannel.send(siOutmessage);
096        }
097    
098    }