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.component.binding;
018    
019    import org.apache.camel.CamelContextAware;
020    import org.apache.camel.Endpoint;
021    import org.apache.camel.Exchange;
022    import org.apache.camel.Processor;
023    import org.apache.camel.Producer;
024    import org.apache.camel.impl.DefaultProducer;
025    import org.apache.camel.util.ServiceHelper;
026    
027    /**
028     * A {@link Producer} which applies a {@link org.apache.camel.spi.Binding} before invoking the underlying {@link Producer} on the {@link Endpoint}
029     */
030    public class BindingProducer extends DefaultProducer {
031        private final BindingEndpoint endpoint;
032        private final Processor bindingProcessor;
033        private final Producer delegateProducer;
034    
035        public BindingProducer(BindingEndpoint endpoint) throws Exception {
036            super(endpoint);
037            this.endpoint = endpoint;
038            this.bindingProcessor = endpoint.getBinding().createProduceProcessor();
039            this.delegateProducer = endpoint.getDelegate().createProducer();
040        }
041    
042        @Override
043        public void process(Exchange exchange) throws Exception {
044            endpoint.pipelineBindingProcessor(bindingProcessor, exchange, delegateProducer);
045        }
046    
047        @Override
048        protected void doStart() throws Exception {
049            // inject CamelContext
050            if (bindingProcessor instanceof CamelContextAware) {
051                ((CamelContextAware) bindingProcessor).setCamelContext(getEndpoint().getCamelContext());
052            }
053            if (delegateProducer instanceof CamelContextAware) {
054                ((CamelContextAware) delegateProducer).setCamelContext(getEndpoint().getCamelContext());
055            }
056            ServiceHelper.startServices(bindingProcessor, delegateProducer);
057            super.doStart();
058        }
059    
060        @Override
061        protected void doStop() throws Exception {
062            ServiceHelper.stopServices(delegateProducer, bindingProcessor);
063            super.doStop();
064        }
065    }