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
024 /**
025 *
026 */
027 public final class DirectVmProcessor extends DelegateProcessor {
028
029 private final DirectVmEndpoint endpoint;
030
031 public DirectVmProcessor(Processor processor, DirectVmEndpoint endpoint) {
032 super(processor);
033 this.endpoint = endpoint;
034 }
035
036 @Override
037 public void process(Exchange exchange) throws Exception {
038 // need to use a copy of the incoming exchange, so we route using this camel context
039 Exchange copy = prepareExchange(exchange);
040 try {
041 getProcessor().process(copy);
042 } finally {
043 // make sure to copy results back
044 ExchangeHelper.copyResults(exchange, copy);
045 }
046 }
047
048 /**
049 * Strategy to prepare exchange for being processed by this consumer
050 *
051 * @param exchange the exchange
052 * @return the exchange to process by this consumer.
053 */
054 protected Exchange prepareExchange(Exchange exchange) {
055 // send a new copied exchange with new camel context (do not handover completions)
056 Exchange newExchange = ExchangeHelper.copyExchangeAndSetCamelContext(exchange, endpoint.getCamelContext(), false);
057 // set the from endpoint
058 newExchange.setFromEndpoint(endpoint);
059 return newExchange;
060 }
061
062 @Override
063 public String toString() {
064 return "DirectVm[" + processor + "]";
065 }
066 }