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 javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023import javax.xml.bind.annotation.XmlTransient;
024
025import org.apache.camel.ExchangePattern;
026import org.apache.camel.builder.EndpointProducerBuilder;
027import org.apache.camel.spi.AsEndpointUri;
028import org.apache.camel.spi.Metadata;
029
030/**
031 * Sends the message to a dynamic endpoint
032 * <p/>
033 * You can specify multiple languages in the uri separated by the plus sign,
034 * such as <tt>mock:+language:xpath:/order/@uri</tt> where <tt>mock:</tt> would
035 * be a prefix to a xpath expression.
036 * <p/>
037 * For more dynamic behavior use
038 * <a href="http://camel.apache.org/recipient-list.html">Recipient List</a> or
039 * <a href="http://camel.apache.org/dynamic-router.html">Dynamic Router</a> EIP
040 * instead.
041 */
042@Metadata(label = "eip,endpoint,routing")
043@XmlRootElement(name = "toD")
044@XmlAccessorType(XmlAccessType.FIELD)
045public class ToDynamicDefinition extends NoOutputDefinition<ToDynamicDefinition> {
046
047    @XmlTransient
048    protected EndpointProducerBuilder endpointProducerBuilder;
049    @XmlAttribute
050    @Metadata(required = true)
051    private String uri;
052    @XmlAttribute
053    @Metadata(javaType = "org.apache.camel.ExchangePattern", enums = "InOnly,InOut,InOptionalOut")
054    private String pattern;
055    @XmlAttribute
056    @Metadata(javaType = "java.lang.Integer")
057    private String cacheSize;
058    @XmlAttribute
059    @Metadata(javaType = "java.lang.Boolean")
060    private String ignoreInvalidEndpoint;
061    @XmlAttribute
062    @Metadata(defaultValue = "true", javaType = "java.lang.Boolean")
063    private String allowOptimisedComponents;
064    @XmlAttribute
065    @Metadata(label = "advanced", defaultValue = "true", javaType = "java.lang.Boolean")
066    private String autoStartComponents;
067
068    public ToDynamicDefinition() {
069    }
070
071    public ToDynamicDefinition(String uri) {
072        this.uri = uri;
073    }
074
075    @Override
076    public String getShortName() {
077        return "toD";
078    }
079
080    @Override
081    public String toString() {
082        return "DynamicTo[" + getLabel() + "]";
083    }
084
085    @Override
086    public String getLabel() {
087        return uri;
088    }
089
090    // Fluent API
091    // -------------------------------------------------------------------------
092
093    /**
094     * The uri of the endpoint to send to. The uri can be dynamic computed using
095     * the {@link org.apache.camel.language.simple.SimpleLanguage} expression.
096     */
097    public ToDynamicDefinition uri(@AsEndpointUri String uri) {
098        setUri(uri);
099        return this;
100    }
101
102    /**
103     * The uri of the endpoint to send to.
104     *
105     * @param endpointProducerBuilder the dynamic endpoint to send to (resolved
106     *            using simple language by default)
107     */
108    public ToDynamicDefinition uri(@AsEndpointUri EndpointProducerBuilder endpointProducerBuilder) {
109        setEndpointProducerBuilder(endpointProducerBuilder);
110        return this;
111    }
112
113    /**
114     * Sets the optional {@link ExchangePattern} used to invoke this endpoint
115     */
116    public ToDynamicDefinition pattern(ExchangePattern pattern) {
117        return pattern(pattern.name());
118    }
119
120    /**
121     * Sets the optional {@link ExchangePattern} used to invoke this endpoint
122     */
123    public ToDynamicDefinition pattern(String pattern) {
124        setPattern(pattern);
125        return this;
126    }
127
128    /**
129     * Sets the maximum size used by the
130     * {@link org.apache.camel.spi.ProducerCache} which is used to cache and
131     * reuse producers when using this recipient list, when uris are reused.
132     *
133     * Beware that when using dynamic endpoints then it affects how well the cache can be utilized.
134     * If each dynamic endpoint is unique then its best to turn of caching by setting this to -1, which
135     * allows Camel to not cache both the producers and endpoints; they are regarded as prototype scoped
136     * and will be stopped and discarded after use. This reduces memory usage as otherwise producers/endpoints
137     * are stored in memory in the caches.
138     *
139     * However if there are a high degree of dynamic endpoints that have been used before, then it can
140     * benefit to use the cache to reuse both producers and endpoints and therefore the cache size
141     * can be set accordingly or rely on the default size (1000).
142     *
143     * If there is a mix of unique and used before dynamic endpoints, then setting a reasonable cache size
144     * can help reduce memory usage to avoid storing too many non frequent used producers.
145     *
146     * @param cacheSize the cache size, use <tt>0</tt> for default cache size,
147     *            or <tt>-1</tt> to turn cache off.
148     * @return the builder
149     */
150    public ToDynamicDefinition cacheSize(int cacheSize) {
151        return cacheSize(Integer.toString(cacheSize));
152    }
153
154    /**
155     * Sets the maximum size used by the
156     * {@link org.apache.camel.spi.ProducerCache} which is used to cache and
157     * reuse producers when using this recipient list, when uris are reused.
158     *
159     * Beware that when using dynamic endpoints then it affects how well the cache can be utilized.
160     * If each dynamic endpoint is unique then its best to turn of caching by setting this to -1, which
161     * allows Camel to not cache both the producers and endpoints; they are regarded as prototype scoped
162     * and will be stopped and discarded after use. This reduces memory usage as otherwise producers/endpoints
163     * are stored in memory in the caches.
164     *
165     * However if there are a high degree of dynamic endpoints that have been used before, then it can
166     * benefit to use the cache to reuse both producers and endpoints and therefore the cache size
167     * can be set accordingly or rely on the default size (1000).
168     *
169     * If there is a mix of unique and used before dynamic endpoints, then setting a reasonable cache size
170     * can help reduce memory usage to avoid storing too many non frequent used producers.
171     *
172     * @param cacheSize the cache size, use <tt>0</tt> for default cache size,
173     *            or <tt>-1</tt> to turn cache off.
174     * @return the builder
175     */
176    public ToDynamicDefinition cacheSize(String cacheSize) {
177        setCacheSize(cacheSize);
178        return this;
179    }
180
181    /**
182     * Ignore the invalidate endpoint exception when try to create a producer
183     * with that endpoint
184     *
185     * @return the builder
186     */
187    public ToDynamicDefinition ignoreInvalidEndpoint(boolean ignoreInvalidEndpoint) {
188        return ignoreInvalidEndpoint(Boolean.toString(ignoreInvalidEndpoint));
189    }
190
191    /**
192     * Ignore the invalidate endpoint exception when try to create a producer
193     * with that endpoint
194     *
195     * @return the builder
196     */
197    public ToDynamicDefinition ignoreInvalidEndpoint(String ignoreInvalidEndpoint) {
198        setIgnoreInvalidEndpoint(ignoreInvalidEndpoint);
199        return this;
200    }
201
202    /**
203     * Whether to allow components to optimise toD if they are
204     * {@link org.apache.camel.spi.SendDynamicAware}.
205     *
206     * @return the builder
207     */
208    public ToDynamicDefinition allowOptimisedComponents() {
209        return allowOptimisedComponents(true);
210    }
211
212    /**
213     * Whether to allow components to optimise toD if they are
214     * {@link org.apache.camel.spi.SendDynamicAware}.
215     *
216     * @return the builder
217     */
218    public ToDynamicDefinition allowOptimisedComponents(boolean allowOptimisedComponents) {
219        return allowOptimisedComponents(Boolean.toString(allowOptimisedComponents));
220    }
221
222    /**
223     * Whether to allow components to optimise toD if they are
224     * {@link org.apache.camel.spi.SendDynamicAware}.
225     *
226     * @return the builder
227     */
228    public ToDynamicDefinition allowOptimisedComponents(String allowOptimisedComponents) {
229        setAllowOptimisedComponents(allowOptimisedComponents);
230        return this;
231    }
232
233    /**
234     * Whether to auto startup components when toD is starting up.
235     *
236     * @return the builder
237     */
238    public ToDynamicDefinition autoStartComponents(String autoStartComponents) {
239        setAutoStartComponents(autoStartComponents);
240        return this;
241    }
242
243    // Properties
244    // -------------------------------------------------------------------------
245
246    public String getUri() {
247        return uri;
248    }
249
250    /**
251     * The uri of the endpoint to send to. The uri can be dynamic computed using
252     * the {@link org.apache.camel.language.simple.SimpleLanguage} expression.
253     */
254    public void setUri(String uri) {
255        this.uri = uri;
256    }
257
258    public EndpointProducerBuilder getEndpointProducerBuilder() {
259        return endpointProducerBuilder;
260    }
261
262    public void setEndpointProducerBuilder(EndpointProducerBuilder endpointProducerBuilder) {
263        this.endpointProducerBuilder = endpointProducerBuilder;
264    }
265
266    public String getPattern() {
267        return pattern;
268    }
269
270    public void setPattern(String pattern) {
271        this.pattern = pattern;
272    }
273
274    public String getCacheSize() {
275        return cacheSize;
276    }
277
278    public void setCacheSize(String cacheSize) {
279        this.cacheSize = cacheSize;
280    }
281
282    public String getIgnoreInvalidEndpoint() {
283        return ignoreInvalidEndpoint;
284    }
285
286    public void setIgnoreInvalidEndpoint(String ignoreInvalidEndpoint) {
287        this.ignoreInvalidEndpoint = ignoreInvalidEndpoint;
288    }
289
290    public String getAllowOptimisedComponents() {
291        return allowOptimisedComponents;
292    }
293
294    public void setAllowOptimisedComponents(String allowOptimisedComponents) {
295        this.allowOptimisedComponents = allowOptimisedComponents;
296    }
297
298    public String getAutoStartComponents() {
299        return autoStartComponents;
300    }
301
302    public void setAutoStartComponents(String autoStartComponents) {
303        this.autoStartComponents = autoStartComponents;
304    }
305}