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.processor;
018    
019    import org.apache.camel.AsyncCallback;
020    import org.apache.camel.AsyncProcessor;
021    import org.apache.camel.Endpoint;
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Producer;
024    import org.apache.camel.Service;
025    import org.apache.camel.impl.ServiceSupport;
026    import org.apache.camel.impl.converter.AsyncProcessorTypeConverter;
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    
030    /**
031     * Processor for forwarding exchanges to an endpoint destination.
032     *
033     * @version $Revision: 41826 $
034     */
035    public class SendProcessor extends ServiceSupport implements AsyncProcessor, Service {
036        private static final transient Log LOG = LogFactory.getLog(SendProcessor.class);
037        private Endpoint destination;
038        private Producer producer;
039        private AsyncProcessor processor;
040    
041        public SendProcessor(Endpoint destination) {
042            if (destination == null) {
043                throw new IllegalArgumentException("Endpoint cannot be null!");
044            }
045            this.destination = destination;
046        }
047    
048        @Override
049        public String toString() {
050            return "sendTo(" + destination + ")";
051        }
052    
053        public void process(Exchange exchange) throws Exception {
054            if (producer == null) {
055                if (isStopped()) {
056                    LOG.warn("Ignoring exchange sent after processor is stopped: " + exchange);
057                } else {
058                    throw new IllegalStateException("No producer, this processor has not been started!");
059                }
060            } else {
061                producer.process(exchange);
062            }
063        }
064    
065        public boolean process(Exchange exchange, AsyncCallback callback) {
066            if (producer == null) {
067                if (isStopped()) {
068                    LOG.warn("Ignoring exchange sent after processor is stopped: " + exchange);
069                } else {
070                    exchange.setException(new IllegalStateException("No producer, this processor has not been started!"));
071                }
072                callback.done(true);
073                return true;
074            } else {
075                return processor.process(exchange, callback);
076            }
077        }
078        
079        public Endpoint getDestination() {
080            return destination;
081        }
082    
083        protected void doStart() throws Exception {
084            this.producer = destination.createProducer();
085            this.producer.start();
086            this.processor = AsyncProcessorTypeConverter.convert(producer);
087        }
088    
089        protected void doStop() throws Exception {
090            if (producer != null) {
091                try {
092                    producer.stop();
093                } finally {
094                    producer = null;
095                    processor = null;
096                }
097            }
098        }
099    
100    }