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