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.Route;
032 import org.apache.camel.Predicate;
033 import org.apache.camel.processor.DelegateProcessor;
034
035 /**
036 * Represents a collection of routes
037 *
038 * @version $Revision: $
039 */
040 @XmlRootElement(name = "routes")
041 @XmlAccessorType(XmlAccessType.FIELD)
042 public class RoutesType implements RouteContainer {
043
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 @XmlTransient
050 private List<InterceptorType> interceptors = new ArrayList<InterceptorType>();
051 @XmlTransient
052 private List<InterceptType> intercepts = new ArrayList<InterceptType>();
053 @XmlTransient
054 private List<ExceptionType> exceptions = new ArrayList<ExceptionType>();
055 @XmlTransient
056 private CamelContext camelContext;
057
058 @Override
059 public String toString() {
060 return "Routes: " + routes;
061 }
062
063 public void populateRoutes(List<Route> answer) throws Exception {
064 for (RouteType route : routes) {
065 route.addRoutes(camelContext, answer);
066 }
067 }
068
069 // Properties
070 //-----------------------------------------------------------------------
071 public List<RouteType> getRoutes() {
072 return routes;
073 }
074
075 public void setRoutes(List<RouteType> routes) {
076 this.routes = routes;
077 }
078
079 public List<InterceptorType> getInterceptors() {
080 return interceptors;
081 }
082
083 public void setInterceptors(List<InterceptorType> interceptors) {
084 this.interceptors = interceptors;
085 }
086
087 public List<InterceptType> getIntercepts() {
088 return intercepts;
089 }
090
091 public void setIntercepts(List<InterceptType> intercepts) {
092 this.intercepts = intercepts;
093 }
094
095 public List<ExceptionType> getExceptions() {
096 return exceptions;
097 }
098
099 public void setExceptions(List<ExceptionType> exceptions) {
100 this.exceptions = exceptions;
101 }
102
103 public CamelContext getCamelContext() {
104 return camelContext;
105 }
106
107 public void setCamelContext(CamelContext camelContext) {
108 this.camelContext = camelContext;
109 }
110
111 public Boolean getInheritErrorHandlerFlag() {
112 return inheritErrorHandlerFlag;
113 }
114
115 public void setInheritErrorHandlerFlag(Boolean inheritErrorHandlerFlag) {
116 this.inheritErrorHandlerFlag = inheritErrorHandlerFlag;
117 }
118
119 // Fluent API
120 //-------------------------------------------------------------------------
121
122 /**
123 * Creates a new route
124 */
125 public RouteType route() {
126 RouteType route = new RouteType();
127 return route(route);
128 }
129
130 /**
131 * Creates a new route from the given URI input
132 */
133 public RouteType from(String uri) {
134 RouteType route = new RouteType(uri);
135 return route(route);
136 }
137
138 /**
139 * Creates a new route from the given endpoint
140 */
141 public RouteType from(Endpoint endpoint) {
142 RouteType route = new RouteType(endpoint);
143 return route(route);
144 }
145
146 public RouteType route(RouteType route) {
147 // lets configure the route
148 route.setCamelContext(getCamelContext());
149 route.setInheritErrorHandlerFlag(getInheritErrorHandlerFlag());
150 route.getInterceptors().addAll(getInterceptors());
151 route.getOutputs().addAll(getIntercepts());
152 route.getOutputs().addAll(getExceptions());
153 getRoutes().add(route);
154 return route;
155 }
156
157 public RoutesType intercept(DelegateProcessor interceptor) {
158 getInterceptors().add(new InterceptorRef(interceptor));
159 return this;
160 }
161
162 public InterceptType intercept() {
163 InterceptType answer = new InterceptType();
164 getIntercepts().add(answer);
165 return answer;
166 }
167
168 public OtherwiseType intercept(Predicate predicate) {
169 InterceptType answer = new InterceptType();
170 getIntercepts().add(answer);
171 return answer.when(predicate);
172 }
173
174 public ExceptionType exception(Class exceptionType) {
175 ExceptionType answer = new ExceptionType(exceptionType);
176 getExceptions().add(answer);
177 return answer;
178 }
179 }