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: 751357 $
039     */
040    @XmlRootElement(name = "routes")
041    @XmlAccessorType(XmlAccessType.FIELD)
042    public class RoutesDefinition extends OptionalIdentifiedType<RoutesDefinition> 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<RouteDefinition> routes = new ArrayList<RouteDefinition>();
048        @XmlTransient
049        private List<AbstractInterceptorDefinition> interceptors = new ArrayList<AbstractInterceptorDefinition>();
050        @XmlTransient
051        private List<InterceptDefinition> intercepts = new ArrayList<InterceptDefinition>();
052        @XmlTransient
053        private List<OnExceptionDefinition> exceptions = new ArrayList<OnExceptionDefinition>();
054        @XmlTransient
055        private CamelContext camelContext;
056        @XmlTransient
057        private ErrorHandlerBuilder errorHandlerBuilder;
058    
059        @Override
060        public String toString() {
061            return "Routes: " + routes;
062        }
063    
064        // Properties
065        //-----------------------------------------------------------------------
066        public List<RouteDefinition> getRoutes() {
067            return routes;
068        }
069    
070        public void setRoutes(List<RouteDefinition> routes) {
071            this.routes = routes;
072        }
073    
074        public List<AbstractInterceptorDefinition> getInterceptors() {
075            return interceptors;
076        }
077    
078        public void setInterceptors(List<AbstractInterceptorDefinition> interceptors) {
079            this.interceptors = interceptors;
080        }
081    
082        public List<InterceptDefinition> getIntercepts() {
083            return intercepts;
084        }
085    
086        public void setIntercepts(List<InterceptDefinition> intercepts) {
087            this.intercepts = intercepts;
088        }
089    
090        public List<OnExceptionDefinition> getExceptions() {
091            return exceptions;
092        }
093    
094        public void setExceptions(List<OnExceptionDefinition> exceptions) {
095            this.exceptions = exceptions;
096        }
097    
098        public CamelContext getCamelContext() {
099            return camelContext;
100        }
101    
102        public void setCamelContext(CamelContext camelContext) {
103            this.camelContext = camelContext;
104        }
105    
106        public boolean isInheritErrorHandler() {
107            return ProcessorDefinition.isInheritErrorHandler(getInheritErrorHandlerFlag());
108        }
109    
110        public Boolean getInheritErrorHandlerFlag() {
111            return inheritErrorHandlerFlag;
112        }
113    
114        public void setInheritErrorHandlerFlag(Boolean inheritErrorHandlerFlag) {
115            this.inheritErrorHandlerFlag = inheritErrorHandlerFlag;
116        }
117    
118        public ErrorHandlerBuilder getErrorHandlerBuilder() {
119            return errorHandlerBuilder;
120        }
121    
122        public void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder) {
123            this.errorHandlerBuilder = errorHandlerBuilder;
124        }
125    
126        // Fluent API
127        //-------------------------------------------------------------------------
128    
129        /**
130         * Creates a new route
131         *
132         * @return the builder
133         */
134        public RouteDefinition route() {
135            RouteDefinition route = createRoute();
136            return route(route);
137        }
138    
139        /**
140         * Creates a new route from the given URI input
141         *
142         * @param uri  the from uri
143         * @return the builder
144         */
145        public RouteDefinition from(String uri) {
146            RouteDefinition route = createRoute();
147            route.from(uri);
148            return route(route);
149        }
150    
151        /**
152         * Creates a new route from the given endpoint
153         *
154         * @param endpoint  the from endpoint
155         * @return the builder
156         */
157        public RouteDefinition from(Endpoint endpoint) {
158            RouteDefinition route = createRoute();
159            route.from(endpoint);
160            return route(route);
161        }
162    
163        /**
164         * Creates a new route from the given URI inputs
165         *
166         * @param uris  the from uri
167         * @return the builder
168         */
169        public RouteDefinition from(String... uris) {
170            RouteDefinition route = createRoute();
171            route.from(uris);
172            return route(route);
173        }
174    
175        /**
176         * Creates a new route from the given endpoints
177         *
178         * @param endpoints  the from endpoints
179         * @return the builder
180         */
181        public RouteDefinition from(Endpoint... endpoints) {
182            RouteDefinition route = createRoute();
183            route.from(endpoints);
184            return route(route);
185        }
186    
187        /**
188         * Creates a new route using the given route
189         *
190         * @param route the route
191         * @return the builder
192         */
193        public RouteDefinition route(RouteDefinition route) {
194            // lets configure the route
195            route.setCamelContext(getCamelContext());
196            route.setInheritErrorHandlerFlag(getInheritErrorHandlerFlag());
197            List<AbstractInterceptorDefinition> list = getInterceptors();
198            for (AbstractInterceptorDefinition interceptorType : list) {
199                route.addInterceptor(interceptorType);
200            }
201            List<InterceptDefinition> intercepts = getIntercepts();
202            for (InterceptDefinition intercept : intercepts) {
203                // need to create a proxy for this one and use the
204                // proceed of the proxy which will be local to this route
205                InterceptDefinition proxy = intercept.createProxy();
206                route.addOutput(proxy);
207                route.pushBlock(proxy.getProceed());
208            }
209            route.getOutputs().addAll(getExceptions());
210            getRoutes().add(route);
211            return route;
212        }
213    
214        /**
215         * Adds an interceptor
216         *
217         * @param interceptor  the interceptor
218         * @return the builder
219         */
220        public RoutesDefinition intercept(DelegateProcessor interceptor) {
221            getInterceptors().add(new InterceptorDefinition(interceptor));
222            return this;
223        }
224    
225        /**
226         * Creates and adds an interceptor
227         *
228         * @return the interceptor builder to configure
229         */
230        public InterceptDefinition intercept() {
231            InterceptDefinition answer = new InterceptDefinition();
232            getIntercepts().add(answer);
233            return answer;
234        }
235    
236        /**
237         * Creates and adds an interceptor that is attached with a predicate
238         *
239         * @param predicate  the predicate
240         * @return the builder
241         */
242        public ChoiceDefinition intercept(Predicate predicate) {
243            InterceptDefinition answer = new InterceptDefinition();
244            getIntercepts().add(answer);
245            return answer.when(predicate);
246        }
247    
248        /**
249         * Adds an on exception
250         * 
251         * @param exception  the exception
252         * @return the builder
253         */
254        public OnExceptionDefinition onException(Class exception) {
255            OnExceptionDefinition answer = new OnExceptionDefinition(exception);
256            getExceptions().add(answer);
257            return answer;
258        }
259    
260        // Implementation methods
261        //-------------------------------------------------------------------------
262        protected RouteDefinition createRoute() {
263            RouteDefinition route = new RouteDefinition();
264            ErrorHandlerBuilder handler = getErrorHandlerBuilder();
265            if (isInheritErrorHandler() && handler != null) {
266                route.setErrorHandlerBuilderIfNull(handler);
267            }
268            return route;
269        }
270    }