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    @Metadata(javaType = "java.lang.Boolean")
046    private String ignoreInvalidEndpoints;
047    @XmlAttribute
048    @Metadata(javaType = "java.lang.Integer")
049    private String cacheSize;
050
051    public RoutingSlipDefinition() {
052        this((String)null, DEFAULT_DELIMITER);
053    }
054
055    public RoutingSlipDefinition(String headerName) {
056        this(headerName, DEFAULT_DELIMITER);
057    }
058
059    public RoutingSlipDefinition(String headerName, String uriDelimiter) {
060        super(new HeaderExpression(headerName));
061        setUriDelimiter(uriDelimiter);
062    }
063
064    public RoutingSlipDefinition(Expression expression, String uriDelimiter) {
065        super(expression);
066        setUriDelimiter(uriDelimiter);
067    }
068
069    public RoutingSlipDefinition(Expression expression) {
070        this(expression, DEFAULT_DELIMITER);
071    }
072
073    @Override
074    public String toString() {
075        return "RoutingSlip[" + getExpression() + "]";
076    }
077
078    @Override
079    public String getShortName() {
080        return "routingSlip";
081    }
082
083    @Override
084    public String getLabel() {
085        return "routingSlip[" + getExpression() + "]";
086    }
087
088    @Override
089    public List<ProcessorDefinition<?>> getOutputs() {
090        return Collections.emptyList();
091    }
092
093    /**
094     * Expression to define the routing slip, which defines which endpoints to
095     * route the message in a pipeline style. Notice the expression is evaluated
096     * once, if you want a more dynamic style, then the dynamic router eip is a
097     * better choice.
098     */
099    @Override
100    public void setExpression(ExpressionDefinition expression) {
101        // override to include javadoc what the expression is used for
102        super.setExpression(expression);
103    }
104
105    public void setUriDelimiter(String uriDelimiter) {
106        this.uriDelimiter = uriDelimiter;
107    }
108
109    public String getUriDelimiter() {
110        return uriDelimiter;
111    }
112
113    public void setIgnoreInvalidEndpoints(String ignoreInvalidEndpoints) {
114        this.ignoreInvalidEndpoints = ignoreInvalidEndpoints;
115    }
116
117    public String getIgnoreInvalidEndpoints() {
118        return ignoreInvalidEndpoints;
119    }
120
121    public String getCacheSize() {
122        return cacheSize;
123    }
124
125    public void setCacheSize(String cacheSize) {
126        this.cacheSize = cacheSize;
127    }
128
129    // Fluent API
130    // -------------------------------------------------------------------------
131
132    @Override
133    @SuppressWarnings("unchecked")
134    public Type end() {
135        // allow end() to return to previous type so you can continue in the DSL
136        return (Type)super.end();
137    }
138
139    /**
140     * Ignore the invalidate endpoint exception when try to create a producer
141     * with that endpoint
142     *
143     * @return the builder
144     */
145    public RoutingSlipDefinition<Type> ignoreInvalidEndpoints() {
146        return ignoreInvalidEndpoints(true);
147    }
148
149    /**
150     * Ignore the invalidate endpoint exception when try to create a producer
151     * with that endpoint
152     *
153     * @return the builder
154     */
155    public RoutingSlipDefinition<Type> ignoreInvalidEndpoints(boolean ignoreInvalidEndpoints) {
156        return ignoreInvalidEndpoints(Boolean.toString(ignoreInvalidEndpoints));
157    }
158
159    /**
160     * Ignore the invalidate endpoint exception when try to create a producer
161     * with that endpoint
162     *
163     * @return the builder
164     */
165    public RoutingSlipDefinition<Type> ignoreInvalidEndpoints(String ignoreInvalidEndpoints) {
166        setIgnoreInvalidEndpoints(ignoreInvalidEndpoints);
167        return this;
168    }
169
170    /**
171     * Sets the uri delimiter to use
172     *
173     * @param uriDelimiter the delimiter
174     * @return the builder
175     */
176    public RoutingSlipDefinition<Type> uriDelimiter(String uriDelimiter) {
177        setUriDelimiter(uriDelimiter);
178        return this;
179    }
180
181    /**
182     * Sets the maximum size used by the
183     * {@link org.apache.camel.spi.ProducerCache} which is used to cache and
184     * reuse producers when using this routing slip, when uris are reused.
185     *
186     * Beware that when using dynamic endpoints then it affects how well the cache can be utilized.
187     * If each dynamic endpoint is unique then its best to turn of caching by setting this to -1, which
188     * allows Camel to not cache both the producers and endpoints; they are regarded as prototype scoped
189     * and will be stopped and discarded after use. This reduces memory usage as otherwise producers/endpoints
190     * are stored in memory in the caches.
191     *
192     * However if there are a high degree of dynamic endpoints that have been used before, then it can
193     * benefit to use the cache to reuse both producers and endpoints and therefore the cache size
194     * can be set accordingly or rely on the default size (1000).
195     *
196     * If there is a mix of unique and used before dynamic endpoints, then setting a reasonable cache size
197     * can help reduce memory usage to avoid storing too many non frequent used producers.
198     *
199     * @param cacheSize the cache size, use <tt>0</tt> for default cache size,
200     *            or <tt>-1</tt> to turn cache off.
201     * @return the builder
202     */
203    public RoutingSlipDefinition<Type> cacheSize(int cacheSize) {
204        return cacheSize(Integer.toString(cacheSize));
205    }
206
207    /**
208     * Sets the maximum size used by the
209     * {@link org.apache.camel.spi.ProducerCache} which is used to cache and
210     * reuse producers when using this routing slip, when uris are reused.
211     *
212     * Beware that when using dynamic endpoints then it affects how well the cache can be utilized.
213     * If each dynamic endpoint is unique then its best to turn of caching by setting this to -1, which
214     * allows Camel to not cache both the producers and endpoints; they are regarded as prototype scoped
215     * and will be stopped and discarded after use. This reduces memory usage as otherwise producers/endpoints
216     * are stored in memory in the caches.
217     *
218     * However if there are a high degree of dynamic endpoints that have been used before, then it can
219     * benefit to use the cache to reuse both producers and endpoints and therefore the cache size
220     * can be set accordingly or rely on the default size (1000).
221     *
222     * If there is a mix of unique and used before dynamic endpoints, then setting a reasonable cache size
223     * can help reduce memory usage to avoid storing too many non frequent used producers.
224     *
225     * @param cacheSize the cache size, use <tt>0</tt> for default cache size,
226     *            or <tt>-1</tt> to turn cache off.
227     * @return the builder
228     */
229    public RoutingSlipDefinition<Type> cacheSize(String cacheSize) {
230        setCacheSize(cacheSize);
231        return this;
232    }
233
234}