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.CamelContext; 028 import org.apache.camel.Endpoint; 029 import org.apache.camel.builder.ErrorHandlerBuilder; 030 import org.apache.camel.util.EndpointHelper; 031 032 /** 033 * Represents a collection of routes 034 * 035 * @version $Revision: 788663 $ 036 */ 037 @XmlRootElement(name = "routes") 038 @XmlAccessorType(XmlAccessType.FIELD) 039 public class RoutesDefinition extends OptionalIdentifiedType<RoutesDefinition> implements RouteContainer { 040 @XmlElementRef 041 private List<RouteDefinition> routes = new ArrayList<RouteDefinition>(); 042 @XmlTransient 043 private List<InterceptDefinition> intercepts = new ArrayList<InterceptDefinition>(); 044 @XmlTransient 045 private List<InterceptFromDefinition> interceptFroms = new ArrayList<InterceptFromDefinition>(); 046 @XmlTransient 047 private List<InterceptSendToEndpointDefinition> interceptSendTos = new ArrayList<InterceptSendToEndpointDefinition>(); 048 @XmlTransient 049 private List<OnExceptionDefinition> onExceptions = new ArrayList<OnExceptionDefinition>(); 050 @XmlTransient 051 private List<OnCompletionDefinition> onCompletions = new ArrayList<OnCompletionDefinition>(); 052 @XmlTransient 053 private CamelContext camelContext; 054 @XmlTransient 055 private ErrorHandlerBuilder errorHandlerBuilder; 056 057 @Override 058 public String toString() { 059 return "Routes: " + routes; 060 } 061 062 @Override 063 public String getShortName() { 064 return "routes"; 065 } 066 067 // Properties 068 //----------------------------------------------------------------------- 069 public List<RouteDefinition> getRoutes() { 070 return routes; 071 } 072 073 public void setRoutes(List<RouteDefinition> routes) { 074 this.routes = routes; 075 } 076 077 public List<InterceptFromDefinition> getInterceptFroms() { 078 return interceptFroms; 079 } 080 081 public void setInterceptFroms(List<InterceptFromDefinition> interceptFroms) { 082 this.interceptFroms = interceptFroms; 083 } 084 085 public List<InterceptSendToEndpointDefinition> getInterceptSendTos() { 086 return interceptSendTos; 087 } 088 089 public void setInterceptSendTos(List<InterceptSendToEndpointDefinition> interceptSendTos) { 090 this.interceptSendTos = interceptSendTos; 091 } 092 093 public List<InterceptDefinition> getIntercepts() { 094 return intercepts; 095 } 096 097 public void setIntercepts(List<InterceptDefinition> intercepts) { 098 this.intercepts = intercepts; 099 } 100 101 public List<OnExceptionDefinition> getOnExceptions() { 102 return onExceptions; 103 } 104 105 public void setOnExceptions(List<OnExceptionDefinition> onExceptions) { 106 this.onExceptions = onExceptions; 107 } 108 109 public List<OnCompletionDefinition> getOnCompletions() { 110 return onCompletions; 111 } 112 113 public void setOnCompletions(List<OnCompletionDefinition> onCompletions) { 114 this.onCompletions = onCompletions; 115 } 116 117 public CamelContext getCamelContext() { 118 return camelContext; 119 } 120 121 public void setCamelContext(CamelContext camelContext) { 122 this.camelContext = camelContext; 123 } 124 125 public ErrorHandlerBuilder getErrorHandlerBuilder() { 126 return errorHandlerBuilder; 127 } 128 129 public void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder) { 130 this.errorHandlerBuilder = errorHandlerBuilder; 131 } 132 133 // Fluent API 134 //------------------------------------------------------------------------- 135 136 /** 137 * Creates a new route 138 * 139 * @return the builder 140 */ 141 public RouteDefinition route() { 142 RouteDefinition route = createRoute(); 143 return route(route); 144 } 145 146 /** 147 * Creates a new route from the given URI input 148 * 149 * @param uri the from uri 150 * @return the builder 151 */ 152 public RouteDefinition from(String uri) { 153 RouteDefinition route = createRoute(); 154 route.from(uri); 155 return route(route); 156 } 157 158 /** 159 * Creates a new route from the given endpoint 160 * 161 * @param endpoint the from endpoint 162 * @return the builder 163 */ 164 public RouteDefinition from(Endpoint endpoint) { 165 RouteDefinition route = createRoute(); 166 route.from(endpoint); 167 return route(route); 168 } 169 170 /** 171 * Creates a new route from the given URI inputs 172 * 173 * @param uris the from uri 174 * @return the builder 175 */ 176 public RouteDefinition from(String... uris) { 177 RouteDefinition route = createRoute(); 178 route.from(uris); 179 return route(route); 180 } 181 182 /** 183 * Creates a new route from the given endpoints 184 * 185 * @param endpoints the from endpoints 186 * @return the builder 187 */ 188 public RouteDefinition from(Endpoint... endpoints) { 189 RouteDefinition route = createRoute(); 190 route.from(endpoints); 191 return route(route); 192 } 193 194 /** 195 * Creates a new route using the given route 196 * 197 * @param route the route 198 * @return the builder 199 */ 200 public RouteDefinition route(RouteDefinition route) { 201 // lets configure the route 202 route.setCamelContext(getCamelContext()); 203 204 // configure intercepts 205 for (InterceptDefinition intercept : getIntercepts()) { 206 // add as first output so intercept is handled before the acutal route and that gives 207 // us the needed head start to init and be able to intercept all the remaining processing steps 208 route.getOutputs().add(0, intercept); 209 } 210 211 // configure intercept from 212 for (InterceptFromDefinition intercept : getInterceptFroms()) { 213 214 // should we only apply interceptor for a given endpoint uri 215 boolean match = true; 216 if (intercept.getUri() != null) { 217 match = false; 218 for (FromDefinition input : route.getInputs()) { 219 if (EndpointHelper.matchEndpoint(input.getUri(), intercept.getUri())) { 220 match = true; 221 break; 222 } 223 } 224 } 225 226 if (match) { 227 // add as first output so intercept is handled before the acutal route and that gives 228 // us the needed head start to init and be able to intercept all the remaining processing steps 229 route.getOutputs().add(0, intercept); 230 } 231 } 232 233 // configure intercept send to endpoint 234 for (InterceptSendToEndpointDefinition sendTo : getInterceptSendTos()) { 235 // add as first output so intercept is handled before the acutal route and that gives 236 // us the needed head start to init and be able to intercept all the remaining processing steps 237 route.getOutputs().add(0, sendTo); 238 } 239 240 // add on completions after the interceptors 241 route.getOutputs().addAll(getOnCompletions()); 242 243 // add on exceptions at top since we need to inject this by the error handlers 244 route.getOutputs().addAll(0, getOnExceptions()); 245 246 getRoutes().add(route); 247 return route; 248 } 249 250 /** 251 * Creates and adds an interceptor that is triggered on every step in the route 252 * processing. 253 * 254 * @return the interceptor builder to configure 255 */ 256 public InterceptDefinition intercept() { 257 InterceptDefinition answer = new InterceptDefinition(); 258 getIntercepts().add(0, answer); 259 return answer; 260 } 261 262 /** 263 * Creates and adds an interceptor that is triggered when an exchange 264 * is received as input to any routes (eg from all the <tt>from</tt>) 265 * 266 * @return the interceptor builder to configure 267 */ 268 public InterceptFromDefinition interceptFrom() { 269 InterceptFromDefinition answer = new InterceptFromDefinition(); 270 getInterceptFroms().add(answer); 271 return answer; 272 } 273 274 /** 275 * Creates and adds an interceptor that is triggered when an exchange is received 276 * as input to the route defined with the given endpoint (eg from the <tt>from</tt>) 277 * 278 * @param uri uri of the endpoint 279 * @return the interceptor builder to configure 280 */ 281 public InterceptFromDefinition interceptFrom(final String uri) { 282 InterceptFromDefinition answer = new InterceptFromDefinition(uri); 283 getInterceptFroms().add(answer); 284 return answer; 285 } 286 287 /** 288 * Creates and adds an interceptor that is triggered when an exchange is 289 * send to the given endpoint 290 * 291 * @param uri uri of the endpoint 292 * @return the builder 293 */ 294 public InterceptSendToEndpointDefinition interceptSendToEndpoint(final String uri) { 295 InterceptSendToEndpointDefinition answer = new InterceptSendToEndpointDefinition(uri); 296 getInterceptSendTos().add(answer); 297 return answer; 298 } 299 300 /** 301 * Adds an on exception 302 * 303 * @param exception the exception 304 * @return the builder 305 */ 306 public OnExceptionDefinition onException(Class exception) { 307 OnExceptionDefinition answer = new OnExceptionDefinition(exception); 308 getOnExceptions().add(answer); 309 return answer; 310 } 311 312 /** 313 * Adds an on completion 314 * 315 * @return the builder 316 */ 317 public OnCompletionDefinition onCompletion() { 318 OnCompletionDefinition answer = new OnCompletionDefinition(); 319 getOnCompletions().add(answer); 320 return answer; 321 } 322 323 // Implementation methods 324 //------------------------------------------------------------------------- 325 protected RouteDefinition createRoute() { 326 RouteDefinition route = new RouteDefinition(); 327 ErrorHandlerBuilder handler = getErrorHandlerBuilder(); 328 if (handler != null) { 329 route.setErrorHandlerBuilderIfNull(handler); 330 } 331 return route; 332 } 333 }