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.directvm;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Processor;
021    import org.apache.camel.processor.DelegateProcessor;
022    import org.apache.camel.util.ExchangeHelper;
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    
026    /**
027    *
028    */
029    public final class DirectVmProcessor extends DelegateProcessor {
030    
031        private static final transient Logger LOG = LoggerFactory.getLogger(DirectVmProcessor.class);
032        private final DirectVmEndpoint endpoint;
033    
034        public DirectVmProcessor(Processor processor, DirectVmEndpoint endpoint) {
035            super(processor);
036            this.endpoint = endpoint;
037        }
038    
039        @Override
040        public void process(Exchange exchange) throws Exception {
041            // need to use a copy of the incoming exchange, so we route using this camel context
042            Exchange copy = prepareExchange(exchange);
043    
044            ClassLoader current = Thread.currentThread().getContextClassLoader();
045            boolean changed = false;
046            try {
047                // set TCCL to application context class loader if given
048                ClassLoader appClassLoader = endpoint.getCamelContext().getApplicationContextClassLoader();
049                if (appClassLoader != null) {
050                    LOG.trace("Setting Thread ContextClassLoader to {}", appClassLoader);
051                    Thread.currentThread().setContextClassLoader(appClassLoader);
052                    changed = true;
053                }
054                getProcessor().process(copy);
055            } finally {
056                // make sure to copy results back
057                ExchangeHelper.copyResults(exchange, copy);
058                // restore TCCL if it was changed during processing
059                if (changed) {
060                    LOG.trace("Restoring Thread ContextClassLoader to {}", current);
061                    Thread.currentThread().setContextClassLoader(current);
062                }
063            }
064        }
065    
066        /**
067         * Strategy to prepare exchange for being processed by this consumer
068         *
069         * @param exchange the exchange
070         * @return the exchange to process by this consumer.
071         */
072        protected Exchange prepareExchange(Exchange exchange) {
073            // send a new copied exchange with new camel context (do not handover completions)
074            Exchange newExchange = ExchangeHelper.copyExchangeAndSetCamelContext(exchange, endpoint.getCamelContext(), false);
075            // set the from endpoint
076            newExchange.setFromEndpoint(endpoint);
077            return newExchange;
078        }
079    
080        @Override
081        public String toString() {
082            return "DirectVm[" + processor + "]";
083        }
084    }