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.Collection; 021 import java.util.List; 022 023 import javax.xml.bind.annotation.XmlAccessType; 024 import javax.xml.bind.annotation.XmlAccessorType; 025 import javax.xml.bind.annotation.XmlAttribute; 026 import javax.xml.bind.annotation.XmlElementRef; 027 import javax.xml.bind.annotation.XmlRootElement; 028 import javax.xml.bind.annotation.XmlTransient; 029 import javax.xml.bind.annotation.XmlType; 030 031 import org.apache.camel.CamelContext; 032 import org.apache.camel.CamelContextAware; 033 import org.apache.camel.Endpoint; 034 import org.apache.camel.NoSuchEndpointException; 035 import org.apache.camel.Route; 036 import org.apache.camel.impl.RouteContext; 037 import org.apache.camel.processor.interceptor.StreamCachingInterceptor; 038 import org.apache.camel.util.CamelContextHelper; 039 import org.apache.commons.logging.Log; 040 import org.apache.commons.logging.LogFactory; 041 042 /** 043 * Represents an XML <route/> element 044 * 045 * @version $Revision: 36565 $ 046 */ 047 @XmlRootElement(name = "route") 048 @XmlType(propOrder = {"inputs", "outputs" }) 049 @XmlAccessorType(XmlAccessType.FIELD) 050 public class RouteType extends ProcessorType<ProcessorType> implements CamelContextAware { 051 private static final transient Log LOG = LogFactory.getLog(RouteType.class); 052 @XmlTransient 053 private List<InterceptorType> interceptors = new ArrayList<InterceptorType>(); 054 @XmlElementRef 055 private List<FromType> inputs = new ArrayList<FromType>(); 056 @XmlElementRef 057 private List<ProcessorType<?>> outputs = new ArrayList<ProcessorType<?>>(); 058 @XmlAttribute 059 private String group; 060 @XmlTransient 061 private CamelContext camelContext; 062 063 public RouteType() { 064 } 065 066 public RouteType(String uri) { 067 from(uri); 068 } 069 070 public RouteType(Endpoint endpoint) { 071 from(endpoint); 072 } 073 074 @Override 075 public String toString() { 076 return "Route[ " + inputs + " -> " + outputs + "]"; 077 } 078 079 public void addRoutes(CamelContext context, Collection<Route> routes) throws Exception { 080 setCamelContext(context); 081 082 for (FromType fromType : inputs) { 083 addRoutes(routes, fromType); 084 } 085 } 086 087 public Endpoint resolveEndpoint(String uri) throws NoSuchEndpointException { 088 CamelContext context = getCamelContext(); 089 if (context == null) { 090 throw new IllegalArgumentException("No CamelContext has been injected!"); 091 } 092 return CamelContextHelper.getMandatoryEndpoint(context, uri); 093 } 094 095 // Fluent API 096 // ----------------------------------------------------------------------- 097 098 /** 099 * Creates an input to the route 100 */ 101 public RouteType from(String uri) { 102 getInputs().add(new FromType(uri)); 103 return this; 104 } 105 106 /** 107 * Creates an input to the route 108 */ 109 public RouteType from(Endpoint endpoint) { 110 getInputs().add(new FromType(endpoint)); 111 return this; 112 } 113 114 /** 115 * Set the group name for this route 116 */ 117 public RouteType group(String name) { 118 setGroup(name); 119 return this; 120 } 121 122 // Properties 123 // ----------------------------------------------------------------------- 124 125 public List<InterceptorType> getInterceptors() { 126 return interceptors; 127 } 128 129 public void setInterceptors(List<InterceptorType> interceptors) { 130 this.interceptors = interceptors; 131 } 132 133 public List<FromType> getInputs() { 134 return inputs; 135 } 136 137 public void setInputs(List<FromType> inputs) { 138 this.inputs = inputs; 139 } 140 141 public List<ProcessorType<?>> getOutputs() { 142 return outputs; 143 } 144 145 public void setOutputs(List<ProcessorType<?>> outputs) { 146 this.outputs = outputs; 147 148 if (outputs != null) { 149 for (ProcessorType output : outputs) { 150 configureChild(output); 151 } 152 } 153 } 154 155 public CamelContext getCamelContext() { 156 return camelContext; 157 } 158 159 public void setCamelContext(CamelContext camelContext) { 160 this.camelContext = camelContext; 161 } 162 163 /** 164 * The group that this route belongs to; could be the name of the RouteBuilder class 165 * or be explicitly configured in the XML. 166 * 167 * May be null. 168 */ 169 public String getGroup() { 170 return group; 171 } 172 173 public void setGroup(String group) { 174 this.group = group; 175 } 176 177 // Implementation methods 178 // ------------------------------------------------------------------------- 179 180 protected void addRoutes(Collection<Route> routes, FromType fromType) throws Exception { 181 RouteContext routeContext = new RouteContext(this, fromType, routes); 182 routeContext.getEndpoint(); // force endpoint resolution 183 if (camelContext != null) { 184 camelContext.getLifecycleStrategy().onRouteContextCreate(routeContext); 185 } 186 187 List<ProcessorType<?>> list = new ArrayList<ProcessorType<?>>(outputs); 188 for (ProcessorType output : list) { 189 output.addRoutes(routeContext, routes); 190 } 191 192 routeContext.commit(); 193 } 194 195 @Override 196 protected void configureChild(ProcessorType output) { 197 super.configureChild(output); 198 199 if (isInheritErrorHandler()) { 200 output.setErrorHandlerBuilder(getErrorHandlerBuilder()); 201 } 202 203 List<InterceptorType> interceptors = getInterceptors(); 204 for (InterceptorType interceptor : interceptors) { 205 output.addInterceptor(interceptor); 206 } 207 /* 208 List<InterceptorType> list = output.getInterceptors(); 209 if (list == null) { 210 LOG.warn("No interceptor collection: " + output); 211 } else { 212 list.addAll(getInterceptors()); 213 } 214 */ 215 } 216 217 /** 218 * Disable stream caching for this Route. 219 */ 220 public RouteType noStreamCaching() { 221 StreamCachingInterceptor.noStreamCaching(interceptors); 222 return this; 223 } 224 225 /** 226 * Enable stream caching for this Route. 227 */ 228 public RouteType streamCaching() { 229 intercept(new StreamCachingInterceptor()); 230 return this; 231 } 232 }