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.directvm;
018
019import org.apache.camel.AsyncCallback;
020import org.apache.camel.Exchange;
021import org.apache.camel.impl.DefaultAsyncProducer;
022import org.apache.camel.spi.HeaderFilterStrategy;
023
024/**
025 * The Direct-VM producer.
026 */
027public class DirectVmProducer extends DefaultAsyncProducer {
028
029    private DirectVmEndpoint endpoint;
030
031    public DirectVmProducer(DirectVmEndpoint endpoint) {
032        super(endpoint);
033        this.endpoint = endpoint;
034    }
035
036    @Override
037    public boolean process(Exchange exchange, AsyncCallback callback) {
038        // send to consumer
039        DirectVmConsumer consumer = endpoint.getComponent().getConsumer(endpoint);
040        
041        if (consumer == null) {
042            if (endpoint.isFailIfNoConsumers()) {
043                exchange.setException(new DirectVmConsumerNotAvailableException("No consumers available on endpoint: " + endpoint, exchange));
044            } else {
045                log.debug("message ignored, no consumers available on endpoint: " + endpoint);
046            }
047            callback.done(true);
048            return true;
049        }
050        
051        final HeaderFilterStrategy headerFilterStrategy = endpoint.getHeaderFilterStrategy();
052
053        // Only clone the Exchange if we actually need to filter out properties or headers.
054        final Exchange submitted = (!endpoint.isPropagateProperties() || headerFilterStrategy != null) ? exchange.copy(true) : exchange;
055        
056        // Clear properties in the copy if we are not propagating them.
057        if (!endpoint.isPropagateProperties()) {
058            submitted.getProperties().clear();
059        }
060        
061        // Filter headers by Header Filter Strategy if there is one set.
062        if (headerFilterStrategy != null) {
063            submitted.getIn().getHeaders().entrySet().removeIf(e -> headerFilterStrategy.applyFilterToCamelHeaders(e.getKey(), e.getValue(), submitted));
064        }
065        
066        return consumer.getAsyncProcessor().process(submitted, done -> {
067            exchange.setException(submitted.getException());
068            exchange.getOut().copyFrom(submitted.hasOut() ? submitted.getOut() : submitted.getIn());
069
070            if (headerFilterStrategy != null) {
071                exchange.getOut().getHeaders().entrySet().removeIf(e -> headerFilterStrategy.applyFilterToExternalHeaders(e.getKey(), e.getValue(), submitted));
072            }
073
074            if (endpoint.isPropagateProperties()) {
075                exchange.getProperties().putAll(submitted.getProperties());
076            }
077
078            callback.done(done);
079        });
080    }
081    
082}