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.reifier;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.NamedNode;
021import org.apache.camel.Processor;
022import org.apache.camel.model.InterceptDefinition;
023import org.apache.camel.model.ProcessorDefinition;
024import org.apache.camel.model.RouteDefinition;
025import org.apache.camel.processor.Pipeline;
026import org.apache.camel.spi.InterceptStrategy;
027import org.apache.camel.spi.RouteContext;
028
029public class InterceptReifier<T extends InterceptDefinition> extends ProcessorReifier<T> {
030
031    public InterceptReifier(ProcessorDefinition<?> definition) {
032        super((T)definition);
033    }
034
035    @Override
036    public Processor createProcessor(final RouteContext routeContext) throws Exception {
037        // create the output processor
038        Processor output = this.createChildProcessor(routeContext, true);
039
040        // add the output as a intercept strategy to the route context so its
041        // invoked on each processing step
042        routeContext.getInterceptStrategies().add(new InterceptStrategy() {
043            private Processor interceptedTarget;
044
045            public Processor wrapProcessorInInterceptors(CamelContext context, NamedNode definition, Processor target, Processor nextTarget) throws Exception {
046                // store the target we are intercepting
047                this.interceptedTarget = target;
048
049                // remember the target that was intercepted
050                InterceptReifier.this.definition.getIntercepted().add(interceptedTarget);
051
052                if (interceptedTarget != null) {
053                    // wrap in a pipeline so we continue routing to the next
054                    return Pipeline.newInstance(context, output, interceptedTarget);
055                } else {
056                    return output;
057                }
058            }
059
060            @Override
061            public String toString() {
062                return "intercept[" + (interceptedTarget != null ? interceptedTarget : output) + "]";
063            }
064        });
065
066        // remove me from the route so I am not invoked in a regular route path
067        ((RouteDefinition)routeContext.getRoute()).getOutputs().remove(this);
068        // and return no processor to invoke next from me
069        return null;
070    }
071
072}