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.Route;
023    import org.apache.camel.spi.InflightRepository;
024    import org.apache.camel.spi.RouteContext;
025    
026    /**
027     * This processor tracks the current {@link RouteContext} while processing the {@link Exchange}.
028     * This ensures that the {@link Exchange} have details under which route its being currently processed.
029     */
030    public class RouteInflightRepositoryProcessor extends DelegateAsyncProcessor {
031        
032        private final InflightRepository inflightRepository;
033        private String id;
034    
035        public RouteInflightRepositoryProcessor(InflightRepository inflightRepository, Processor processor) {
036            super(processor);
037            this.inflightRepository = inflightRepository;
038        }
039    
040        public void setRoute(Route route) {
041            this.id = route.getId();
042        }
043    
044        @Override
045        protected boolean processNext(final Exchange exchange, final AsyncCallback callback) {
046            inflightRepository.add(exchange, id);
047            
048            boolean sync = processor.process(exchange, new AsyncCallback() {
049                public void done(boolean doneSync) {
050                    try {
051                        inflightRepository.remove(exchange, id);
052                    } finally {
053                        callback.done(doneSync);
054                    }
055                }
056            });
057            return sync;
058        }
059    
060        @Override
061        public String toString() {
062            return super.toString();
063        }
064    }