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.CamelContext;
020    import org.apache.camel.Exchange;
021    import org.apache.camel.Processor;
022    import org.apache.camel.Route;
023    import org.apache.camel.impl.ServiceSupport;
024    import org.apache.camel.impl.SynchronizationAdapter;
025    import org.apache.camel.spi.RoutePolicy;
026    
027    /**
028     * @version $Revision: 831859 $
029     */
030    public class RoutePolicyProcessor extends DelegateProcessor {
031    
032        private final RoutePolicy routePolicy;
033        private Route route;
034    
035        public RoutePolicyProcessor(Processor processor, RoutePolicy routePolicy) {
036            super(processor);
037            this.routePolicy = routePolicy;
038        }
039    
040        @Override
041        public String toString() {
042            return "RoutePolicy[" + routePolicy + "]";
043        }
044    
045        @Override
046        protected void processNext(Exchange exchange) throws Exception {
047            // check whether the policy is enabled
048            if (isRoutePolicyRunAllowed()) {
049    
050                // invoke begin
051                routePolicy.onExchangeBegin(route, exchange);
052    
053                // add on completion that invokes the policy callback on complete
054                // as the Exchange can be routed async and thus we need the callback to
055                // invoke when the route is completed
056                exchange.addOnCompletion(new SynchronizationAdapter() {
057                    @Override
058                    public void onDone(Exchange exchange) {
059                        // do not invoke it if Camel is stopping as we don't want
060                        // the policy to start a consumer during Camel is stopping
061                        if (isCamelStopping(exchange.getContext())) {
062                            return;
063                        }
064                        routePolicy.onExchangeDone(route, exchange);
065                    }
066                });
067            }
068    
069            if (processor != null) {
070                processor.process(exchange);
071            }
072        }
073    
074        public void setRoute(Route route) {
075            this.route = route;
076        }
077    
078        private static boolean isCamelStopping(CamelContext context) {
079            if (context instanceof ServiceSupport) {
080                ServiceSupport ss = (ServiceSupport) context;
081                return ss.isStopping() || ss.isStopped();
082            }
083            return false;
084        }
085    
086        private boolean isRoutePolicyRunAllowed() {
087            if (routePolicy instanceof ServiceSupport) {
088                ServiceSupport ss = (ServiceSupport) routePolicy;
089                return ss.isRunAllowed();
090            }
091            return true;
092        }
093    
094    }