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.Collections;
020import java.util.List;
021
022import javax.xml.bind.annotation.XmlAccessType;
023import javax.xml.bind.annotation.XmlAccessorType;
024import javax.xml.bind.annotation.XmlAttribute;
025import javax.xml.bind.annotation.XmlRootElement;
026
027import org.apache.camel.Expression;
028import org.apache.camel.model.language.ExpressionDefinition;
029import org.apache.camel.model.language.HeaderExpression;
030import org.apache.camel.spi.Metadata;
031
032/**
033 * Routes a message through a series of steps that are pre-determined (the slip)
034 */
035@Metadata(label = "eip,endpoint,routing")
036@XmlRootElement(name = "routingSlip")
037@XmlAccessorType(XmlAccessType.FIELD)
038public class RoutingSlipDefinition<Type extends ProcessorDefinition<Type>> extends ExpressionNode {
039    public static final String DEFAULT_DELIMITER = ",";
040
041    @XmlAttribute
042    @Metadata(defaultValue = ",")
043    private String uriDelimiter;
044    @XmlAttribute
045    private Boolean ignoreInvalidEndpoints;
046    @XmlAttribute
047    private Integer cacheSize;
048
049    public RoutingSlipDefinition() {
050        this((String)null, DEFAULT_DELIMITER);
051    }
052
053    public RoutingSlipDefinition(String headerName) {
054        this(headerName, DEFAULT_DELIMITER);
055    }
056
057    public RoutingSlipDefinition(String headerName, String uriDelimiter) {
058        super(new HeaderExpression(headerName));
059        setUriDelimiter(uriDelimiter);
060    }
061
062    public RoutingSlipDefinition(Expression expression, String uriDelimiter) {
063        super(expression);
064        setUriDelimiter(uriDelimiter);
065    }
066
067    public RoutingSlipDefinition(Expression expression) {
068        this(expression, DEFAULT_DELIMITER);
069    }
070
071    @Override
072    public String toString() {
073        return "RoutingSlip[" + getExpression() + "]";
074    }
075
076    @Override
077    public String getShortName() {
078        return "routingSlip";
079    }
080
081    @Override
082    public String getLabel() {
083        return "routingSlip[" + getExpression() + "]";
084    }
085
086    @Override
087    public List<ProcessorDefinition<?>> getOutputs() {
088        return Collections.emptyList();
089    }
090
091    /**
092     * Expression to define the routing slip, which defines which endpoints to
093     * route the message in a pipeline style. Notice the expression is evaluated
094     * once, if you want a more dynamic style, then the dynamic router eip is a
095     * better choice.
096     */
097    @Override
098    public void setExpression(ExpressionDefinition expression) {
099        // override to include javadoc what the expression is used for
100        super.setExpression(expression);
101    }
102
103    public void setUriDelimiter(String uriDelimiter) {
104        this.uriDelimiter = uriDelimiter;
105    }
106
107    public String getUriDelimiter() {
108        return uriDelimiter;
109    }
110
111    public void setIgnoreInvalidEndpoints(Boolean ignoreInvalidEndpoints) {
112        this.ignoreInvalidEndpoints = ignoreInvalidEndpoints;
113    }
114
115    public Boolean getIgnoreInvalidEndpoints() {
116        return ignoreInvalidEndpoints;
117    }
118
119    public Integer getCacheSize() {
120        return cacheSize;
121    }
122
123    public void setCacheSize(Integer cacheSize) {
124        this.cacheSize = cacheSize;
125    }
126
127    // Fluent API
128    // -------------------------------------------------------------------------
129
130    @Override
131    @SuppressWarnings("unchecked")
132    public Type end() {
133        // allow end() to return to previous type so you can continue in the DSL
134        return (Type)super.end();
135    }
136
137    /**
138     * Ignore the invalidate endpoint exception when try to create a producer
139     * with that endpoint
140     *
141     * @return the builder
142     */
143    public RoutingSlipDefinition<Type> ignoreInvalidEndpoints() {
144        setIgnoreInvalidEndpoints(true);
145        return this;
146    }
147
148    /**
149     * Sets the uri delimiter to use
150     *
151     * @param uriDelimiter the delimiter
152     * @return the builder
153     */
154    public RoutingSlipDefinition<Type> uriDelimiter(String uriDelimiter) {
155        setUriDelimiter(uriDelimiter);
156        return this;
157    }
158
159    /**
160     * Sets the maximum size used by the
161     * {@link org.apache.camel.spi.ProducerCache} which is used to cache and
162     * reuse producers when using this routing slip, when uris are reused.
163     *
164     * @param cacheSize the cache size, use <tt>0</tt> for default cache size,
165     *            or <tt>-1</tt> to turn cache off.
166     * @return the builder
167     */
168    public RoutingSlipDefinition<Type> cacheSize(int cacheSize) {
169        setCacheSize(cacheSize);
170        return this;
171    }
172
173}