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