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.model;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    import javax.xml.bind.annotation.XmlAccessType;
022    import javax.xml.bind.annotation.XmlAccessorType;
023    import javax.xml.bind.annotation.XmlElementRef;
024    import javax.xml.bind.annotation.XmlRootElement;
025    import javax.xml.bind.annotation.XmlTransient;
026    
027    import org.apache.camel.Endpoint;
028    import org.apache.camel.ErrorHandlerFactory;
029    
030    /**
031     * Represents a collection of routes
032     *
033     * @version 
034     */
035    @XmlRootElement(name = "routes")
036    @XmlAccessorType(XmlAccessType.FIELD)
037    public class RoutesDefinition extends OptionalIdentifiedDefinition<RoutesDefinition> implements RouteContainer {
038        @XmlElementRef
039        private List<RouteDefinition> routes = new ArrayList<RouteDefinition>();
040        @XmlTransient
041        private List<InterceptDefinition> intercepts = new ArrayList<InterceptDefinition>();
042        @XmlTransient
043        private List<InterceptFromDefinition> interceptFroms = new ArrayList<InterceptFromDefinition>();
044        @XmlTransient
045        private List<InterceptSendToEndpointDefinition> interceptSendTos = new ArrayList<InterceptSendToEndpointDefinition>();
046        @XmlTransient
047        private List<OnExceptionDefinition> onExceptions = new ArrayList<OnExceptionDefinition>();
048        @XmlTransient
049        private List<OnCompletionDefinition> onCompletions = new ArrayList<OnCompletionDefinition>();
050        @XmlTransient
051        private ModelCamelContext camelContext;
052        @XmlTransient
053        private ErrorHandlerFactory errorHandlerBuilder;
054    
055        public RoutesDefinition() {
056        }
057    
058        @Override
059        public String toString() {
060            return "Routes: " + routes;
061        }
062    
063        @Override
064        public String getShortName() {
065            return "routes";
066        }
067    
068        public String getLabel() {
069            return "Route " + getId();
070        }
071    
072        // Properties
073        //-----------------------------------------------------------------------
074        public List<RouteDefinition> getRoutes() {
075            return routes;
076        }
077    
078        public void setRoutes(List<RouteDefinition> routes) {
079            this.routes = routes;
080        }
081    
082        public List<InterceptFromDefinition> getInterceptFroms() {
083            return interceptFroms;
084        }
085    
086        public void setInterceptFroms(List<InterceptFromDefinition> interceptFroms) {
087            this.interceptFroms = interceptFroms;
088        }
089    
090        public List<InterceptSendToEndpointDefinition> getInterceptSendTos() {
091            return interceptSendTos;
092        }
093    
094        public void setInterceptSendTos(List<InterceptSendToEndpointDefinition> interceptSendTos) {
095            this.interceptSendTos = interceptSendTos;
096        }
097    
098        public List<InterceptDefinition> getIntercepts() {
099            return intercepts;
100        }
101    
102        public void setIntercepts(List<InterceptDefinition> intercepts) {
103            this.intercepts = intercepts;
104        }
105    
106        public List<OnExceptionDefinition> getOnExceptions() {
107            return onExceptions;
108        }
109    
110        public void setOnExceptions(List<OnExceptionDefinition> onExceptions) {
111            this.onExceptions = onExceptions;
112        }
113    
114        public List<OnCompletionDefinition> getOnCompletions() {
115            return onCompletions;
116        }
117    
118        public void setOnCompletions(List<OnCompletionDefinition> onCompletions) {
119            this.onCompletions = onCompletions;
120        }
121    
122        public ModelCamelContext getCamelContext() {
123            return camelContext;
124        }
125    
126        public void setCamelContext(ModelCamelContext camelContext) {
127            this.camelContext = camelContext;
128        }
129    
130        public ErrorHandlerFactory getErrorHandlerBuilder() {
131            return errorHandlerBuilder;
132        }
133    
134        public void setErrorHandlerBuilder(ErrorHandlerFactory errorHandlerBuilder) {
135            this.errorHandlerBuilder = errorHandlerBuilder;
136        }
137    
138        // Fluent API
139        //-------------------------------------------------------------------------
140    
141        /**
142         * Creates a new route
143         *
144         * @return the builder
145         */
146        public RouteDefinition route() {
147            RouteDefinition route = createRoute();
148            return route(route);
149        }
150    
151        /**
152         * Creates a new route from the given URI input
153         *
154         * @param uri  the from uri
155         * @return the builder
156         */
157        public RouteDefinition from(String uri) {
158            RouteDefinition route = createRoute();
159            route.from(uri);
160            return route(route);
161        }
162    
163        /**
164         * Creates a new route from the given endpoint
165         *
166         * @param endpoint  the from endpoint
167         * @return the builder
168         */
169        public RouteDefinition from(Endpoint endpoint) {
170            RouteDefinition route = createRoute();
171            route.from(endpoint);
172            return route(route);
173        }
174    
175        /**
176         * Creates a new route from the given URI inputs
177         *
178         * @param uris  the from uri
179         * @return the builder
180         */
181        public RouteDefinition from(String... uris) {
182            RouteDefinition route = createRoute();
183            route.from(uris);
184            return route(route);
185        }
186    
187        /**
188         * Creates a new route from the given endpoints
189         *
190         * @param endpoints  the from endpoints
191         * @return the builder
192         */
193        public RouteDefinition from(Endpoint... endpoints) {
194            RouteDefinition route = createRoute();
195            route.from(endpoints);
196            return route(route);
197        }
198    
199        /**
200         * Creates a new route using the given route
201         *
202         * @param route the route
203         * @return the builder
204         */
205        public RouteDefinition route(RouteDefinition route) {
206            // must prepare the route before we can add it to the routes list
207            RouteDefinitionHelper.prepareRoute(getCamelContext(), route, getOnExceptions(), getIntercepts(), getInterceptFroms(),
208                    getInterceptSendTos(), getOnCompletions());
209            getRoutes().add(route);
210            // mark this route as prepared
211            route.markPrepared();
212            return route;
213        }
214    
215        /**
216         * Creates and adds an interceptor that is triggered on every step in the route
217         * processing.
218         *
219         * @return the interceptor builder to configure
220         */
221        public InterceptDefinition intercept() {
222            InterceptDefinition answer = new InterceptDefinition();
223            getIntercepts().add(0, answer);
224            return answer;
225        }
226    
227        /**
228         * Creates and adds an interceptor that is triggered when an exchange
229         * is received as input to any routes (eg from all the <tt>from</tt>)
230         *
231         * @return the interceptor builder to configure
232         */
233        public InterceptFromDefinition interceptFrom() {
234            InterceptFromDefinition answer = new InterceptFromDefinition();
235            getInterceptFroms().add(answer);
236            return answer;
237        }
238    
239        /**
240         * Creates and adds an interceptor that is triggered when an exchange is received
241         * as input to the route defined with the given endpoint (eg from the <tt>from</tt>)
242         *
243         * @param uri uri of the endpoint
244         * @return the interceptor builder to configure
245         */
246        public InterceptFromDefinition interceptFrom(final String uri) {
247            InterceptFromDefinition answer = new InterceptFromDefinition(uri);
248            getInterceptFroms().add(answer);
249            return answer;
250        }
251    
252        /**
253         * Creates and adds an interceptor that is triggered when an exchange is
254         * send to the given endpoint
255         *
256         * @param uri uri of the endpoint
257         * @return  the builder
258         */
259        public InterceptSendToEndpointDefinition interceptSendToEndpoint(final String uri) {
260            InterceptSendToEndpointDefinition answer = new InterceptSendToEndpointDefinition(uri);
261            getInterceptSendTos().add(answer);
262            return answer;
263        }
264    
265        /**
266         * Adds an on exception
267         * 
268         * @param exception  the exception
269         * @return the builder
270         */
271        public OnExceptionDefinition onException(Class<? extends Throwable> exception) {
272            OnExceptionDefinition answer = new OnExceptionDefinition(exception);
273            answer.setRouteScoped(false);
274            getOnExceptions().add(answer);
275            return answer;
276        }
277    
278        /**
279         * Adds an on completion
280         *
281         * @return the builder
282         */
283        public OnCompletionDefinition onCompletion() {
284            OnCompletionDefinition answer = new OnCompletionDefinition();
285            getOnCompletions().add(answer);
286            return answer;
287        }
288    
289        // Implementation methods
290        //-------------------------------------------------------------------------
291        protected RouteDefinition createRoute() {
292            RouteDefinition route = new RouteDefinition();
293            ErrorHandlerFactory handler = getErrorHandlerBuilder();
294            if (handler != null) {
295                route.setErrorHandlerBuilderIfNull(handler);
296            }
297            return route;
298        }
299    }