001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.camel.builder; 019 020 import java.util.ArrayList; 021 import java.util.List; 022 023 import org.apache.camel.processor.DelegateProcessor; 024 import org.apache.camel.Processor; 025 import org.apache.camel.RuntimeCamelException; 026 027 /** 028 * @version $Revision: 519943 $ 029 */ 030 public class InterceptorBuilder implements ProcessorFactory { 031 private final List<DelegateProcessor> intercepts = new ArrayList<DelegateProcessor>(); 032 private final FromBuilder parent; 033 private FromBuilder target; 034 035 public InterceptorBuilder(FromBuilder parent) { 036 this.parent = parent; 037 } 038 039 @Fluent("interceptor") 040 public InterceptorBuilder add(@FluentArg("ref") DelegateProcessor interceptor) { 041 intercepts.add(interceptor); 042 return this; 043 } 044 045 @Fluent(callOnElementEnd=true) 046 public FromBuilder target() { 047 this.target = new FromBuilder(parent); 048 return target; 049 } 050 051 public Processor createProcessor() throws Exception { 052 053 // The target is required. 054 if( target == null ) 055 throw new RuntimeCamelException("target provided."); 056 057 // Interceptors are optional 058 DelegateProcessor first=null; 059 DelegateProcessor last=null; 060 for (DelegateProcessor p : intercepts) { 061 if( first == null ) { 062 first = p; 063 } 064 if( last != null ) { 065 last.setProcessor(p); 066 } 067 last = p; 068 } 069 070 Processor p = target.createProcessor(); 071 if( last != null ) { 072 last.setProcessor(p); 073 } 074 return first == null ? p : first; 075 } 076 }