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     */
017    package org.apache.camel.model;
018    
019    import java.util.Collections;
020    import java.util.List;
021    import javax.xml.bind.annotation.XmlAccessType;
022    import javax.xml.bind.annotation.XmlAccessorType;
023    import javax.xml.bind.annotation.XmlAttribute;
024    import javax.xml.bind.annotation.XmlRootElement;
025    
026    import org.apache.camel.Expression;
027    import org.apache.camel.Processor;
028    import org.apache.camel.processor.DynamicRouter;
029    import org.apache.camel.spi.RouteContext;
030    
031    /**
032     * Represents an XML <dynamicRouter/> element
033     */
034    @XmlRootElement(name = "dynamicRouter")
035    @XmlAccessorType(XmlAccessType.FIELD)
036    public class DynamicRouterDefinition<Type extends ProcessorDefinition<Type>> extends NoOutputExpressionNode {
037    
038        public static final String DEFAULT_DELIMITER = ",";
039    
040        @XmlAttribute
041        private String uriDelimiter;
042        @XmlAttribute
043        private Boolean ignoreInvalidEndpoints;
044    
045        public DynamicRouterDefinition() {
046        }
047    
048        public DynamicRouterDefinition(Expression expression) {
049            super(expression);
050        }
051    
052        @Override
053        public String toString() {
054            return "DynamicRouter[" + getExpression() + "]";
055        }
056        
057        @Override
058        public String getLabel() {
059            return "dynamicRouter[" + getExpression() + "]";
060        }
061    
062        @Override
063        public String getShortName() {
064            return "dynamicRouter";
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            return dynamicRouter;
082        }
083    
084        public void setUriDelimiter(String uriDelimiter) {
085            this.uriDelimiter = uriDelimiter;
086        }
087    
088        public String getUriDelimiter() {
089            return uriDelimiter;
090        }
091    
092        public void setIgnoreInvalidEndpoints(Boolean ignoreInvalidEndpoints) {
093            this.ignoreInvalidEndpoints = ignoreInvalidEndpoints;
094        }
095    
096        public Boolean getIgnoreInvalidEndpoints() {
097            return ignoreInvalidEndpoints;
098        }
099    
100        // Fluent API
101        // -------------------------------------------------------------------------
102    
103        @Override
104        @SuppressWarnings("unchecked")
105        public Type end() {
106            // allow end() to return to previous type so you can continue in the DSL
107            return (Type) super.end();
108        }
109    
110        /**
111         * Ignore the invalidate endpoint exception when try to create a producer with that endpoint
112         *
113         * @return the builder
114         */
115        public DynamicRouterDefinition<Type> ignoreInvalidEndpoints() {
116            setIgnoreInvalidEndpoints(true);
117            return this;
118        }
119    
120        /**
121         * Sets the uri delimiter to use
122         *
123         * @param uriDelimiter the delimiter
124         * @return the builder
125         */
126        public DynamicRouterDefinition<Type> uriDelimiter(String uriDelimiter) {
127            setUriDelimiter(uriDelimiter);
128            return this;
129        }
130    
131    }