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.processor;
018
019 import org.apache.camel.AsyncCallback;
020 import org.apache.camel.Exchange;
021 import org.apache.camel.Processor;
022 import org.apache.camel.spi.RouteContext;
023 import org.apache.camel.spi.UnitOfWork;
024
025 /**
026 * This processor tracks the current {@link RouteContext} while processing the {@link Exchange}.
027 * This ensures that the {@link Exchange} have details under which route its being currently processed.
028 */
029 public class RouteContextProcessor extends DelegateAsyncProcessor {
030
031 private final RouteContext routeContext;
032
033 public RouteContextProcessor(RouteContext routeContext, Processor processor) {
034 super(processor);
035 this.routeContext = routeContext;
036 }
037
038 @Override
039 protected boolean processNext(final Exchange exchange, final AsyncCallback callback) {
040 // push the current route context
041 if (exchange.getUnitOfWork() != null) {
042 exchange.getUnitOfWork().pushRouteContext(routeContext);
043 }
044
045 boolean sync = processor.process(exchange, new AsyncCallback() {
046 public void done(boolean doneSync) {
047 try {
048 UnitOfWork uow = exchange.getUnitOfWork();
049 // pop the route context we just used
050 if (uow != null) {
051 uow.popRouteContext();
052 }
053 } catch (Exception e) {
054 exchange.setException(e);
055 } finally {
056 callback.done(doneSync);
057 }
058 }
059 });
060
061 return sync;
062 }
063
064 @Override
065 public String toString() {
066 return "RouteContextProcessor[" + processor + "]";
067 }
068
069 }