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.adapter;
018
019 import org.apache.camel.CamelContext;
020 import org.apache.camel.Exchange;
021 import org.apache.camel.ExchangePattern;
022 import org.apache.camel.ProducerTemplate;
023 import org.apache.camel.component.spring.integration.SpringIntegrationBinding;
024 import org.apache.camel.impl.DefaultCamelContext;
025 import org.apache.camel.impl.DefaultExchange;
026 import org.apache.commons.logging.Log;
027 import org.apache.commons.logging.LogFactory;
028 import org.springframework.integration.core.Message;
029 import org.springframework.integration.core.MessageChannel;
030 import org.springframework.integration.core.MessageHeaders;
031 import org.springframework.integration.message.MessageDeliveryException;
032 import org.springframework.integration.message.MessageHandler;
033 import org.springframework.integration.message.MessageRejectedException;
034
035 /**
036 * CamelTargeAdapter will redirect the Spring Integration message to the Camel context.
037 * When we inject the camel context into it, we need also specify the Camel endpoint url
038 * we will route the Spring Integration message to the Camel context
039 *
040 * @version $Revision: 792319 $
041 */
042 public class CamelTargetAdapter extends AbstractCamelAdapter implements MessageHandler {
043
044 private final Log logger = LogFactory.getLog(this.getClass());
045 private ProducerTemplate camelTemplate;
046 private MessageChannel replyChannel;
047
048
049 public void setReplyChannel(MessageChannel channel) {
050 replyChannel = channel;
051 }
052
053 public MessageChannel getReplyChannel() {
054 return replyChannel;
055 }
056
057 public ProducerTemplate getCamelTemplate() {
058 if (camelTemplate == null) {
059 CamelContext ctx = getCamelContext();
060 if (ctx == null) {
061 ctx = new DefaultCamelContext();
062 }
063 camelTemplate = ctx.createProducerTemplate();
064 }
065 return camelTemplate;
066 }
067
068 public boolean send(Message<?> message) throws MessageRejectedException, MessageDeliveryException {
069 ExchangePattern pattern;
070 boolean result = false;
071 if (isExpectReply()) {
072 pattern = ExchangePattern.InOut;
073 } else {
074 pattern = ExchangePattern.InOnly;
075 }
076 Exchange inExchange = new DefaultExchange(getCamelContext(), pattern);
077 SpringIntegrationBinding.storeToCamelMessage(message, inExchange.getIn());
078 Exchange outExchange = getCamelTemplate().send(getCamelEndpointUri(), inExchange);
079 if (outExchange.getFault() != null) {
080 result = true;
081 }
082 Message response = null;
083 if (isExpectReply()) {
084 //Check the message header for the return address
085 response = SpringIntegrationBinding.storeToSpringIntegrationMessage(outExchange.getOut());
086 if (replyChannel == null) {
087 MessageChannel messageReplyChannel = (MessageChannel) message.getHeaders().get(MessageHeaders.REPLY_CHANNEL);
088 if (messageReplyChannel != null) {
089 result = messageReplyChannel.send(response);
090 } else {
091 throw new MessageDeliveryException(response, "Can't find reply channel from the CamelTargetAdapter or MessageHeaders");
092 }
093 } else {
094 result = replyChannel.send(response);
095 }
096 }
097 return result;
098 }
099
100 public void handleMessage(Message<?> message) {
101 send(message);
102 }
103
104 }