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 * The binding component is used for as a of wrapping an Endpoint in a contract with a data format.
040 *
041 * In Camel terms a binding is a way of wrapping an Endpoint in a contract; such as a Data Format,
042 * a Content Enricher or validation step. Bindings are completely optional and you can choose to use
043 * them on any camel endpoint.
044 * Bindings are inspired by the work of SwitchYard project adding service contracts to various technologies
045 * like Camel and many others. But rather than the SwitchYard approach of wrapping Camel in SCA,
046 * Camel Bindings provide a way of wrapping Camel endpoints with contracts inside the Camel framework itself;
047 * so you can use them easily inside any Camel route.
048 *
049 * Applies a {@link org.apache.camel.spi.Binding} to an underlying {@link Endpoint} so that the binding processes messages
050 * before its sent to the endpoint and processes messages received by the endpoint consumer before its passed
051 * to the real consumer.
052 *
053 * @deprecated use {@link org.apache.camel.spi.Contract} instead
054 */
055@Deprecated @Metadata(deprecationNode = "Use org.apache.camel.spi.Contract instead")
056@UriEndpoint(firstVersion = "2.11.0", scheme = "binding", title = "Binding", syntax = "binding:bindingName:delegateUri",
057    consumerClass = BindingConsumerProcessor.class, label = "core,transformation")
058public class BindingEndpoint extends DefaultEndpoint implements HasBinding {
059
060    @UriPath @Metadata(required = "true")
061    private final String bindingName;
062    @UriPath @Metadata(required = "true")
063    private final String delegateUri;
064    private Binding binding;
065    private Endpoint delegate;
066
067    @Deprecated
068    public BindingEndpoint(String uri, Component component, Binding binding, Endpoint delegate) {
069        super(uri, component);
070        this.binding = binding;
071        this.delegate = delegate;
072        this.bindingName = null;
073        this.delegateUri = null;
074    }
075
076    public BindingEndpoint(String uri, Component component, String bindingName, String delegateUri) {
077        super(uri, component);
078        this.bindingName = bindingName;
079        this.delegateUri = delegateUri;
080    }
081
082    @Override
083    public Producer createProducer() throws Exception {
084        return new BindingProducer(this);
085    }
086
087    @Override
088    public Consumer createConsumer(Processor processor) throws Exception {
089        Processor bindingProcessor = new BindingConsumerProcessor(this, processor);
090        return delegate.createConsumer(bindingProcessor);
091    }
092
093    @Override
094    public boolean isSingleton() {
095        return true;
096    }
097
098    @Override
099    public Binding getBinding() {
100        return binding;
101    }
102
103    public Endpoint getDelegate() {
104        return delegate;
105    }
106
107    /**
108     * Name of the binding to lookup in the Camel registry.
109     */
110    public String getBindingName() {
111        return bindingName;
112    }
113
114    /**
115     * Uri of the delegate endpoint.
116     */
117    public String getDelegateUri() {
118        return delegateUri;
119    }
120
121    /**
122     * Applies the {@link Binding} processor to the given exchange before passing it on to the delegateProcessor (either a producer or consumer)
123     */
124    public void pipelineBindingProcessor(Processor bindingProcessor, Exchange exchange, Processor delegateProcessor) throws Exception {
125        bindingProcessor.process(exchange);
126
127        Exchange delegateExchange = PipelineHelper.createNextExchange(exchange);
128        delegateProcessor.process(delegateExchange);
129    }
130
131    @Override
132    protected void doStart() throws Exception {
133        if (binding == null) {
134            binding = CamelContextHelper.mandatoryLookup(getCamelContext(), bindingName, Binding.class);
135        }
136        if (delegate == null) {
137            delegate = getMandatoryEndpoint(getCamelContext(), delegateUri);
138        }
139
140        // inject CamelContext
141        if (binding instanceof CamelContextAware) {
142            ((CamelContextAware) binding).setCamelContext(getCamelContext());
143        }
144        ServiceHelper.startServices(delegate, binding);
145        super.doStart();
146    }
147
148    @Override
149    protected void doStop() throws Exception {
150        ServiceHelper.stopServices(delegate, binding);
151        super.doStop();
152    }
153}