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