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