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.spi.Metadata;
031import org.apache.camel.spi.UriEndpoint;
032import org.apache.camel.spi.UriPath;
033import org.apache.camel.util.CamelContextHelper;
034import org.apache.camel.util.ServiceHelper;
035
036import static org.apache.camel.util.CamelContextHelper.getMandatoryEndpoint;
037
038/**
039 * Applies a {@link org.apache.camel.spi.Binding} to an underlying {@link Endpoint} so that the binding processes messages
040 * before its sent to the endpoint and processes messages received by the endpoint consumer before its passed
041 * to the real consumer.
042 */
043@UriEndpoint(scheme = "binding", syntax = "binding:bindingName:delegateUri", consumerClass = BindingConsumerProcessor.class, label = "core,transformation")
044public class BindingEndpoint extends DefaultEndpoint implements HasBinding {
045
046    @UriPath @Metadata(required = "true")
047    private final String bindingName;
048    @UriPath @Metadata(required = "true")
049    private final String delegateUri;
050    private Binding binding;
051    private Endpoint delegate;
052
053    @Deprecated
054    public BindingEndpoint(String uri, Component component, Binding binding, Endpoint delegate) {
055        super(uri, component);
056        this.binding = binding;
057        this.delegate = delegate;
058        this.bindingName = null;
059        this.delegateUri = null;
060    }
061
062    public BindingEndpoint(String uri, Component component, String bindingName, String delegateUri) {
063        super(uri, component);
064        this.bindingName = bindingName;
065        this.delegateUri = delegateUri;
066    }
067
068    @Override
069    public Producer createProducer() throws Exception {
070        return new BindingProducer(this);
071    }
072
073    @Override
074    public Consumer createConsumer(Processor processor) throws Exception {
075        Processor bindingProcessor = new BindingConsumerProcessor(this, processor);
076        return delegate.createConsumer(bindingProcessor);
077    }
078
079    @Override
080    public boolean isSingleton() {
081        return true;
082    }
083
084    @Override
085    public Binding getBinding() {
086        return binding;
087    }
088
089    public Endpoint getDelegate() {
090        return delegate;
091    }
092
093    /**
094     * Name of the binding to lookup in the Camel registry.
095     */
096    public String getBindingName() {
097        return bindingName;
098    }
099
100    /**
101     * Uri of the delegate endpoint.
102     */
103    public String getDelegateUri() {
104        return delegateUri;
105    }
106
107    /**
108     * Applies the {@link Binding} processor to the given exchange before passing it on to the delegateProcessor (either a producer or consumer)
109     */
110    public void pipelineBindingProcessor(Processor bindingProcessor, Exchange exchange, Processor delegateProcessor) throws Exception {
111        bindingProcessor.process(exchange);
112
113        Exchange delegateExchange = PipelineHelper.createNextExchange(exchange);
114        delegateProcessor.process(delegateExchange);
115    }
116
117    @Override
118    protected void doStart() throws Exception {
119        if (binding == null) {
120            binding = CamelContextHelper.mandatoryLookup(getCamelContext(), bindingName, Binding.class);
121        }
122        if (delegate == null) {
123            delegate = getMandatoryEndpoint(getCamelContext(), delegateUri);
124        }
125
126        // inject CamelContext
127        if (binding instanceof CamelContextAware) {
128            ((CamelContextAware) binding).setCamelContext(getCamelContext());
129        }
130        ServiceHelper.startServices(delegate, binding);
131        super.doStart();
132    }
133
134    @Override
135    protected void doStop() throws Exception {
136        ServiceHelper.stopServices(delegate, binding);
137        super.doStop();
138    }
139}