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
022 import javax.xml.bind.annotation.XmlAccessType;
023 import javax.xml.bind.annotation.XmlAccessorType;
024 import javax.xml.bind.annotation.XmlAttribute;
025 import javax.xml.bind.annotation.XmlElementRef;
026 import javax.xml.bind.annotation.XmlRootElement;
027 import javax.xml.bind.annotation.XmlTransient;
028
029 import org.apache.camel.CamelContext;
030 import org.apache.camel.Endpoint;
031 import org.apache.camel.Predicate;
032 import org.apache.camel.builder.ErrorHandlerBuilder;
033 import org.apache.camel.processor.DelegateProcessor;
034
035 /**
036 * Represents a collection of routes
037 *
038 * @version $Revision: 720799 $
039 */
040 @XmlRootElement(name = "routes")
041 @XmlAccessorType(XmlAccessType.FIELD)
042 public class RoutesType extends OptionalIdentifiedType<RoutesType> implements RouteContainer {
043 // TODO: not sure how else to use an optional attribute in JAXB2
044 @XmlAttribute
045 private Boolean inheritErrorHandlerFlag;
046 @XmlElementRef
047 private List<RouteType> routes = new ArrayList<RouteType>();
048 // @deprecated will be removed in Camel 2.0
049 @XmlElementRef
050 private List<ServiceActivationType> activations = new ArrayList<ServiceActivationType>();
051 @XmlTransient
052 private List<InterceptorType> interceptors = new ArrayList<InterceptorType>();
053 @XmlTransient
054 private List<InterceptType> intercepts = new ArrayList<InterceptType>();
055 @XmlTransient
056 private List<ExceptionType> exceptions = new ArrayList<ExceptionType>();
057 @XmlTransient
058 private CamelContext camelContext;
059 @XmlTransient
060 private ErrorHandlerBuilder errorHandlerBuilder;
061
062 @Override
063 public String toString() {
064 return "Routes: " + routes;
065 }
066
067 // Properties
068 //-----------------------------------------------------------------------
069 public List<RouteType> getRoutes() {
070 return routes;
071 }
072
073 public void setRoutes(List<RouteType> routes) {
074 this.routes = routes;
075 }
076
077 public List<InterceptorType> getInterceptors() {
078 return interceptors;
079 }
080
081 public void setInterceptors(List<InterceptorType> interceptors) {
082 this.interceptors = interceptors;
083 }
084
085 public List<InterceptType> getIntercepts() {
086 return intercepts;
087 }
088
089 public void setIntercepts(List<InterceptType> intercepts) {
090 this.intercepts = intercepts;
091 }
092
093 public List<ExceptionType> getExceptions() {
094 return exceptions;
095 }
096
097 public void setExceptions(List<ExceptionType> exceptions) {
098 this.exceptions = exceptions;
099 }
100
101 public CamelContext getCamelContext() {
102 return camelContext;
103 }
104
105 public void setCamelContext(CamelContext camelContext) {
106 this.camelContext = camelContext;
107 }
108
109 public boolean isInheritErrorHandler() {
110 return ProcessorType.isInheritErrorHandler(getInheritErrorHandlerFlag());
111 }
112
113 public Boolean getInheritErrorHandlerFlag() {
114 return inheritErrorHandlerFlag;
115 }
116
117 public void setInheritErrorHandlerFlag(Boolean inheritErrorHandlerFlag) {
118 this.inheritErrorHandlerFlag = inheritErrorHandlerFlag;
119 }
120
121 public ErrorHandlerBuilder getErrorHandlerBuilder() {
122 return errorHandlerBuilder;
123 }
124
125 public void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder) {
126 this.errorHandlerBuilder = errorHandlerBuilder;
127 }
128
129 // Fluent API
130 //-------------------------------------------------------------------------
131
132 /**
133 * Creates a new route
134 */
135 public RouteType route() {
136 RouteType route = createRoute();
137 return route(route);
138 }
139
140 /**
141 * Creates a new route from the given URI input
142 */
143 public RouteType from(String uri) {
144 RouteType route = createRoute();
145 route.from(uri);
146 return route(route);
147 }
148
149 /**
150 * Creates a new route from the given endpoint
151 */
152 public RouteType from(Endpoint endpoint) {
153 RouteType route = createRoute();
154 route.from(endpoint);
155 return route(route);
156 }
157
158 public RouteType route(RouteType route) {
159 // lets configure the route
160 route.setCamelContext(getCamelContext());
161 route.setInheritErrorHandlerFlag(getInheritErrorHandlerFlag());
162 List<InterceptorType> list = getInterceptors();
163 for (InterceptorType interceptorType : list) {
164 route.addInterceptor(interceptorType);
165 }
166 List<InterceptType> intercepts = getIntercepts();
167 for (InterceptType intercept : intercepts) {
168 // need to create a proxy for this one and use the
169 // proceed of the proxy which will be local to this route
170 InterceptType proxy = intercept.createProxy();
171 route.addOutput(proxy);
172 route.pushBlock(proxy.getProceed());
173 }
174 route.getOutputs().addAll(getExceptions());
175 getRoutes().add(route);
176 return route;
177 }
178
179 public RoutesType intercept(DelegateProcessor interceptor) {
180 getInterceptors().add(new InterceptorRef(interceptor));
181 return this;
182 }
183
184 public InterceptType intercept() {
185 InterceptType answer = new InterceptType();
186 getIntercepts().add(answer);
187 return answer;
188 }
189
190 public ChoiceType intercept(Predicate predicate) {
191 InterceptType answer = new InterceptType();
192 getIntercepts().add(answer);
193 return answer.when(predicate);
194 }
195
196 public ExceptionType onException(Class exceptionType) {
197 ExceptionType answer = new ExceptionType(exceptionType);
198 getExceptions().add(answer);
199 return answer;
200 }
201
202 // Implementation methods
203 //-------------------------------------------------------------------------
204 protected RouteType createRoute() {
205 RouteType route = new RouteType();
206 ErrorHandlerBuilder handler = getErrorHandlerBuilder();
207 if (isInheritErrorHandler() && handler != null) {
208 route.setErrorHandlerBuilderIfNull(handler);
209 }
210 return route;
211 }
212 }