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 java.util.HashMap;
020    import java.util.Map;
021    
022    import org.apache.camel.AsyncCallback;
023    import org.apache.camel.AsyncProcessor;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.ExchangePattern;
026    import org.apache.camel.RuntimeCamelException;
027    import org.apache.camel.impl.DefaultProducer;
028    import org.apache.camel.spring.SpringCamelContext;
029    import org.apache.camel.util.AsyncProcessorHelper;
030    import org.apache.camel.util.ObjectHelper;
031    import org.apache.commons.logging.Log;
032    import org.apache.commons.logging.LogFactory;
033    import org.springframework.integration.channel.BeanFactoryChannelResolver;
034    import org.springframework.integration.channel.ChannelResolver;
035    import org.springframework.integration.channel.DirectChannel;
036    import org.springframework.integration.core.Message;
037    import org.springframework.integration.core.MessageChannel;
038    import org.springframework.integration.core.MessageHeaders;
039    import org.springframework.integration.message.MessageHandler;
040    
041    /**
042     * A producer of exchanges for the Spring Integration
043     * Please specify the outputChannel in the endpoint url for this producer.
044     * If the message pattern is inOut, the inputChannel property
045     * should be set for receiving the response message.
046     * @version $Revision: 711528 $
047     */
048    public class SpringIntegrationProducer extends DefaultProducer<SpringIntegrationExchange> implements AsyncProcessor {
049        private static final transient Log LOG = LogFactory.getLog(SpringIntegrationProducer.class);
050        private SpringCamelContext context;
051        private DirectChannel inputChannel;
052        private MessageChannel outputChannel;
053        private String outputChannelName;
054        private ChannelResolver channelResolver;
055        private SpringIntegrationEndpoint endpoint;
056    
057        public SpringIntegrationProducer(SpringIntegrationEndpoint endpoint) {
058            super(endpoint);
059            this.endpoint = endpoint;
060            context = (SpringCamelContext) endpoint.getCamelContext();
061            if (context != null && endpoint.getMessageChannel() == null) {
062                outputChannelName = endpoint.getDefaultChannel();
063                channelResolver = new BeanFactoryChannelResolver(context.getApplicationContext());
064                if (ObjectHelper.isNullOrBlank(outputChannelName)) {
065                    outputChannelName = endpoint.getInputChannel();
066                }
067                if (ObjectHelper.isNullOrBlank(outputChannelName)) {
068                    throw new RuntimeCamelException("Can't find the right outputChannelName, "
069                                                    + "please check the endpoint uri outputChannel part!");
070                } else {
071                    outputChannel = channelResolver.resolveChannelName(outputChannelName);
072                }
073            } else {
074                if (endpoint.getMessageChannel() != null) {
075                    outputChannel = endpoint.getMessageChannel();
076                } else {
077                    throw new RuntimeCamelException("Can't find the right message channel, please check your configuration.");
078                }
079            }
080            if (endpoint.isInOut()) {
081                endpoint.setExchangePattern(ExchangePattern.InOut);
082                // we need to setup right inputChannel for further processing
083                if (ObjectHelper.isNullOrBlank(endpoint.getInputChannel())) {
084                    throw new RuntimeCamelException("Can't find the right inputChannel, "
085                                                    + "please check the endpoint uri inputChannel part!");
086                } else {
087                    inputChannel = (DirectChannel)channelResolver.resolveChannelName(endpoint.getInputChannel());
088                }
089            } else {
090                endpoint.setExchangePattern(ExchangePattern.InOnly);
091            }
092        }
093    
094        public void process(Exchange exchange) throws Exception {
095            
096            AsyncProcessorHelper.process(this, exchange);       
097            
098        }
099    
100        public boolean process(final Exchange exchange, final AsyncCallback callback) {
101            Map<String, Object> headers = new HashMap<String, Object>();
102            if (exchange.getPattern().isOutCapable()) {
103                headers.put(MessageHeaders.REPLY_CHANNEL , inputChannel);
104                inputChannel.subscribe(new MessageHandler() {                
105                    public void handleMessage(Message<?> message) {                    
106                        SpringIntegrationBinding.storeToCamelMessage(message, exchange.getOut());
107                        callback.done(true);
108                    }
109                });
110            }
111            org.springframework.integration.core.Message siOutmessage = SpringIntegrationBinding.createSpringIntegrationMessage(exchange, headers);
112            
113            outputChannel.send(siOutmessage);
114            if (!exchange.getPattern().isOutCapable()) {
115                callback.done(true);
116            }
117            
118            return true;
119        }
120    
121    
122    }