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.Endpoint;
020 import org.apache.camel.Exchange;
021 import org.apache.camel.Processor;
022 import org.apache.camel.Producer;
023 import org.apache.camel.Service;
024 import org.apache.camel.impl.ServiceSupport;
025 import org.apache.commons.logging.Log;
026 import org.apache.commons.logging.LogFactory;
027
028 /**
029 * @version $Revision: 563607 $
030 */
031 public class SendProcessor extends ServiceSupport implements Processor, Service {
032 private static final transient Log LOG = LogFactory.getLog(SendProcessor.class);
033 private Endpoint destination;
034 private Producer producer;
035
036 public SendProcessor(Endpoint destination) {
037 if (destination == null) {
038 throw new IllegalArgumentException("Endpoint cannot be null!");
039 }
040 this.destination = destination;
041 }
042
043 protected void doStop() throws Exception {
044 if (producer != null) {
045 try {
046 producer.stop();
047 } finally {
048 producer = null;
049 }
050 }
051 }
052
053 protected void doStart() throws Exception {
054 this.producer = destination.createProducer();
055 }
056
057 public void process(Exchange exchange) throws Exception {
058 if (producer == null) {
059 if (isStopped()) {
060 LOG.warn("Ignoring exchange sent after processor is stopped: " + exchange);
061 } else {
062 throw new IllegalStateException("No producer, this processor has not been started!");
063 }
064 } else {
065 producer.process(exchange);
066 }
067 }
068
069 public Endpoint getDestination() {
070 return destination;
071 }
072
073 @Override
074 public String toString() {
075 return "sendTo(" + destination + ")";
076 }
077 }