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.Metadata;
028
029/**
030 * Sends the message to a dynamic endpoint
031 * <p/>
032 * You can specify multiple languages in the uri separated by the plus sign,
033 * such as <tt>mock:+language:xpath:/order/@uri</tt> where <tt>mock:</tt> would
034 * be a prefix to a xpath expression.
035 * <p/>
036 * For more dynamic behavior use
037 * <a href="http://camel.apache.org/recipient-list.html">Recipient List</a> or
038 * <a href="http://camel.apache.org/dynamic-router.html">Dynamic Router</a> EIP
039 * instead.
040 */
041@Metadata(label = "eip,endpoint,routing")
042@XmlRootElement(name = "toD")
043@XmlAccessorType(XmlAccessType.FIELD)
044public class ToDynamicDefinition extends NoOutputDefinition<ToDynamicDefinition> {
045
046    @XmlTransient
047    protected EndpointProducerBuilder endpointProducerBuilder;
048    @XmlAttribute
049    @Metadata(required = true)
050    private String uri;
051    @XmlAttribute
052    @Metadata(javaType = "org.apache.camel.ExchangePattern", enums = "InOnly,InOut,InOptionalOut")
053    private String pattern;
054    @XmlAttribute
055    @Metadata(javaType = "java.lang.Integer")
056    private String cacheSize;
057    @XmlAttribute
058    @Metadata(javaType = "java.lang.Boolean")
059    private String ignoreInvalidEndpoint;
060    @XmlAttribute
061    @Metadata(defaultValue = "true", javaType = "java.lang.Boolean")
062    private String allowOptimisedComponents;
063
064    public ToDynamicDefinition() {
065    }
066
067    public ToDynamicDefinition(String uri) {
068        this.uri = uri;
069    }
070
071    @Override
072    public String getShortName() {
073        return "toD";
074    }
075
076    @Override
077    public String toString() {
078        return "DynamicTo[" + getLabel() + "]";
079    }
080
081    @Override
082    public String getLabel() {
083        return uri;
084    }
085
086    // Fluent API
087    // -------------------------------------------------------------------------
088
089    /**
090     * Sets the optional {@link ExchangePattern} used to invoke this endpoint
091     */
092    public ToDynamicDefinition pattern(ExchangePattern pattern) {
093        return pattern(pattern.name());
094    }
095
096    /**
097     * Sets the optional {@link ExchangePattern} used to invoke this endpoint
098     */
099    public ToDynamicDefinition pattern(String pattern) {
100        setPattern(pattern);
101        return this;
102    }
103
104    /**
105     * Sets the maximum size used by the
106     * {@link org.apache.camel.spi.ConsumerCache} which is used to cache and
107     * reuse producers.
108     *
109     * @param cacheSize the cache size, use <tt>0</tt> for default cache size,
110     *            or <tt>-1</tt> to turn cache off.
111     * @return the builder
112     */
113    public ToDynamicDefinition cacheSize(int cacheSize) {
114        return cacheSize(Integer.toString(cacheSize));
115    }
116
117    /**
118     * Sets the maximum size used by the
119     * {@link org.apache.camel.spi.ConsumerCache} which is used to cache and
120     * reuse producers.
121     *
122     * @param cacheSize the cache size, use <tt>0</tt> for default cache size,
123     *            or <tt>-1</tt> to turn cache off.
124     * @return the builder
125     */
126    public ToDynamicDefinition cacheSize(String cacheSize) {
127        setCacheSize(cacheSize);
128        return this;
129    }
130
131    /**
132     * Ignore the invalidate endpoint exception when try to create a producer
133     * with that endpoint
134     *
135     * @return the builder
136     */
137    public ToDynamicDefinition ignoreInvalidEndpoint(boolean ignoreInvalidEndpoint) {
138        return ignoreInvalidEndpoint(Boolean.toString(ignoreInvalidEndpoint));
139    }
140
141    /**
142     * Ignore the invalidate endpoint exception when try to create a producer
143     * with that endpoint
144     *
145     * @return the builder
146     */
147    public ToDynamicDefinition ignoreInvalidEndpoint(String ignoreInvalidEndpoint) {
148        setIgnoreInvalidEndpoint(ignoreInvalidEndpoint);
149        return this;
150    }
151
152    /**
153     * Whether to allow components to optimise toD if they are
154     * {@link org.apache.camel.spi.SendDynamicAware}.
155     *
156     * @return the builder
157     */
158    public ToDynamicDefinition allowOptimisedComponents() {
159        return allowOptimisedComponents(true);
160    }
161
162    /**
163     * Whether to allow components to optimise toD if they are
164     * {@link org.apache.camel.spi.SendDynamicAware}.
165     *
166     * @return the builder
167     */
168    public ToDynamicDefinition allowOptimisedComponents(boolean allowOptimisedComponents) {
169        return allowOptimisedComponents(Boolean.toString(allowOptimisedComponents));
170    }
171
172    /**
173     * Whether to allow components to optimise toD if they are
174     * {@link org.apache.camel.spi.SendDynamicAware}.
175     *
176     * @return the builder
177     */
178    public ToDynamicDefinition allowOptimisedComponents(String allowOptimisedComponents) {
179        setAllowOptimisedComponents(allowOptimisedComponents);
180        return this;
181    }
182
183    // Properties
184    // -------------------------------------------------------------------------
185
186    public String getUri() {
187        return uri;
188    }
189
190    /**
191     * The uri of the endpoint to send to. The uri can be dynamic computed using
192     * the {@link org.apache.camel.language.simple.SimpleLanguage} expression.
193     */
194    public void setUri(String uri) {
195        this.uri = uri;
196    }
197
198    public EndpointProducerBuilder getEndpointProducerBuilder() {
199        return endpointProducerBuilder;
200    }
201
202    public void setEndpointProducerBuilder(EndpointProducerBuilder endpointProducerBuilder) {
203        this.endpointProducerBuilder = endpointProducerBuilder;
204    }
205
206    public String getPattern() {
207        return pattern;
208    }
209
210    public void setPattern(String pattern) {
211        this.pattern = pattern;
212    }
213
214    public String getCacheSize() {
215        return cacheSize;
216    }
217
218    public void setCacheSize(String cacheSize) {
219        this.cacheSize = cacheSize;
220    }
221
222    public String getIgnoreInvalidEndpoint() {
223        return ignoreInvalidEndpoint;
224    }
225
226    public void setIgnoreInvalidEndpoint(String ignoreInvalidEndpoint) {
227        this.ignoreInvalidEndpoint = ignoreInvalidEndpoint;
228    }
229
230    public String getAllowOptimisedComponents() {
231        return allowOptimisedComponents;
232    }
233
234    public void setAllowOptimisedComponents(String allowOptimisedComponents) {
235        this.allowOptimisedComponents = allowOptimisedComponents;
236    }
237
238}