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