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.Processor; 022import org.apache.camel.processor.DelegateAsyncProcessor; 023import org.apache.camel.util.ExchangeHelper; 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026 027/** 028* 029*/ 030public final class DirectVmProcessor extends DelegateAsyncProcessor { 031 032 private static final Logger LOG = LoggerFactory.getLogger(DirectVmProcessor.class); 033 private final DirectVmEndpoint endpoint; 034 035 public DirectVmProcessor(Processor processor, DirectVmEndpoint endpoint) { 036 super(processor); 037 this.endpoint = endpoint; 038 } 039 040 @Override 041 public boolean process(final Exchange exchange, final AsyncCallback callback) { 042 // need to use a copy of the incoming exchange, so we route using this camel context 043 final Exchange copy = prepareExchange(exchange); 044 045 ClassLoader current = Thread.currentThread().getContextClassLoader(); 046 boolean changed = false; 047 try { 048 // set TCCL to application context class loader if given 049 ClassLoader appClassLoader = endpoint.getCamelContext().getApplicationContextClassLoader(); 050 if (appClassLoader != null) { 051 LOG.trace("Setting Thread ContextClassLoader to {}", appClassLoader); 052 Thread.currentThread().setContextClassLoader(appClassLoader); 053 changed = true; 054 } 055 056 final boolean chgd = changed; 057 return processor.process(copy, new AsyncCallback() { 058 @Override 059 public void done(boolean done) { 060 try { 061 // restore TCCL if it was changed during processing 062 if (chgd) { 063 LOG.trace("Restoring Thread ContextClassLoader to {}", current); 064 Thread.currentThread().setContextClassLoader(current); 065 } 066 // make sure to copy results back 067 ExchangeHelper.copyResults(exchange, copy); 068 } finally { 069 // must call callback when we are done 070 callback.done(done); 071 } 072 } 073 }); 074 } finally { 075 // restore TCCL if it was changed during processing 076 if (changed) { 077 LOG.trace("Restoring Thread ContextClassLoader to {}", current); 078 Thread.currentThread().setContextClassLoader(current); 079 } 080 } 081 } 082 083 /** 084 * Strategy to prepare exchange for being processed by this consumer 085 * 086 * @param exchange the exchange 087 * @return the exchange to process by this consumer. 088 */ 089 protected Exchange prepareExchange(Exchange exchange) { 090 // send a new copied exchange with new camel context (do not handover completions) 091 Exchange newExchange = ExchangeHelper.copyExchangeAndSetCamelContext(exchange, endpoint.getCamelContext(), false); 092 // set the from endpoint 093 newExchange.setFromEndpoint(endpoint); 094 // The StreamCache created by the child routes must not be 095 // closed by the unit of work of the child route, but by the unit of 096 // work of the parent route or grand parent route or grand grand parent route ...(in case of nesting). 097 // Set therefore the unit of work of the parent route as stream cache unit of work, 098 // if it is not already set. 099 if (newExchange.getProperty(Exchange.STREAM_CACHE_UNIT_OF_WORK) == null) { 100 newExchange.setProperty(Exchange.STREAM_CACHE_UNIT_OF_WORK, exchange.getUnitOfWork()); 101 } 102 return newExchange; 103 } 104 105 @Override 106 public String toString() { 107 return "DirectVm[" + processor + "]"; 108 } 109}