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