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.builder; 018 019import org.apache.camel.model.PipelineDefinition; 020import org.apache.camel.model.ProcessorDefinition; 021import org.apache.camel.model.RouteDefinition; 022 023/** 024 * A builder when using the <a href="http://camel.apache.org/advicewith.html">advice with</a> feature. 025 */ 026public class AdviceWithBuilder<T extends ProcessorDefinition<?>> { 027 028 private final AdviceWithRouteBuilder builder; 029 private final String id; 030 private final String toString; 031 private final Class<T> type; 032 private boolean selectFirst; 033 private boolean selectLast; 034 private int selectFrom = -1; 035 private int selectTo = -1; 036 private int maxDeep = -1; 037 038 public AdviceWithBuilder(AdviceWithRouteBuilder builder, String id, String toString, Class<T> type) { 039 this.builder = builder; 040 this.id = id; 041 this.toString = toString; 042 this.type = type; 043 044 if (id == null && toString == null && type == null) { 045 throw new IllegalArgumentException("Either id, toString or type must be specified"); 046 } 047 } 048 049 /** 050 * Will only apply the first node matched. 051 * 052 * @return the builder to build the nodes. 053 */ 054 public AdviceWithBuilder<T> selectFirst() { 055 selectFirst = true; 056 selectLast = false; 057 return this; 058 } 059 060 /** 061 * Will only apply the last node matched. 062 * 063 * @return the builder to build the nodes. 064 */ 065 public AdviceWithBuilder<T> selectLast() { 066 selectLast = true; 067 selectFirst = false; 068 return this; 069 } 070 071 /** 072 * Will only apply the n'th node matched. 073 * 074 * @param index index of node to match (is 0-based) 075 * @return the builder to build the nodes. 076 */ 077 public AdviceWithBuilder<T> selectIndex(int index) { 078 if (index < 0) { 079 throw new IllegalArgumentException("Index must be a non negative number, was: " + index); 080 } 081 selectFrom = index; 082 selectTo = index; 083 return this; 084 } 085 086 /** 087 * Will only apply the node in the index range matched. 088 * 089 * @param from from index of node to start matching (inclusive) 090 * @param to to index of node to stop matching (inclusive) 091 * @return the builder to build the nodes. 092 */ 093 public AdviceWithBuilder<T> selectRange(int from, int to) { 094 if (from < 0) { 095 throw new IllegalArgumentException("From must be a non negative number, was: " + from); 096 } 097 if (from > to) { 098 throw new IllegalArgumentException("From must be equal or lower than to. from: " + from + ", to: " + to); 099 } 100 selectFrom = from; 101 selectTo = to; 102 return this; 103 } 104 105 /** 106 * Will only apply for nodes maximum levels deep. 107 * <p/> 108 * The first level is <tt>1</tt>, and level <tt>2</tt> is the children of the the first level nodes, and so on. 109 * <p/> 110 * Use zero or negative value for unbounded level. 111 * 112 * @param maxDeep the maximum levels to traverse deep in the Camel route tree. 113 * @return the builder to build the nodes. 114 */ 115 public AdviceWithBuilder<T> maxDeep(int maxDeep) { 116 if (maxDeep == 0) { 117 // disable it 118 this.maxDeep = -1; 119 } 120 this.maxDeep = maxDeep; 121 return this; 122 } 123 124 /** 125 * Replaces the matched node(s) with the following nodes. 126 * 127 * @return the builder to build the nodes. 128 */ 129 public ProcessorDefinition<?> replace() { 130 RouteDefinition route = builder.getOriginalRoute(); 131 PipelineDefinition answer = new PipelineDefinition(); 132 if (id != null) { 133 builder.getAdviceWithTasks().add(AdviceWithTasks.replaceById(route, id, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); 134 } else if (toString != null) { 135 builder.getAdviceWithTasks().add(AdviceWithTasks.replaceByToString(route, toString, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); 136 } else if (type != null) { 137 builder.getAdviceWithTasks().add(AdviceWithTasks.replaceByType(route, type, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); 138 } 139 return answer; 140 } 141 142 /** 143 * Removes the matched node(s) 144 */ 145 public void remove() { 146 RouteDefinition route = builder.getOriginalRoute(); 147 if (id != null) { 148 builder.getAdviceWithTasks().add(AdviceWithTasks.removeById(route, id, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); 149 } else if (toString != null) { 150 builder.getAdviceWithTasks().add(AdviceWithTasks.removeByToString(route, toString, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); 151 } else if (type != null) { 152 builder.getAdviceWithTasks().add(AdviceWithTasks.removeByType(route, type, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); 153 } 154 } 155 156 /** 157 * Insert the following node(s) <b>before</b> the matched node(s) 158 * 159 * @return the builder to build the nodes. 160 */ 161 public ProcessorDefinition<?> before() { 162 RouteDefinition route = builder.getOriginalRoute(); 163 PipelineDefinition answer = new PipelineDefinition(); 164 if (id != null) { 165 builder.getAdviceWithTasks().add(AdviceWithTasks.beforeById(route, id, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); 166 } else if (toString != null) { 167 builder.getAdviceWithTasks().add(AdviceWithTasks.beforeByToString(route, toString, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); 168 } else if (type != null) { 169 builder.getAdviceWithTasks().add(AdviceWithTasks.beforeByType(route, type, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); 170 } 171 return answer; 172 } 173 174 /** 175 * Insert the following node(s) <b>after</b> the matched node(s) 176 * 177 * @return the builder to build the nodes. 178 */ 179 public ProcessorDefinition<?> after() { 180 RouteDefinition route = builder.getOriginalRoute(); 181 PipelineDefinition answer = new PipelineDefinition(); 182 if (id != null) { 183 builder.getAdviceWithTasks().add(AdviceWithTasks.afterById(route, id, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); 184 } else if (toString != null) { 185 builder.getAdviceWithTasks().add(AdviceWithTasks.afterByToString(route, toString, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); 186 } else if (type != null) { 187 builder.getAdviceWithTasks().add(AdviceWithTasks.afterByType(route, type, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); 188 } 189 return answer; 190 } 191 192}