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