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.ExchangePattern;
022 import org.apache.camel.Processor;
023 import org.apache.camel.Producer;
024 import org.apache.camel.ProducerCallback;
025 import org.apache.camel.impl.InterceptSendToEndpoint;
026 import org.apache.camel.impl.ProducerCache;
027 import org.apache.camel.impl.ServiceSupport;
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: 885202 $
036 */
037 public class SendProcessor extends ServiceSupport implements Processor, Traceable {
038 protected static final transient Log LOG = LogFactory.getLog(SendProcessor.class);
039 protected ProducerCache producerCache;
040 protected Endpoint destination;
041 protected ExchangePattern pattern;
042 private boolean init;
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 synchronized void setDestination(Endpoint destination) {
060 this.destination = destination;
061 this.init = false;
062 }
063
064 public String getTraceLabel() {
065 return destination.getEndpointUri();
066 }
067
068 public void process(final Exchange exchange) throws Exception {
069 // the destination could since have been intercepted by a interceptSendToEndpoint so we got to
070 // init this before we can use the destination
071 if (!init) {
072 init = true;
073 Endpoint lookup = exchange.getContext().hasEndpoint(destination.getEndpointKey());
074 if (lookup instanceof InterceptSendToEndpoint) {
075 if (LOG.isDebugEnabled()) {
076 LOG.debug("SendTo is intercepted using a interceptSendToEndpoint: " + lookup.getEndpointUri());
077 }
078 destination = lookup;
079 }
080 }
081
082 doProcess(exchange);
083 }
084
085 /**
086 * Strategy to process the exchange
087 *
088 * @param exchange the exchange
089 * @throws Exception can be thrown if error processing exchange
090 * @return the exchange that was processed
091 */
092 public Exchange doProcess(final Exchange exchange) throws Exception {
093 // send the exchange to the destination using a producer
094 return getProducerCache(exchange).doInProducer(destination, exchange, pattern, new ProducerCallback<Exchange>() {
095 public Exchange doInProducer(Producer producer, Exchange exchange, ExchangePattern pattern) throws Exception {
096 exchange = configureExchange(exchange, pattern);
097 producer.process(exchange);
098 return exchange;
099 }
100 });
101 }
102
103 protected ProducerCache getProducerCache(Exchange exchange) throws Exception {
104 // setup producer cache as we need to use the pluggable service pool defined on camel context
105 if (producerCache == null) {
106 producerCache = new ProducerCache(exchange.getContext());
107 producerCache.start();
108 }
109 return producerCache;
110 }
111
112 public Endpoint getDestination() {
113 return destination;
114 }
115
116 public ExchangePattern getPattern() {
117 return pattern;
118 }
119
120 protected Exchange configureExchange(Exchange exchange, ExchangePattern pattern) {
121 if (pattern != null) {
122 exchange.setPattern(pattern);
123 }
124 // set property which endpoint we send to
125 exchange.setProperty(Exchange.TO_ENDPOINT, destination.getEndpointUri());
126 return exchange;
127 }
128
129 protected void doStart() throws Exception {
130 if (producerCache != null) {
131 producerCache.start();
132 }
133 }
134
135 protected void doStop() throws Exception {
136 if (producerCache != null) {
137 producerCache.stop();
138 }
139 }
140
141 }