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