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