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     */
017    package org.apache.camel.builder;
018    
019    import org.apache.camel.model.PipelineDefinition;
020    import org.apache.camel.model.ProcessorDefinition;
021    import 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     */
026    public 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    
037        public AdviceWithBuilder(AdviceWithRouteBuilder builder, String id, String toString, Class<T> type) {
038            this.builder = builder;
039            this.id = id;
040            this.toString = toString;
041            this.type = type;
042    
043            if (id == null && toString == null && type == null) {
044                throw new IllegalArgumentException("Either id, toString or type must be specified");
045            }
046        }
047    
048        /**
049         * Will only apply the first node matched.
050         *
051         * @return the builder to build the nodes.
052         */
053        public AdviceWithBuilder<T> selectFirst() {
054            selectFirst = true;
055            selectLast = false;
056            return this;
057        }
058    
059        /**
060         * Will only apply the last node matched.
061         *
062         * @return the builder to build the nodes.
063         */
064        public AdviceWithBuilder<T> selectLast() {
065            selectLast = true;
066            selectFirst = false;
067            return this;
068        }
069    
070        /**
071         * Will only apply the n'th node matched.
072         *
073         * @param index index of node to match (is 0-based)
074         * @return the builder to build the nodes.
075         */
076        public AdviceWithBuilder<T> selectIndex(int index) {
077            if (index < 0) {
078                throw new IllegalArgumentException("Index must be a non negative number, was: " + index);
079            }
080            selectFrom = index;
081            selectTo = index;
082            return this;
083        }
084    
085        /**
086         * Will only apply the node in the index range matched.
087         *
088         * @param from from index of node to start matching (inclusive)
089         * @param to to index of node to stop matching (inclusive)
090         * @return the builder to build the nodes.
091         */
092        public AdviceWithBuilder<T> selectRange(int from, int to) {
093            if (from < 0) {
094                throw new IllegalArgumentException("From must be a non negative number, was: " + from);
095            }
096            if (from > to) {
097                throw new IllegalArgumentException("From must be equal or lower than to. from: " + from + ", to: " + to);
098            }
099            selectFrom = from;
100            selectTo = to;
101            return this;
102        }
103    
104        /**
105         * Replaces the matched node(s) with the following nodes.
106         *
107         * @return the builder to build the nodes.
108         */
109        public ProcessorDefinition<?> replace() {
110            RouteDefinition route = builder.getOriginalRoute();
111            PipelineDefinition answer = new PipelineDefinition();
112            if (id != null) {
113                builder.getAdviceWithTasks().add(AdviceWithTasks.replaceById(route, id, answer, selectFirst, selectLast, selectFrom, selectTo));
114            } else if (toString != null) {
115                builder.getAdviceWithTasks().add(AdviceWithTasks.replaceByToString(route, toString, answer, selectFirst, selectLast, selectFrom, selectTo));
116            } else if (type != null) {
117                builder.getAdviceWithTasks().add(AdviceWithTasks.replaceByType(route, type, answer, selectFirst, selectLast, selectFrom, selectTo));
118            }
119            return answer;
120        }
121    
122        /**
123         * Removes the matched node(s)
124         */
125        public void remove() {
126            RouteDefinition route = builder.getOriginalRoute();
127            if (id != null) {
128                builder.getAdviceWithTasks().add(AdviceWithTasks.removeById(route, id, selectFirst, selectLast, selectFrom, selectTo));
129            } else if (toString != null) {
130                builder.getAdviceWithTasks().add(AdviceWithTasks.removeByToString(route, toString, selectLast, selectFirst, selectFrom, selectTo));
131            } else if (type != null) {
132                builder.getAdviceWithTasks().add(AdviceWithTasks.removeByType(route, type, selectFirst, selectFirst, selectFrom, selectTo));
133            }
134        }
135    
136        /**
137         * Insert the following node(s) <b>before</b> the matched node(s)
138         *
139         * @return the builder to build the nodes.
140         */
141        public ProcessorDefinition<?> before() {
142            RouteDefinition route = builder.getOriginalRoute();
143            PipelineDefinition answer = new PipelineDefinition();
144            if (id != null) {
145                builder.getAdviceWithTasks().add(AdviceWithTasks.beforeById(route, id, answer, selectFirst, selectLast, selectFrom, selectTo));
146            } else if (toString != null) {
147                builder.getAdviceWithTasks().add(AdviceWithTasks.beforeByToString(route, toString, answer, selectLast, selectFirst, selectFrom, selectTo));
148            } else if (type != null) {
149                builder.getAdviceWithTasks().add(AdviceWithTasks.beforeByType(route, type, answer, selectFirst, selectLast, selectFrom, selectTo));
150            }
151            return answer;
152        }
153    
154        /**
155         * Insert the following node(s) <b>after</b> the matched node(s)
156         *
157         * @return the builder to build the nodes.
158         */
159        public ProcessorDefinition<?> after() {
160            RouteDefinition route = builder.getOriginalRoute();
161            PipelineDefinition answer = new PipelineDefinition();
162            if (id != null) {
163                builder.getAdviceWithTasks().add(AdviceWithTasks.afterById(route, id, answer, selectFirst, selectLast, selectFrom, selectTo));
164            } else if (toString != null) {
165                builder.getAdviceWithTasks().add(AdviceWithTasks.afterByToString(route, toString, answer, selectLast, selectFirst, selectFrom, selectTo));
166            } else if (type != null) {
167                builder.getAdviceWithTasks().add(AdviceWithTasks.afterByType(route, type, answer, selectFirst, selectLast, selectFrom, selectTo));
168            }
169            return answer;
170        }
171    
172    }