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