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.Route; 033 import org.apache.camel.builder.ErrorHandlerBuilder; 034 import org.apache.camel.processor.DelegateProcessor; 035 036 /** 037 * Represents a collection of routes 038 * 039 * @version $Revision: 35332 $ 040 */ 041 @XmlRootElement(name = "routes") 042 @XmlAccessorType(XmlAccessType.FIELD) 043 public class RoutesType implements RouteContainer { 044 // TODO: not sure how else to use an optional attribute in JAXB2 045 @XmlAttribute 046 private Boolean inheritErrorHandlerFlag = Boolean.TRUE; 047 @XmlElementRef 048 private List<RouteType> routes = new ArrayList<RouteType>(); 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 inheritErrorHandlerFlag != null && inheritErrorHandlerFlag.booleanValue(); 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 route.addOutput(intercept); 169 route.pushBlock(intercept.getProceed()); 170 } 171 route.getOutputs().addAll(getExceptions()); 172 getRoutes().add(route); 173 return route; 174 } 175 176 public RoutesType intercept(DelegateProcessor interceptor) { 177 getInterceptors().add(new InterceptorRef(interceptor)); 178 return this; 179 } 180 181 public InterceptType intercept() { 182 InterceptType answer = new InterceptType(); 183 getIntercepts().add(answer); 184 return answer; 185 } 186 187 public ChoiceType intercept(Predicate predicate) { 188 InterceptType answer = new InterceptType(); 189 getIntercepts().add(answer); 190 return answer.when(predicate); 191 } 192 193 public ExceptionType exception(Class exceptionType) { 194 ExceptionType answer = new ExceptionType(exceptionType); 195 getExceptions().add(answer); 196 return answer; 197 } 198 199 // Implementation methods 200 //------------------------------------------------------------------------- 201 protected RouteType createRoute() { 202 RouteType route = new RouteType(); 203 if (isInheritErrorHandler()) { 204 route.setErrorHandlerBuilder(getErrorHandlerBuilder()); 205 } 206 return route; 207 } 208 }