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.component.binding;
018
019import org.apache.camel.CamelContextAware;
020import org.apache.camel.Component;
021import org.apache.camel.Consumer;
022import org.apache.camel.Endpoint;
023import org.apache.camel.Exchange;
024import org.apache.camel.Processor;
025import org.apache.camel.Producer;
026import org.apache.camel.impl.DefaultEndpoint;
027import org.apache.camel.processor.PipelineHelper;
028import org.apache.camel.spi.Binding;
029import org.apache.camel.spi.HasBinding;
030import org.apache.camel.util.ServiceHelper;
031
032/**
033 * Applies a {@link org.apache.camel.spi.Binding} to an underlying {@link Endpoint} so that the binding processes messages
034 * before its sent to the endpoint and processes messages received by the endpoint consumer before its passed
035 * to the real consumer.
036 */
037public class BindingEndpoint extends DefaultEndpoint implements HasBinding {
038    private final Binding binding;
039    private final Endpoint delegate;
040
041    public BindingEndpoint(String uri, Component component, Binding binding, Endpoint delegate) {
042        super(uri, component);
043        this.binding = binding;
044        this.delegate = delegate;
045    }
046
047    @Override
048    public Producer createProducer() throws Exception {
049        return new BindingProducer(this);
050    }
051
052    @Override
053    public Consumer createConsumer(Processor processor) throws Exception {
054        Processor bindingProcessor = new BindingConsumerProcessor(this, processor);
055        return delegate.createConsumer(bindingProcessor);
056    }
057
058    @Override
059    public boolean isSingleton() {
060        return true;
061    }
062
063    @Override
064    public Binding getBinding() {
065        return binding;
066    }
067
068    public Endpoint getDelegate() {
069        return delegate;
070    }
071
072    /**
073     * Applies the {@link Binding} processor to the given exchange before passing it on to the delegateProcessor (either a producer or consumer)
074     */
075    public void pipelineBindingProcessor(Processor bindingProcessor, Exchange exchange, Processor delegateProcessor) throws Exception {
076        bindingProcessor.process(exchange);
077
078        Exchange delegateExchange = PipelineHelper.createNextExchange(exchange);
079        delegateProcessor.process(delegateExchange);
080    }
081
082    @Override
083    protected void doStart() throws Exception {
084        // inject CamelContext
085        if (binding instanceof CamelContextAware) {
086            ((CamelContextAware) binding).setCamelContext(getCamelContext());
087        }
088        ServiceHelper.startServices(delegate, binding);
089        super.doStart();
090    }
091
092    @Override
093    protected void doStop() throws Exception {
094        ServiceHelper.stopServices(delegate, binding);
095        super.doStop();
096    }
097}