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 java.util.concurrent.atomic.AtomicBoolean;
020
021 import org.apache.camel.Consumer;
022 import org.apache.camel.Endpoint;
023 import org.apache.camel.Exchange;
024 import org.apache.camel.Processor;
025 import org.apache.camel.component.spring.integration.SpringIntegrationBinding;
026 import org.apache.commons.logging.Log;
027 import org.apache.commons.logging.LogFactory;
028 import org.springframework.beans.factory.DisposableBean;
029 import org.springframework.beans.factory.InitializingBean;
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 CamelContext will be injected into CameSourceAdapter which will
038 * let Spring Integration channel talk to the CamelContext certain endpoint
039 *
040 * @version $Revision: 792319 $
041 */
042 public class CamelSourceAdapter extends AbstractCamelAdapter implements InitializingBean, DisposableBean {
043 private static final Log LOG = LogFactory.getLog(CamelSourceAdapter.class);
044
045 private Consumer consumer;
046 private Endpoint camelEndpoint;
047 private MessageChannel requestChannel;
048 private DirectChannel replyChannel;
049
050 private AtomicBoolean initialized = new AtomicBoolean();
051
052 public void setRequestChannel(MessageChannel channel) {
053 requestChannel = channel;
054 }
055
056 public MessageChannel getChannel() {
057 return requestChannel;
058 }
059
060 public void setReplyChannel(DirectChannel channel) {
061 replyChannel = channel;
062 }
063
064 protected class ConsumerProcessor implements Processor {
065
066 public void process(final Exchange exchange) throws Exception {
067 org.springframework.integration.core.Message request = SpringIntegrationBinding.createSpringIntegrationMessage(exchange);
068
069 if (exchange.getPattern().isOutCapable()) {
070 exchange.getIn().getHeaders().put(MessageHeaders.REPLY_CHANNEL , replyChannel);
071 replyChannel.subscribe(new MessageHandler() {
072 public void handleMessage(Message<?> message) {
073 if (LOG.isDebugEnabled()) {
074 LOG.debug("set the out message with the SI response message");
075 }
076 //TODO set the corralationID
077 SpringIntegrationBinding.storeToCamelMessage(message, exchange.getOut());
078 }
079 });
080 }
081
082 requestChannel.send(request);
083 }
084 }
085
086 public final void afterPropertiesSet() throws Exception {
087 if (initialized.compareAndSet(false, true)) {
088 initialize();
089 }
090 }
091
092 public void destroy() throws Exception {
093 if (consumer != null) {
094 consumer.stop();
095 }
096 }
097
098 protected void initialize() throws Exception {
099 // start the service here
100 camelEndpoint = getCamelContext().getEndpoint(getCamelEndpointUri());
101 consumer = camelEndpoint.createConsumer(new ConsumerProcessor());
102 consumer.start();
103 }
104
105 }