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 */
017package org.apache.camel.model;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.List;
022import java.util.concurrent.atomic.AtomicBoolean;
023import java.util.function.Supplier;
024
025import javax.xml.bind.annotation.XmlAccessType;
026import javax.xml.bind.annotation.XmlAccessorType;
027import javax.xml.bind.annotation.XmlAttribute;
028import javax.xml.bind.annotation.XmlElement;
029import javax.xml.bind.annotation.XmlElementRef;
030import javax.xml.bind.annotation.XmlRootElement;
031import javax.xml.bind.annotation.XmlTransient;
032import javax.xml.bind.annotation.XmlType;
033
034import org.apache.camel.Endpoint;
035import org.apache.camel.ErrorHandlerFactory;
036import org.apache.camel.NamedRoute;
037import org.apache.camel.ShutdownRoute;
038import org.apache.camel.ShutdownRunningTask;
039import org.apache.camel.builder.EndpointConsumerBuilder;
040import org.apache.camel.builder.ErrorHandlerBuilderRef;
041import org.apache.camel.model.rest.RestBindingDefinition;
042import org.apache.camel.model.rest.RestDefinition;
043import org.apache.camel.spi.AsEndpointUri;
044import org.apache.camel.spi.Metadata;
045import org.apache.camel.spi.RoutePolicy;
046
047/**
048 * A Camel route
049 */
050@Metadata(label = "configuration")
051@XmlRootElement(name = "route")
052@XmlType(propOrder = {"input", "inputType", "outputType", "outputs", "routeProperties"})
053@XmlAccessorType(XmlAccessType.PROPERTY)
054// must use XmlAccessType.PROPERTY as there is some custom logic needed to be executed in the setter methods
055public class RouteDefinition extends OutputDefinition<RouteDefinition> implements NamedRoute {
056    private final AtomicBoolean prepared = new AtomicBoolean(false);
057    private FromDefinition input;
058    private String group;
059    private String streamCache;
060    private String trace;
061    private String messageHistory;
062    private String logMask;
063    private String delayer;
064    private String autoStartup;
065    private Integer startupOrder;
066    private List<RoutePolicy> routePolicies;
067    private String routePolicyRef;
068    private String shutdownRoute;
069    private String shutdownRunningTask;
070    private String errorHandlerRef;
071    private ErrorHandlerFactory errorHandlerFactory;
072    // keep state whether the error handler is context scoped or not
073    // (will by default be context scoped of no explicit error handler
074    // configured)
075    private boolean contextScopedErrorHandler = true;
076    private Boolean rest;
077    private RestDefinition restDefinition;
078    private RestBindingDefinition restBindingDefinition;
079    private InputTypeDefinition inputType;
080    private OutputTypeDefinition outputType;
081    private List<PropertyDefinition> routeProperties;
082
083    public RouteDefinition() {
084    }
085
086    public RouteDefinition(@AsEndpointUri String uri) {
087        from(uri);
088    }
089
090    public RouteDefinition(Endpoint endpoint) {
091        from(endpoint);
092    }
093
094    /**
095     * This route is created from the REST DSL.
096     */
097    public void fromRest(@AsEndpointUri String uri) {
098        from(uri);
099        rest = true;
100    }
101
102    /**
103     * Check if the route has been prepared
104     *
105     * @return wether the route has been prepared or not
106     * @see RouteDefinitionHelper#prepareRoute(ModelCamelContext,
107     *      RouteDefinition)
108     */
109    public boolean isPrepared() {
110        return prepared.get();
111    }
112
113    /**
114     * Marks the route definition as prepared.
115     * <p/>
116     * This is needed if routes have been created by components such as
117     * <tt>camel-spring</tt> or <tt>camel-blueprint</tt>. Usually they share
118     * logic in the <tt>camel-core-xml</tt> module which prepares the routes.
119     */
120    public void markPrepared() {
121        prepared.set(true);
122    }
123
124    /**
125     * Marks the route definition as un-prepared.
126     * <p/>
127     * This is needed if routes have been created by components such as
128     * <tt>camel-scala</tt>. To unset the prepare so the routes can be prepared
129     * at a later stage when scala has build the routes completely.
130     */
131    public void markUnprepared() {
132        prepared.set(false);
133    }
134
135    @Override
136    public String toString() {
137        if (getId() != null) {
138            return "Route(" + getId() + ")[" + (input != null ? input : "") + " -> " + outputs + "]";
139        } else {
140            return "Route[" + input + " -> " + outputs + "]";
141        }
142    }
143
144    @Override
145    public String getShortName() {
146        return "route";
147    }
148
149    @Override
150    public String getLabel() {
151        return "Route[" + input.getLabel() + "]";
152    }
153
154    @Override
155    public String getRouteId() {
156        return getId();
157    }
158
159    @Override
160    public String getEndpointUrl() {
161        return input != null ? input.getEndpointUri() : null;
162    }
163
164    // Fluent API
165    // -----------------------------------------------------------------------
166
167    /**
168     * Creates an input to the route
169     *
170     * @param uri the from uri
171     * @return the builder
172     */
173    public RouteDefinition from(@AsEndpointUri String uri) {
174        setInput(new FromDefinition(uri));
175        return this;
176    }
177
178    /**
179     * Creates an input to the route
180     *
181     * @param endpoint the from endpoint
182     * @return the builder
183     */
184    public RouteDefinition from(Endpoint endpoint) {
185        setInput(new FromDefinition(endpoint));
186        return this;
187    }
188
189    /**
190     * Creates an input to the route
191     *
192     * @param endpoint the from endpoint
193     * @return the builder
194     */
195    public RouteDefinition from(EndpointConsumerBuilder endpoint) {
196        setInput(new FromDefinition(endpoint));
197        return this;
198    }
199
200    /**
201     * Set the group name for this route
202     *
203     * @param name the group name
204     * @return the builder
205     */
206    public RouteDefinition group(String name) {
207        setGroup(name);
208        return this;
209    }
210
211    /**
212     * Set the route group for this route
213     *
214     * @param group the route group
215     * @return the builder
216     */
217    @Override
218    public RouteDefinition routeGroup(String group) {
219        setGroup(group);
220        return this;
221    }
222
223    /**
224     * Set the route id for this route
225     *
226     * @param id the route id
227     * @return the builder
228     */
229    @Override
230    public RouteDefinition routeId(String id) {
231        if (hasCustomIdAssigned()) {
232            throw new IllegalArgumentException("You can only set routeId one time per route.");
233        }
234        setId(id);
235        return this;
236    }
237
238    /**
239     * Set the route description for this route
240     *
241     * @param description the route description
242     * @return the builder
243     */
244    @Override
245    public RouteDefinition routeDescription(String description) {
246        DescriptionDefinition desc = new DescriptionDefinition();
247        desc.setText(description);
248        setDescription(desc);
249        return this;
250    }
251
252    /**
253     * Disable stream caching for this route.
254     *
255     * @return the builder
256     */
257    public RouteDefinition noStreamCaching() {
258        setStreamCache("false");
259        return this;
260    }
261
262    /**
263     * Enable stream caching for this route.
264     *
265     * @return the builder
266     */
267    public RouteDefinition streamCaching() {
268        setStreamCache("true");
269        return this;
270    }
271
272    /**
273     * Enable stream caching for this route.
274     *
275     * @param streamCache whether to use stream caching (true or false), the
276     *            value can be a property placeholder
277     * @return the builder
278     */
279    public RouteDefinition streamCaching(String streamCache) {
280        setStreamCache(streamCache);
281        return this;
282    }
283
284    /**
285     * Disable tracing for this route.
286     *
287     * @return the builder
288     */
289    public RouteDefinition noTracing() {
290        setTrace("false");
291        return this;
292    }
293
294    /**
295     * Enable tracing for this route.
296     *
297     * @return the builder
298     */
299    public RouteDefinition tracing() {
300        setTrace("true");
301        return this;
302    }
303
304    /**
305     * Enable tracing for this route.
306     *
307     * @param tracing whether to use tracing (true or false), the value can be a
308     *            property placeholder
309     * @return the builder
310     */
311    public RouteDefinition tracing(String tracing) {
312        setTrace(tracing);
313        return this;
314    }
315
316    /**
317     * Enable message history for this route.
318     *
319     * @return the builder
320     */
321    public RouteDefinition messageHistory() {
322        setMessageHistory("true");
323        return this;
324    }
325
326    /**
327     * Enable message history for this route.
328     *
329     * @param messageHistory whether to use message history (true or false), the
330     *            value can be a property placeholder
331     * @return the builder
332     */
333    public RouteDefinition messageHistory(String messageHistory) {
334        setMessageHistory(messageHistory);
335        return this;
336    }
337
338    /**
339     * Enable security mask for Logging on this route.
340     *
341     * @return the builder
342     */
343    public RouteDefinition logMask() {
344        setLogMask("true");
345        return this;
346    }
347
348    /**
349     * Sets whether security mask for logging is enabled on this route.
350     *
351     * @param logMask whether to enable security mask for Logging (true or
352     *            false), the value can be a property placeholder
353     * @return the builder
354     */
355    public RouteDefinition logMask(String logMask) {
356        setLogMask(logMask);
357        return this;
358    }
359
360    /**
361     * Disable message history for this route.
362     *
363     * @return the builder
364     */
365    public RouteDefinition noMessageHistory() {
366        setMessageHistory("false");
367        return this;
368    }
369
370    /**
371     * Disable delayer for this route.
372     *
373     * @return the builder
374     */
375    public RouteDefinition noDelayer() {
376        setDelayer("0");
377        return this;
378    }
379
380    /**
381     * Enable delayer for this route.
382     *
383     * @param delay delay in millis
384     * @return the builder
385     */
386    public RouteDefinition delayer(long delay) {
387        setDelayer("" + delay);
388        return this;
389    }
390
391    /**
392     * Installs the given
393     * <a href="http://camel.apache.org/error-handler.html">error handler</a>
394     * builder.
395     *
396     * @param errorHandlerBuilder the error handler to be used by default for
397     *            all child routes
398     * @return the current builder with the error handler configured
399     */
400    public RouteDefinition errorHandler(ErrorHandlerFactory errorHandlerBuilder) {
401        setErrorHandlerFactory(errorHandlerBuilder);
402        // we are now using a route scoped error handler
403        contextScopedErrorHandler = false;
404        return this;
405    }
406
407    /**
408     * Disables this route from being auto started when Camel starts.
409     *
410     * @return the builder
411     */
412    public RouteDefinition noAutoStartup() {
413        setAutoStartup("false");
414        return this;
415    }
416
417    /**
418     * Sets the auto startup property on this route.
419     *
420     * @param autoStartup whether to auto startup (true or false), the value can
421     *            be a property placeholder
422     * @return the builder
423     */
424    public RouteDefinition autoStartup(String autoStartup) {
425        setAutoStartup(autoStartup);
426        return this;
427    }
428
429    /**
430     * Sets the auto startup property on this route.
431     *
432     * @param autoStartup - boolean indicator
433     * @return the builder
434     */
435    public RouteDefinition autoStartup(boolean autoStartup) {
436        setAutoStartup(Boolean.toString(autoStartup));
437        return this;
438    }
439
440    /**
441     * Configures the startup order for this route
442     * <p/>
443     * Camel will reorder routes and star them ordered by 0..N where 0 is the
444     * lowest number and N the highest number. Camel will stop routes in reverse
445     * order when its stopping.
446     *
447     * @param order the order represented as a number
448     * @return the builder
449     */
450    @Override
451    public RouteDefinition startupOrder(int order) {
452        setStartupOrder(order);
453        return this;
454    }
455
456    /**
457     * Configures route policies for this route
458     *
459     * @param policies the route policies
460     * @return the builder
461     */
462    public RouteDefinition routePolicy(RoutePolicy... policies) {
463        if (routePolicies == null) {
464            routePolicies = new ArrayList<>();
465        }
466        routePolicies.addAll(Arrays.asList(policies));
467        return this;
468    }
469
470    /**
471     * Configures route policy for this route
472     *
473     * @param policy route policy
474     * @return the builder
475     */
476    public RouteDefinition routePolicy(Supplier<RoutePolicy> policy) {
477        return routePolicy(policy.get());
478    }
479
480    /**
481     * Configures a route policy for this route
482     *
483     * @param routePolicyRef reference to a {@link RoutePolicy} to lookup and
484     *            use. You can specify multiple references by separating using
485     *            comma.
486     * @return the builder
487     */
488    public RouteDefinition routePolicyRef(String routePolicyRef) {
489        setRoutePolicyRef(routePolicyRef);
490        return this;
491    }
492
493    /**
494     * Configures a shutdown route option.
495     *
496     * @param shutdownRoute the option to use when shutting down this route
497     * @return the builder
498     */
499    public RouteDefinition shutdownRoute(ShutdownRoute shutdownRoute) {
500        return shutdownRoute(shutdownRoute.name());
501    }
502
503    /**
504     * Configures a shutdown route option.
505     *
506     * @param shutdownRoute the option to use when shutting down this route
507     * @return the builder
508     */
509    public RouteDefinition shutdownRoute(String shutdownRoute) {
510        setShutdownRoute(shutdownRoute);
511        return this;
512    }
513
514    /**
515     * Configures a shutdown running task option.
516     *
517     * @param shutdownRunningTask the option to use when shutting down and how
518     *            to act upon running tasks.
519     * @return the builder
520     */
521    public RouteDefinition shutdownRunningTask(ShutdownRunningTask shutdownRunningTask) {
522        return shutdownRunningTask(shutdownRunningTask.name());
523    }
524
525    /**
526     * Configures a shutdown running task option.
527     *
528     * @param shutdownRunningTask the option to use when shutting down and how
529     *            to act upon running tasks.
530     * @return the builder
531     */
532    public RouteDefinition shutdownRunningTask(String shutdownRunningTask) {
533        setShutdownRunningTask(shutdownRunningTask);
534        return this;
535    }
536
537    /**
538     * Declare the expected data type of the input message. If the actual
539     * message type is different at runtime, camel look for a required
540     * {@link org.apache.camel.spi.Transformer} and apply if exists. The type
541     * name consists of two parts, 'scheme' and 'name' connected with ':'. For
542     * Java type 'name' is a fully qualified class name. For example
543     * {@code java:java.lang.String}, {@code json:ABCOrder}.
544     *
545     * @see org.apache.camel.spi.Transformer
546     * @param urn input type URN
547     * @return the builder
548     */
549    public RouteDefinition inputType(String urn) {
550        inputType = new InputTypeDefinition().urn(urn).validate(false);
551        return this;
552    }
553
554    /**
555     * Declare the expected data type of the input message with content
556     * validation enabled. If the actual message type is different at runtime,
557     * camel look for a required {@link org.apache.camel.spi.Transformer} and
558     * apply if exists, and then applies {@link org.apache.camel.spi.Validator}
559     * as well. The type name consists of two parts, 'scheme' and 'name'
560     * connected with ':'. For Java type 'name' is a fully qualified class name.
561     * For example {@code java:java.lang.String}, {@code json:ABCOrder}.
562     *
563     * @see org.apache.camel.spi.Transformer
564     * @see org.apache.camel.spi.Validator
565     * @param urn input type URN
566     * @return the builder
567     */
568    public RouteDefinition inputTypeWithValidate(String urn) {
569        inputType = new InputTypeDefinition().urn(urn).validate(true);
570        return this;
571    }
572
573    /**
574     * Declare the expected data type of the input message by Java class. If the
575     * actual message type is different at runtime, camel look for a required
576     * {@link org.apache.camel.spi.Transformer} and apply if exists.
577     *
578     * @see org.apache.camel.spi.Transformer
579     * @param clazz Class object of the input type
580     * @return the builder
581     */
582    public RouteDefinition inputType(Class clazz) {
583        inputType = new InputTypeDefinition().javaClass(clazz).validate(false);
584        return this;
585    }
586
587    /**
588     * Declare the expected data type of the input message by Java class with
589     * content validation enabled. If the actual message type is different at
590     * runtime, camel look for a required
591     * {@link org.apache.camel.spi.Transformer} and apply if exists, and then
592     * applies {@link org.apache.camel.spi.Validator} as well.
593     *
594     * @see org.apache.camel.spi.Transformer
595     * @see org.apache.camel.spi.Validator
596     * @param clazz Class object of the input type
597     * @return the builder
598     */
599    public RouteDefinition inputTypeWithValidate(Class clazz) {
600        inputType = new InputTypeDefinition().javaClass(clazz).validate(true);
601        return this;
602    }
603
604    /**
605     * Declare the expected data type of the output message. If the actual
606     * message type is different at runtime, camel look for a required
607     * {@link org.apache.camel.spi.Transformer} and apply if exists. The type
608     * name consists of two parts, 'scheme' and 'name' connected with ':'. For
609     * Java type 'name' is a fully qualified class name. For example
610     * {@code java:java.lang.String}, {@code json:ABCOrder}.
611     *
612     * @see org.apache.camel.spi.Transformer
613     * @param urn output type URN
614     * @return the builder
615     */
616    public RouteDefinition outputType(String urn) {
617        outputType = new OutputTypeDefinition().urn(urn).validate(false);
618        return this;
619    }
620
621    /**
622     * Declare the expected data type of the output message with content
623     * validation enabled. If the actual message type is different at runtime,
624     * Camel look for a required {@link org.apache.camel.spi.Transformer} and
625     * apply if exists, and then applies {@link org.apache.camel.spi.Validator}
626     * as well. The type name consists of two parts, 'scheme' and 'name'
627     * connected with ':'. For Java type 'name' is a fully qualified class name.
628     * For example {@code java:java.lang.String}, {@code json:ABCOrder}.
629     * 
630     * @see org.apache.camel.spi.Transformer
631     * @see org.apache.camel.spi.Validator
632     * @param urn output type URN
633     * @return the builder
634     */
635    public RouteDefinition outputTypeWithValidate(String urn) {
636        outputType = new OutputTypeDefinition().urn(urn).validate(true);
637        return this;
638    }
639
640    /**
641     * Declare the expected data type of the output message by Java class. If
642     * the actual message type is different at runtime, camel look for a
643     * required {@link org.apache.camel.spi.Transformer} and apply if exists.
644     *
645     * @see org.apache.camel.spi.Transformer
646     * @param clazz Class object of the output type
647     * @return the builder
648     */
649    public RouteDefinition outputType(Class clazz) {
650        outputType = new OutputTypeDefinition().javaClass(clazz).validate(false);
651        return this;
652    }
653
654    /**
655     * Declare the expected data type of the ouput message by Java class with
656     * content validation enabled. If the actual message type is different at
657     * runtime, camel look for a required
658     * {@link org.apache.camel.spi.Transformer} and apply if exists, and then
659     * applies {@link org.apache.camel.spi.Validator} as well.
660     * 
661     * @see org.apache.camel.spi.Transformer
662     * @see org.apache.camel.spi.Validator
663     * @param clazz Class object of the output type
664     * @return the builder
665     */
666    public RouteDefinition outputTypeWithValidate(Class clazz) {
667        outputType = new OutputTypeDefinition().javaClass(clazz).validate(true);
668        return this;
669    }
670
671    /**
672     * Adds a custom property on the route.
673     */
674    public RouteDefinition routeProperty(String key, String value) {
675        if (routeProperties == null) {
676            routeProperties = new ArrayList<>();
677        }
678
679        PropertyDefinition prop = new PropertyDefinition();
680        prop.setKey(key);
681        prop.setValue(value);
682
683        routeProperties.add(prop);
684
685        return this;
686    }
687
688    // Properties
689    // -----------------------------------------------------------------------
690
691    public FromDefinition getInput() {
692        return input;
693    }
694
695    /**
696     * Input to the route.
697     */
698    @XmlElementRef(required = false)
699    public void setInput(FromDefinition input) {
700        if (this.input != null && input != null && this.input != input) {
701            throw new IllegalArgumentException("Only one input is allowed per route. Cannot accept input: " + input);
702        }
703        // required = false: in rest-dsl you can embed an in-lined route which
704        // does not have a <from> as its implied to be the rest endpoint
705        this.input = input;
706    }
707
708    @Override
709    public List<ProcessorDefinition<?>> getOutputs() {
710        return outputs;
711    }
712
713    /**
714     * Outputs are processors that determines how messages are processed by this
715     * route.
716     */
717    @XmlElementRef
718    @Override
719    public void setOutputs(List<ProcessorDefinition<?>> outputs) {
720        super.setOutputs(outputs);
721    }
722
723    /**
724     * The group that this route belongs to; could be the name of the
725     * RouteBuilder class or be explicitly configured in the XML.
726     * <p/>
727     * May be null.
728     */
729    public String getGroup() {
730        return group;
731    }
732
733    /**
734     * The group that this route belongs to; could be the name of the
735     * RouteBuilder class or be explicitly configured in the XML.
736     * <p/>
737     * May be null.
738     */
739    @XmlAttribute
740    public void setGroup(String group) {
741        this.group = group;
742    }
743
744    /**
745     * Whether stream caching is enabled on this route.
746     */
747    public String getStreamCache() {
748        return streamCache;
749    }
750
751    /**
752     * Whether stream caching is enabled on this route.
753     */
754    @XmlAttribute
755    @Metadata(javaType = "java.lang.Boolean")
756    public void setStreamCache(String streamCache) {
757        this.streamCache = streamCache;
758    }
759
760    /**
761     * Whether tracing is enabled on this route.
762     */
763    public String getTrace() {
764        return trace;
765    }
766
767    /**
768     * Whether tracing is enabled on this route.
769     */
770    @XmlAttribute
771    @Metadata(javaType = "java.lang.Boolean")
772    public void setTrace(String trace) {
773        this.trace = trace;
774    }
775
776    /**
777     * Whether message history is enabled on this route.
778     */
779    public String getMessageHistory() {
780        return messageHistory;
781    }
782
783    /**
784     * Whether message history is enabled on this route.
785     */
786    @XmlAttribute
787    @Metadata(javaType = "java.lang.Boolean", defaultValue = "true")
788    public void setMessageHistory(String messageHistory) {
789        this.messageHistory = messageHistory;
790    }
791
792    /**
793     * Whether security mask for Logging is enabled on this route.
794     */
795    public String getLogMask() {
796        return logMask;
797    }
798
799    /**
800     * Whether security mask for Logging is enabled on this route.
801     */
802    @XmlAttribute
803    @Metadata(javaType = "java.lang.Boolean")
804    public void setLogMask(String logMask) {
805        this.logMask = logMask;
806    }
807
808    /**
809     * Whether to slow down processing messages by a given delay in msec.
810     */
811    public String getDelayer() {
812        return delayer;
813    }
814
815    /**
816     * Whether to slow down processing messages by a given delay in msec.
817     */
818    @XmlAttribute
819    @Metadata(javaType = "java.lang.Long")
820    public void setDelayer(String delayer) {
821        this.delayer = delayer;
822    }
823
824    /**
825     * Whether to auto start this route
826     */
827    public String getAutoStartup() {
828        return autoStartup;
829    }
830
831    /**
832     * Whether to auto start this route
833     */
834    @XmlAttribute
835    @Metadata(javaType = "java.lang.Boolean", defaultValue = "true")
836    public void setAutoStartup(String autoStartup) {
837        this.autoStartup = autoStartup;
838    }
839
840    /**
841     * To configure the ordering of the routes being started
842     */
843    public Integer getStartupOrder() {
844        return startupOrder;
845    }
846
847    /**
848     * To configure the ordering of the routes being started
849     */
850    @XmlAttribute
851    @Metadata(javaType = "java.lang.Integer")
852    public void setStartupOrder(Integer startupOrder) {
853        this.startupOrder = startupOrder;
854    }
855
856    /**
857     * Sets the bean ref name of the error handler builder to use on this route
858     */
859    @XmlAttribute
860    public void setErrorHandlerRef(String errorHandlerRef) {
861        this.errorHandlerRef = errorHandlerRef;
862        // we use an specific error handler ref (from Spring DSL) then wrap that
863        // with a error handler build ref so Camel knows its not just the
864        // default one
865        setErrorHandlerFactory(new ErrorHandlerBuilderRef(errorHandlerRef));
866    }
867
868    /**
869     * Sets the bean ref name of the error handler builder to use on this route
870     */
871    public String getErrorHandlerRef() {
872        return errorHandlerRef;
873    }
874
875    /**
876     * Sets the error handler if one is not already set
877     */
878    @XmlTransient
879    public void setErrorHandlerFactoryIfNull(ErrorHandlerFactory errorHandlerFactory) {
880        if (this.errorHandlerFactory == null) {
881            setErrorHandlerFactory(errorHandlerFactory);
882        }
883    }
884
885    /**
886     * Reference to custom {@link org.apache.camel.spi.RoutePolicy} to use by
887     * the route. Multiple policies can be configured by separating values using
888     * comma.
889     */
890    @XmlAttribute
891    public void setRoutePolicyRef(String routePolicyRef) {
892        this.routePolicyRef = routePolicyRef;
893    }
894
895    /**
896     * Reference to custom {@link org.apache.camel.spi.RoutePolicy} to use by
897     * the route. Multiple policies can be configured by separating values using
898     * comma.
899     */
900    public String getRoutePolicyRef() {
901        return routePolicyRef;
902    }
903
904    public List<RoutePolicy> getRoutePolicies() {
905        return routePolicies;
906    }
907
908    @XmlTransient
909    public void setRoutePolicies(List<RoutePolicy> routePolicies) {
910        this.routePolicies = routePolicies;
911    }
912
913    public String getShutdownRoute() {
914        return shutdownRoute;
915    }
916
917    /**
918     * To control how to shutdown the route.
919     */
920    @XmlAttribute
921    @Metadata(javaType = "org.apache.camel.ShutdownRoute", defaultValue = "Default", enums = "Default,Defer")
922    public void setShutdownRoute(String shutdownRoute) {
923        this.shutdownRoute = shutdownRoute;
924    }
925
926    /**
927     * To control how to shutdown the route.
928     */
929    public String getShutdownRunningTask() {
930        return shutdownRunningTask;
931    }
932
933    /**
934     * To control how to shutdown the route.
935     */
936    @XmlAttribute
937    @Metadata(javaType = "org.apache.camel.ShutdownRunningTask", defaultValue = "CompleteCurrentTaskOnly", enums = "CompleteCurrentTaskOnly,CompleteAllTasks")
938    public void setShutdownRunningTask(String shutdownRunningTask) {
939        this.shutdownRunningTask = shutdownRunningTask;
940    }
941
942    private ErrorHandlerFactory createErrorHandlerBuilder() {
943        if (errorHandlerRef != null) {
944            return new ErrorHandlerBuilderRef(errorHandlerRef);
945        }
946
947        // return a reference to the default error handler
948        return new ErrorHandlerBuilderRef(ErrorHandlerBuilderRef.DEFAULT_ERROR_HANDLER_BUILDER);
949    }
950
951    public ErrorHandlerFactory getErrorHandlerFactory() {
952        if (errorHandlerFactory == null) {
953            errorHandlerFactory = createErrorHandlerBuilder();
954        }
955        return errorHandlerFactory;
956    }
957
958    /**
959     * Sets the error handler to use with processors created by this builder
960     */
961    @XmlTransient
962    public void setErrorHandlerFactory(ErrorHandlerFactory errorHandlerFactory) {
963        this.errorHandlerFactory = errorHandlerFactory;
964    }
965
966    @XmlAttribute
967    public Boolean isRest() {
968        return rest;
969    }
970
971    public RestDefinition getRestDefinition() {
972        return restDefinition;
973    }
974
975    @XmlTransient
976    public void setRestDefinition(RestDefinition restDefinition) {
977        this.restDefinition = restDefinition;
978    }
979
980    public RestBindingDefinition getRestBindingDefinition() {
981        return restBindingDefinition;
982    }
983
984    @XmlTransient
985    public void setRestBindingDefinition(RestBindingDefinition restBindingDefinition) {
986        this.restBindingDefinition = restBindingDefinition;
987    }
988
989    public boolean isContextScopedErrorHandler() {
990        return contextScopedErrorHandler;
991    }
992
993    @XmlElementRef(required = false)
994    public void setInputType(InputTypeDefinition inputType) {
995        this.inputType = inputType;
996    }
997
998    public InputTypeDefinition getInputType() {
999        return this.inputType;
1000    }
1001
1002    @XmlElementRef(required = false)
1003    public void setOutputType(OutputTypeDefinition outputType) {
1004        this.outputType = outputType;
1005    }
1006
1007    public OutputTypeDefinition getOutputType() {
1008        return this.outputType;
1009    }
1010
1011    public List<PropertyDefinition> getRouteProperties() {
1012        return routeProperties;
1013    }
1014
1015    /**
1016     * To set metadata as properties on the route.
1017     */
1018    @XmlElement(name = "routeProperty")
1019    @Metadata(label = "advanced")
1020    public void setRouteProperties(List<PropertyDefinition> routeProperties) {
1021        this.routeProperties = routeProperties;
1022    }
1023
1024    // ****************************
1025    // Static helpers
1026    // ****************************
1027
1028    public static RouteDefinition fromUri(String uri) {
1029        return new RouteDefinition().from(uri);
1030    }
1031
1032    public static RouteDefinition fromEndpoint(Endpoint endpoint) {
1033        return new RouteDefinition().from(endpoint);
1034    }
1035
1036}