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