org.apache.camel.builder.AdviceWithBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of camel-core Show documentation
Show all versions of camel-core Show documentation
The Core Camel Java DSL based router
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.builder;
import org.apache.camel.model.PipelineDefinition;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.RouteDefinition;
/**
* A builder when using the advice with feature.
*/
public class AdviceWithBuilder> {
private final AdviceWithRouteBuilder builder;
private final String id;
private final String toString;
private final Class type;
private boolean selectFirst;
private boolean selectLast;
private int selectFrom = -1;
private int selectTo = -1;
private int maxDeep = -1;
public AdviceWithBuilder(AdviceWithRouteBuilder builder, String id, String toString, Class type) {
this.builder = builder;
this.id = id;
this.toString = toString;
this.type = type;
if (id == null && toString == null && type == null) {
throw new IllegalArgumentException("Either id, toString or type must be specified");
}
}
/**
* Will only apply the first node matched.
*
* @return the builder to build the nodes.
*/
public AdviceWithBuilder selectFirst() {
selectFirst = true;
selectLast = false;
return this;
}
/**
* Will only apply the last node matched.
*
* @return the builder to build the nodes.
*/
public AdviceWithBuilder selectLast() {
selectLast = true;
selectFirst = false;
return this;
}
/**
* Will only apply the n'th node matched.
*
* @param index index of node to match (is 0-based)
* @return the builder to build the nodes.
*/
public AdviceWithBuilder selectIndex(int index) {
if (index < 0) {
throw new IllegalArgumentException("Index must be a non negative number, was: " + index);
}
selectFrom = index;
selectTo = index;
return this;
}
/**
* Will only apply the node in the index range matched.
*
* @param from from index of node to start matching (inclusive)
* @param to to index of node to stop matching (inclusive)
* @return the builder to build the nodes.
*/
public AdviceWithBuilder selectRange(int from, int to) {
if (from < 0) {
throw new IllegalArgumentException("From must be a non negative number, was: " + from);
}
if (from > to) {
throw new IllegalArgumentException("From must be equal or lower than to. from: " + from + ", to: " + to);
}
selectFrom = from;
selectTo = to;
return this;
}
/**
* Will only apply for nodes maximum levels deep.
*
* The first level is 1, and level 2 is the children of the the first level nodes, and so on.
*
* Use zero or negative value for unbounded level.
*
* @param maxDeep the maximum levels to traverse deep in the Camel route tree.
* @return the builder to build the nodes.
*/
public AdviceWithBuilder maxDeep(int maxDeep) {
if (maxDeep == 0) {
// disable it
this.maxDeep = -1;
}
this.maxDeep = maxDeep;
return this;
}
/**
* Replaces the matched node(s) with the following nodes.
*
* @return the builder to build the nodes.
*/
public ProcessorDefinition replace() {
RouteDefinition route = builder.getOriginalRoute();
PipelineDefinition answer = new PipelineDefinition();
if (id != null) {
builder.getAdviceWithTasks().add(AdviceWithTasks.replaceById(route, id, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
} else if (toString != null) {
builder.getAdviceWithTasks().add(AdviceWithTasks.replaceByToString(route, toString, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
} else if (type != null) {
builder.getAdviceWithTasks().add(AdviceWithTasks.replaceByType(route, type, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
}
return answer;
}
/**
* Removes the matched node(s)
*/
public void remove() {
RouteDefinition route = builder.getOriginalRoute();
if (id != null) {
builder.getAdviceWithTasks().add(AdviceWithTasks.removeById(route, id, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
} else if (toString != null) {
builder.getAdviceWithTasks().add(AdviceWithTasks.removeByToString(route, toString, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
} else if (type != null) {
builder.getAdviceWithTasks().add(AdviceWithTasks.removeByType(route, type, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
}
}
/**
* Insert the following node(s) before the matched node(s)
*
* @return the builder to build the nodes.
*/
public ProcessorDefinition before() {
RouteDefinition route = builder.getOriginalRoute();
PipelineDefinition answer = new PipelineDefinition();
if (id != null) {
builder.getAdviceWithTasks().add(AdviceWithTasks.beforeById(route, id, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
} else if (toString != null) {
builder.getAdviceWithTasks().add(AdviceWithTasks.beforeByToString(route, toString, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
} else if (type != null) {
builder.getAdviceWithTasks().add(AdviceWithTasks.beforeByType(route, type, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
}
return answer;
}
/**
* Insert the following node(s) after the matched node(s)
*
* @return the builder to build the nodes.
*/
public ProcessorDefinition after() {
RouteDefinition route = builder.getOriginalRoute();
PipelineDefinition answer = new PipelineDefinition();
if (id != null) {
builder.getAdviceWithTasks().add(AdviceWithTasks.afterById(route, id, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
} else if (toString != null) {
builder.getAdviceWithTasks().add(AdviceWithTasks.afterByToString(route, toString, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
} else if (type != null) {
builder.getAdviceWithTasks().add(AdviceWithTasks.afterByType(route, type, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
}
return answer;
}
}