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;
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlRootElement;
025
026import org.apache.camel.Expression;
027import org.apache.camel.Processor;
028import org.apache.camel.model.language.ExpressionDefinition;
029import org.apache.camel.processor.DynamicRouter;
030import org.apache.camel.spi.Metadata;
031import org.apache.camel.spi.RouteContext;
032
033/**
034 * Routes messages based on dynamic rules
035 */
036@Metadata(label = "eip,endpoint,routing")
037@XmlRootElement(name = "dynamicRouter")
038@XmlAccessorType(XmlAccessType.FIELD)
039public class DynamicRouterDefinition<Type extends ProcessorDefinition<Type>> extends NoOutputExpressionNode {
040
041    public static final String DEFAULT_DELIMITER = ",";
042
043    @XmlAttribute @Metadata(defaultValue = ",")
044    private String uriDelimiter;
045    @XmlAttribute
046    private Boolean ignoreInvalidEndpoints;
047    @XmlAttribute
048    private Integer cacheSize; 
049
050    public DynamicRouterDefinition() {
051    }
052
053    public DynamicRouterDefinition(Expression expression) {
054        super(expression);
055    }
056
057    @Override
058    public String toString() {
059        return "DynamicRouter[" + getExpression() + "]";
060    }
061    
062    @Override
063    public String getLabel() {
064        return "dynamicRouter[" + getExpression() + "]";
065    }
066
067    @Override
068    public List<ProcessorDefinition<?>> getOutputs() {
069        return Collections.emptyList();
070    }
071
072    @Override
073    public Processor createProcessor(RouteContext routeContext) throws Exception {
074        Expression expression = getExpression().createExpression(routeContext);
075        String delimiter = getUriDelimiter() != null ? getUriDelimiter() : DEFAULT_DELIMITER;
076
077        DynamicRouter dynamicRouter = new DynamicRouter(routeContext.getCamelContext(), expression, delimiter);
078        if (getIgnoreInvalidEndpoints() != null) {
079            dynamicRouter.setIgnoreInvalidEndpoints(getIgnoreInvalidEndpoints());
080        }
081        if (getCacheSize() != null) {
082            dynamicRouter.setCacheSize(getCacheSize());
083        }
084        return dynamicRouter;
085    }
086
087    /**
088     * Expression to call that returns the endpoint(s) to route to in the dynamic routing.
089     * <p/>
090     * <b>Important:</b> The expression will be called in a while loop fashion, until the expression returns <tt>null</tt>
091     * which means the dynamic router is finished.
092     */
093    @Override
094    public void setExpression(ExpressionDefinition expression) {
095        // override to include javadoc what the expression is used for
096        super.setExpression(expression);
097    }
098
099    public void setUriDelimiter(String uriDelimiter) {
100        this.uriDelimiter = uriDelimiter;
101    }
102
103    public String getUriDelimiter() {
104        return uriDelimiter;
105    }
106
107    public void setIgnoreInvalidEndpoints(Boolean ignoreInvalidEndpoints) {
108        this.ignoreInvalidEndpoints = ignoreInvalidEndpoints;
109    }
110
111    public Boolean getIgnoreInvalidEndpoints() {
112        return ignoreInvalidEndpoints;
113    }
114
115    // Fluent API
116    // -------------------------------------------------------------------------
117
118    public Integer getCacheSize() {
119        return cacheSize;
120    }
121
122    public void setCacheSize(Integer cacheSize) {
123        this.cacheSize = cacheSize;
124    }
125
126    @Override
127    @SuppressWarnings("unchecked")
128    public Type end() {
129        // allow end() to return to previous type so you can continue in the DSL
130        return (Type) super.end();
131    }
132
133    /**
134     * Ignore the invalidate endpoint exception when try to create a producer with that endpoint
135     *
136     * @return the builder
137     */
138    public DynamicRouterDefinition<Type> ignoreInvalidEndpoints() {
139        setIgnoreInvalidEndpoints(true);
140        return this;
141    }
142
143    /**
144     * Sets the uri delimiter to use
145     *
146     * @param uriDelimiter the delimiter
147     * @return the builder
148     */
149    public DynamicRouterDefinition<Type> uriDelimiter(String uriDelimiter) {
150        setUriDelimiter(uriDelimiter);
151        return this;
152    }
153    
154    /**
155     * Sets the maximum size used by the {@link org.apache.camel.impl.ProducerCache} which is used
156     * to cache and reuse producers when using this recipient list, when uris are reused.
157     *
158     * @param cacheSize  the cache size, use <tt>0</tt> for default cache size, or <tt>-1</tt> to turn cache off.
159     * @return the builder
160     */
161    public DynamicRouterDefinition<Type> cacheSize(int cacheSize) {
162        setCacheSize(cacheSize);
163        return this;
164    }
165
166}