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 */
017package org.apache.camel.impl;
018
019import java.util.Map;
020
021import org.apache.camel.CamelContext;
022import org.apache.camel.Consumer;
023import org.apache.camel.Endpoint;
024import org.apache.camel.EndpointConfiguration;
025import org.apache.camel.Exchange;
026import org.apache.camel.ExchangePattern;
027import org.apache.camel.PollingConsumer;
028import org.apache.camel.Processor;
029import org.apache.camel.Producer;
030import org.apache.camel.ServicePoolAware;
031import org.apache.camel.ShutdownableService;
032import org.apache.camel.util.ServiceHelper;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036/**
037 * This is an endpoint when sending to it, is intercepted and is routed in a detour
038 *
039 * @version 
040 */
041public class InterceptSendToEndpoint implements Endpoint, ShutdownableService {
042
043    private static final Logger LOG = LoggerFactory.getLogger(InterceptSendToEndpoint.class);
044
045    private final Endpoint delegate;
046    private Processor detour;
047    private boolean skip;
048
049    /**
050     * Intercepts sending to the given endpoint
051     *
052     * @param destination  the original endpoint
053     * @param skip <tt>true</tt> to skip sending after the detour to the original endpoint
054     */
055    public InterceptSendToEndpoint(final Endpoint destination, boolean skip) {
056        this.delegate = destination;
057        this.skip = skip;
058    }
059
060    public void setDetour(Processor detour) {
061        this.detour = detour;
062    }
063
064    public Processor getDetour() {
065        return detour;
066    }
067
068    public Endpoint getDelegate() {
069        return delegate;
070    }
071
072    public String getEndpointUri() {
073        return delegate.getEndpointUri();
074    }
075
076    public EndpointConfiguration getEndpointConfiguration() {
077        return delegate.getEndpointConfiguration();
078    }
079
080    public String getEndpointKey() {
081        return delegate.getEndpointKey();
082    }
083
084    public Exchange createExchange() {
085        return delegate.createExchange();
086    }
087
088    public Exchange createExchange(ExchangePattern pattern) {
089        return delegate.createExchange(pattern);
090    }
091
092    @Deprecated
093    public Exchange createExchange(Exchange exchange) {
094        return delegate.createExchange(exchange);
095    }
096
097    public CamelContext getCamelContext() {
098        return delegate.getCamelContext();
099    }
100
101    public Producer createProducer() throws Exception {
102        Producer producer = delegate.createProducer();
103        if (producer instanceof ServicePoolAware) {
104            return new InterceptSendToEndpointServicePoolProcessor(this, delegate, producer, skip);
105        } else {
106            return new InterceptSendToEndpointProcessor(this, delegate, producer, skip);
107        }
108    }
109
110    public Consumer createConsumer(Processor processor) throws Exception {
111        return delegate.createConsumer(processor);
112    }
113
114    public PollingConsumer createPollingConsumer() throws Exception {
115        return delegate.createPollingConsumer();
116    }
117
118    public void configureProperties(Map<String, Object> options) {
119        delegate.configureProperties(options);
120    }
121
122    public void setCamelContext(CamelContext context) {
123        delegate.setCamelContext(context);
124    }
125
126    public boolean isLenientProperties() {
127        return delegate.isLenientProperties();
128    }
129
130    public boolean isSingleton() {
131        return delegate.isSingleton();
132    }
133
134    public void start() throws Exception {
135        ServiceHelper.startServices(detour, delegate);
136    }
137
138    public void stop() throws Exception {
139        ServiceHelper.stopServices(delegate, detour);
140    }
141
142    @Override
143    public void shutdown() throws Exception {
144        ServiceHelper.stopAndShutdownServices(delegate, detour);
145    }
146
147    @Override
148    public String toString() {
149        return delegate.toString();
150    }
151}