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.impl;
018
019 import java.util.Map;
020
021 import org.apache.camel.CamelContext;
022 import org.apache.camel.Consumer;
023 import org.apache.camel.Endpoint;
024 import org.apache.camel.Exchange;
025 import org.apache.camel.ExchangePattern;
026 import org.apache.camel.PollingConsumer;
027 import org.apache.camel.Processor;
028 import org.apache.camel.Producer;
029 import org.apache.camel.util.ServiceHelper;
030 import org.apache.commons.logging.Log;
031 import org.apache.commons.logging.LogFactory;
032
033 /**
034 * This is an endpoint when sending to it, is intercepted and is routed in a detour
035 *
036 * @version $Revision: 789593 $
037 */
038 public class InterceptSendToEndpoint implements Endpoint {
039
040 private static final transient Log LOG = LogFactory.getLog(InterceptSendToEndpoint.class);
041
042 private final Endpoint delegate;
043 private Producer producer;
044 private Processor detour;
045 private boolean skip;
046
047 /**
048 * Intercepts sending to the given endpoint
049 *
050 * @param destination the original endpoint
051 * @param skip <tt>true</tt> to skip sending after the detour to the original endpoint
052 */
053 public InterceptSendToEndpoint(final Endpoint destination, boolean skip) {
054 this.delegate = destination;
055 this.skip = skip;
056 }
057
058 public void setDetour(Processor detour) {
059 this.detour = detour;
060 }
061
062 public Endpoint getDelegate() {
063 return delegate;
064 }
065
066 public String getEndpointUri() {
067 return delegate.getEndpointUri();
068 }
069
070 public String getEndpointKey() {
071 return delegate.getEndpointKey();
072 }
073
074 public Exchange createExchange() {
075 return delegate.createExchange();
076 }
077
078 public Exchange createExchange(ExchangePattern pattern) {
079 return delegate.createExchange(pattern);
080 }
081
082 public Exchange createExchange(Exchange exchange) {
083 return delegate.createExchange(exchange);
084 }
085
086 public CamelContext getCamelContext() {
087 return delegate.getCamelContext();
088 }
089
090 public Producer createProducer() throws Exception {
091 producer = delegate.createProducer();
092 return new Producer() {
093
094 public Endpoint getEndpoint() {
095 return producer.getEndpoint();
096 }
097
098 public Exchange createExchange() {
099 return producer.createExchange();
100 }
101
102 public Exchange createExchange(ExchangePattern pattern) {
103 return producer.createExchange(pattern);
104 }
105
106 public Exchange createExchange(Exchange exchange) {
107 return producer.createExchange(exchange);
108 }
109
110 public void process(Exchange exchange) throws Exception {
111 // process the detour so we do the detour routing
112 if (LOG.isDebugEnabled()) {
113 LOG.debug("Sending to endpoint: " + getEndpointUri() + " is intercepted and detoured to: " + detour + " for exchange: " + exchange);
114 }
115 // add header with the real endpoint uri
116 exchange.getIn().setHeader(Exchange.INTERCEPTED_ENDPOINT, delegate.getEndpointUri());
117
118 detour.process(exchange);
119 // copy OUT to IN
120 if (exchange.hasOut()) {
121 // replace OUT with IN as detour changed something
122 exchange.setIn(exchange.getOut());
123 exchange.setOut(null);
124 }
125
126 if (!skip) {
127 // route to original destination
128 producer.process(exchange);
129 } else {
130 if (LOG.isDebugEnabled()) {
131 LOG.debug("Stop() means skip sending exchange to original intended destination: " + getEndpointUri() + " for exchange: " + exchange);
132 }
133 }
134 }
135
136 public boolean isSingleton() {
137 return producer.isSingleton();
138 }
139
140 public void start() throws Exception {
141 ServiceHelper.startService(detour);
142 }
143
144 public void stop() throws Exception {
145 ServiceHelper.stopService(detour);
146 }
147 };
148 }
149
150 public Consumer createConsumer(Processor processor) throws Exception {
151 return delegate.createConsumer(processor);
152 }
153
154 public PollingConsumer createPollingConsumer() throws Exception {
155 return delegate.createPollingConsumer();
156 }
157
158 public void configureProperties(Map options) {
159 delegate.configureProperties(options);
160 }
161
162 public void setCamelContext(CamelContext context) {
163 delegate.setCamelContext(context);
164 }
165
166 public boolean isLenientProperties() {
167 return delegate.isLenientProperties();
168 }
169
170 public boolean isSingleton() {
171 return delegate.isSingleton();
172 }
173
174 @Override
175 public String toString() {
176 return delegate.toString();
177 }
178 }