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