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            return processor.process(copy, new AsyncCallback() {
056                @Override
057                public void done(boolean done) {
058                    try {
059                        // make sure to copy results back
060                        ExchangeHelper.copyResults(exchange, copy);
061                    } finally {
062                        // must call callback when we are done
063                        callback.done(done);
064                    }
065                }
066            });
067        } finally {
068            // restore TCCL if it was changed during processing
069            if (changed) {
070                LOG.trace("Restoring Thread ContextClassLoader to {}", current);
071                Thread.currentThread().setContextClassLoader(current);
072            }
073        }
074    }
075
076    /**
077     * Strategy to prepare exchange for being processed by this consumer
078     *
079     * @param exchange the exchange
080     * @return the exchange to process by this consumer.
081     */
082    protected Exchange prepareExchange(Exchange exchange) {
083        // send a new copied exchange with new camel context (do not handover completions)
084        Exchange newExchange = ExchangeHelper.copyExchangeAndSetCamelContext(exchange, endpoint.getCamelContext(), false);
085        // set the from endpoint
086        newExchange.setFromEndpoint(endpoint);
087        return newExchange;
088    }
089
090    @Override
091    public String toString() {
092        return "DirectVm[" + processor + "]";
093    }
094}