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 // Properties
069 //-----------------------------------------------------------------------
070 public List<RouteDefinition> getRoutes() {
071 return routes;
072 }
073
074 public void setRoutes(List<RouteDefinition> routes) {
075 this.routes = routes;
076 }
077
078 public List<InterceptFromDefinition> getInterceptFroms() {
079 return interceptFroms;
080 }
081
082 public void setInterceptFroms(List<InterceptFromDefinition> interceptFroms) {
083 this.interceptFroms = interceptFroms;
084 }
085
086 public List<InterceptSendToEndpointDefinition> getInterceptSendTos() {
087 return interceptSendTos;
088 }
089
090 public void setInterceptSendTos(List<InterceptSendToEndpointDefinition> interceptSendTos) {
091 this.interceptSendTos = interceptSendTos;
092 }
093
094 public List<InterceptDefinition> getIntercepts() {
095 return intercepts;
096 }
097
098 public void setIntercepts(List<InterceptDefinition> intercepts) {
099 this.intercepts = intercepts;
100 }
101
102 public List<OnExceptionDefinition> getOnExceptions() {
103 return onExceptions;
104 }
105
106 public void setOnExceptions(List<OnExceptionDefinition> onExceptions) {
107 this.onExceptions = onExceptions;
108 }
109
110 public List<OnCompletionDefinition> getOnCompletions() {
111 return onCompletions;
112 }
113
114 public void setOnCompletions(List<OnCompletionDefinition> onCompletions) {
115 this.onCompletions = onCompletions;
116 }
117
118 public ModelCamelContext getCamelContext() {
119 return camelContext;
120 }
121
122 public void setCamelContext(ModelCamelContext camelContext) {
123 this.camelContext = camelContext;
124 }
125
126 public ErrorHandlerFactory getErrorHandlerBuilder() {
127 return errorHandlerBuilder;
128 }
129
130 public void setErrorHandlerBuilder(ErrorHandlerFactory errorHandlerBuilder) {
131 this.errorHandlerBuilder = errorHandlerBuilder;
132 }
133
134 // Fluent API
135 //-------------------------------------------------------------------------
136
137 /**
138 * Creates a new route
139 *
140 * @return the builder
141 */
142 public RouteDefinition route() {
143 RouteDefinition route = createRoute();
144 return route(route);
145 }
146
147 /**
148 * Creates a new route from the given URI input
149 *
150 * @param uri the from uri
151 * @return the builder
152 */
153 public RouteDefinition from(String uri) {
154 RouteDefinition route = createRoute();
155 route.from(uri);
156 return route(route);
157 }
158
159 /**
160 * Creates a new route from the given endpoint
161 *
162 * @param endpoint the from endpoint
163 * @return the builder
164 */
165 public RouteDefinition from(Endpoint endpoint) {
166 RouteDefinition route = createRoute();
167 route.from(endpoint);
168 return route(route);
169 }
170
171 /**
172 * Creates a new route from the given URI inputs
173 *
174 * @param uris the from uri
175 * @return the builder
176 */
177 public RouteDefinition from(String... uris) {
178 RouteDefinition route = createRoute();
179 route.from(uris);
180 return route(route);
181 }
182
183 /**
184 * Creates a new route from the given endpoints
185 *
186 * @param endpoints the from endpoints
187 * @return the builder
188 */
189 public RouteDefinition from(Endpoint... endpoints) {
190 RouteDefinition route = createRoute();
191 route.from(endpoints);
192 return route(route);
193 }
194
195 /**
196 * Creates a new route using the given route
197 *
198 * @param route the route
199 * @return the builder
200 */
201 public RouteDefinition route(RouteDefinition route) {
202 // must prepare the route before we can add it to the routes list
203 RouteDefinitionHelper.prepareRoute(getCamelContext(), route, getOnExceptions(), getIntercepts(), getInterceptFroms(),
204 getInterceptSendTos(), getOnCompletions());
205 getRoutes().add(route);
206 // mark this route as prepared
207 route.markPrepared();
208 return route;
209 }
210
211 /**
212 * Creates and adds an interceptor that is triggered on every step in the route
213 * processing.
214 *
215 * @return the interceptor builder to configure
216 */
217 public InterceptDefinition intercept() {
218 InterceptDefinition answer = new InterceptDefinition();
219 getIntercepts().add(0, answer);
220 return answer;
221 }
222
223 /**
224 * Creates and adds an interceptor that is triggered when an exchange
225 * is received as input to any routes (eg from all the <tt>from</tt>)
226 *
227 * @return the interceptor builder to configure
228 */
229 public InterceptFromDefinition interceptFrom() {
230 InterceptFromDefinition answer = new InterceptFromDefinition();
231 getInterceptFroms().add(answer);
232 return answer;
233 }
234
235 /**
236 * Creates and adds an interceptor that is triggered when an exchange is received
237 * as input to the route defined with the given endpoint (eg from the <tt>from</tt>)
238 *
239 * @param uri uri of the endpoint
240 * @return the interceptor builder to configure
241 */
242 public InterceptFromDefinition interceptFrom(final String uri) {
243 InterceptFromDefinition answer = new InterceptFromDefinition(uri);
244 getInterceptFroms().add(answer);
245 return answer;
246 }
247
248 /**
249 * Creates and adds an interceptor that is triggered when an exchange is
250 * send to the given endpoint
251 *
252 * @param uri uri of the endpoint
253 * @return the builder
254 */
255 public InterceptSendToEndpointDefinition interceptSendToEndpoint(final String uri) {
256 InterceptSendToEndpointDefinition answer = new InterceptSendToEndpointDefinition(uri);
257 getInterceptSendTos().add(answer);
258 return answer;
259 }
260
261 /**
262 * Adds an on exception
263 *
264 * @param exception the exception
265 * @return the builder
266 */
267 public OnExceptionDefinition onException(Class<? extends Throwable> exception) {
268 OnExceptionDefinition answer = new OnExceptionDefinition(exception);
269 getOnExceptions().add(answer);
270 return answer;
271 }
272
273 /**
274 * Adds an on completion
275 *
276 * @return the builder
277 */
278 public OnCompletionDefinition onCompletion() {
279 OnCompletionDefinition answer = new OnCompletionDefinition();
280 getOnCompletions().add(answer);
281 return answer;
282 }
283
284 // Implementation methods
285 //-------------------------------------------------------------------------
286 protected RouteDefinition createRoute() {
287 RouteDefinition route = new RouteDefinition();
288 ErrorHandlerFactory handler = getErrorHandlerBuilder();
289 if (handler != null) {
290 route.setErrorHandlerBuilderIfNull(handler);
291 }
292 return route;
293 }
294 }