Coverage Report - org.apache.camel.builder.InterceptorBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
InterceptorBuilder
96% 
100% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.builder;
 19  
 
 20  
 import java.util.ArrayList;
 21  
 import java.util.List;
 22  
 
 23  
 import org.apache.camel.Exchange;
 24  
 import org.apache.camel.processor.DelegateProcessor;
 25  
 import org.apache.camel.Processor;
 26  
 import org.apache.camel.RuntimeCamelException;
 27  
 
 28  
 /**
 29  
  * @version $Revision: 519943 $
 30  
  */
 31  
 public class InterceptorBuilder implements ProcessorFactory {
 32  2
     private final List<DelegateProcessor> intercepts = new ArrayList<DelegateProcessor>();
 33  
         private final FromBuilder parent;
 34  
         private FromBuilder target;
 35  
 
 36  2
         public InterceptorBuilder(FromBuilder parent) {
 37  2
         this.parent = parent;
 38  2
         }
 39  
         
 40  
         @Fluent("interceptor")
 41  
         public InterceptorBuilder add(@FluentArg("ref") DelegateProcessor interceptor) {
 42  4
                 intercepts.add(interceptor);
 43  4
                 return this;
 44  
         }
 45  
         
 46  
         @Fluent(callOnElementEnd=true)
 47  
     public FromBuilder target() {
 48  2
         this.target = new FromBuilder(parent);
 49  2
         return target;
 50  
     }
 51  
 
 52  
     public Processor createProcessor() throws Exception {
 53  
             
 54  
             // The target is required.
 55  2
             if( target == null ) 
 56  0
                     throw new RuntimeCamelException("target provided.");
 57  
             
 58  
             // Interceptors are optional
 59  2
             DelegateProcessor first=null;
 60  2
             DelegateProcessor last=null;
 61  2
         for (DelegateProcessor p : intercepts) {
 62  4
             if( first == null ) {
 63  2
                     first = p;
 64  
             }
 65  4
             if( last != null ) {
 66  2
                     last.setNext(p);
 67  
             }
 68  4
             last = p;
 69  4
         }
 70  
         
 71  2
         Processor p = target.createProcessor();
 72  2
         if( last != null ) {
 73  2
                 last.setNext(p);
 74  
         }
 75  2
         return first == null ? p : first;
 76  
     }
 77  
 }