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.builder;
018    
019    import java.util.ArrayList;
020    import java.util.Arrays;
021    import java.util.List;
022    
023    import org.apache.camel.CamelContext;
024    import org.apache.camel.Endpoint;
025    import org.apache.camel.Expression;
026    import org.apache.camel.LoggingLevel;
027    import org.apache.camel.NoSuchEndpointException;
028    import org.apache.camel.builder.xml.XPathBuilder;
029    import org.apache.camel.util.ObjectHelper;
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    
033    /**
034     * Base class for implementation inheritance for different clauses in the <a
035     * href="http://camel.apache.org/dsl.html">Java DSL</a>
036     *
037     * @version $Revision: 20189 $
038     */
039    public abstract class BuilderSupport {
040        private CamelContext context;
041        private ErrorHandlerBuilder errorHandlerBuilder;
042    
043        protected BuilderSupport(CamelContext context) {
044            this.context = context;
045        }
046    
047        // Builder methods
048        // -------------------------------------------------------------------------
049    
050        /**
051         * Returns a value builder for the given header
052         */
053        public ValueBuilder header(String name) {
054            return Builder.header(name);
055        }
056    
057        /**
058         * Returns a value builder for the given property
059         */
060        public ValueBuilder property(String name) {
061            return Builder.property(name);
062        }   
063        
064        /**
065         * Returns a predicate and value builder for the inbound body on an exchange
066         */
067        public ValueBuilder body() {
068            return Builder.body();
069        }
070    
071        /**
072         * Returns a predicate and value builder for the inbound message body as a
073         * specific type
074         */
075        public <T> ValueBuilder body(Class<T> type) {
076            return Builder.bodyAs(type);
077        }
078    
079        /**
080         * Returns a predicate and value builder for the outbound body on an
081         * exchange
082         */
083        public ValueBuilder outBody() {
084            return Builder.outBody();
085        }
086    
087        /**
088         * Returns a predicate and value builder for the outbound message body as a
089         * specific type
090         */
091        public <T> ValueBuilder outBody(Class<T> type) {
092            return Builder.outBodyAs(type);
093        }
094    
095        /**
096         * Returns a predicate and value builder for the fault body on an
097         * exchange
098         */
099        public ValueBuilder faultBody() {
100            return Builder.faultBody();
101        }
102    
103        /**
104         * Returns a predicate and value builder for the fault message body as a
105         * specific type
106         */
107        public <T> ValueBuilder faultBodyAs(Class<T> type) {
108            return Builder.faultBodyAs(type);
109        }
110                                 
111        /**
112         * Returns a value builder for the given system property
113         */
114        public ValueBuilder systemProperty(String name) {
115            return Builder.systemProperty(name);
116        }
117    
118        /**
119         * Returns a value builder for the given system property
120         */
121        public ValueBuilder systemProperty(String name, String defaultValue) {
122            return Builder.systemProperty(name, defaultValue);
123        }
124    
125        /**
126         * Returns a constant expression value builder
127         */
128        public ValueBuilder constant(Object value) {
129            return Builder.constant(value);
130        }
131        
132        /**
133         * Returns a simple expression value builder
134         */
135        public SimpleBuilder simple(String value) {
136            return SimpleBuilder.simple(value);
137        }
138        
139        /**
140         * Returns a xpath expression value builder
141         */
142        public XPathBuilder xpath(String value) {
143            return XPathBuilder.xpath(value);
144        }
145    
146        /**
147         * Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a>
148         * value builder
149         * <p/>
150         * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
151         *
152         * @param beanOrBeanRef  either an instanceof a bean or a reference to bean to lookup in the Registry
153         * @return the builder
154         */
155        public ValueBuilder bean(Object beanOrBeanRef) {
156            return bean(beanOrBeanRef, null);
157        }
158        
159        /**
160         * Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a>
161         * value builder
162         * <p/>
163         * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
164         *
165         * @param beanOrBeanRef  either an instanceof a bean or a reference to bean to lookup in the Registry
166         * @param method   name of method to invoke
167         * @return the builder
168         */
169        public ValueBuilder bean(Object beanOrBeanRef, String method) {
170            return Builder.bean(beanOrBeanRef, method);
171        }
172    
173        /**
174         * Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a>
175         * value builder
176         *
177         * @param beanType the Class of the bean which we want to invoke
178         * @return the builder
179         */
180        public ValueBuilder bean(Class<?> beanType) {
181            return Builder.bean(beanType, null);
182        }
183        
184        /**
185         * Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a>
186         * value builder
187         *
188         * @param beanType the Class of the bean which we want to invoke
189         * @param method   name of method to invoke
190         * @return the builder
191         */
192        public ValueBuilder bean(Class<?> beanType, String method) {
193            return Builder.bean(beanType, method);
194        }
195    
196        /**
197         * Returns an expression processing the exchange to the given endpoint uri
198         *
199         * @param uri endpoint uri to send the exchange to
200         * @return the builder
201         */
202        public ValueBuilder sendTo(String uri) {
203            return Builder.sendTo(uri);
204        }
205    
206        /**
207         * Returns an expression value builder that replaces all occurrences of the 
208         * regular expression with the given replacement
209         */
210        public ValueBuilder regexReplaceAll(Expression content, String regex, String replacement) {
211            return Builder.regexReplaceAll(content, regex, replacement);
212        }
213    
214        /**
215         * Returns an expression value builder that replaces all occurrences of the 
216         * regular expression with the given replacement
217         */
218        public ValueBuilder regexReplaceAll(Expression content, String regex, Expression replacement) {
219            return Builder.regexReplaceAll(content, regex, replacement);
220        }    
221        
222        /**
223         * Returns a exception expression value builder
224         */
225        public ValueBuilder exceptionMessage() {
226            return Builder.exceptionMessage();
227        }
228    
229        /**
230         * Resolves the given URI to an endpoint
231         *
232         * @param uri  the uri to resolve
233         * @throws NoSuchEndpointException if the endpoint URI could not be resolved
234         * @return the endpoint
235         */
236        public Endpoint endpoint(String uri) throws NoSuchEndpointException {
237            ObjectHelper.notNull(uri, "uri");
238            Endpoint endpoint = getContext().getEndpoint(uri);
239            if (endpoint == null) {
240                throw new NoSuchEndpointException(uri);
241            }
242            return endpoint;
243        }
244    
245        /**
246         * Resolves the given URI to an endpoint of the specified type
247         *
248         * @param uri  the uri to resolve
249         * @param type the excepted type of the endpoint
250         * @throws NoSuchEndpointException if the endpoint URI could not be resolved
251         * @return the endpoint
252         */
253        public <T extends Endpoint> T endpoint(String uri, Class<T> type) throws NoSuchEndpointException {
254            ObjectHelper.notNull(uri, "uri");
255            T endpoint = getContext().getEndpoint(uri, type);
256            if (endpoint == null) {
257                throw new NoSuchEndpointException(uri);
258            }
259            return endpoint;
260        }
261    
262        /**
263         * Resolves the list of URIs into a list of {@link Endpoint} instances
264         *
265         * @param uris  list of endpoints to resolve
266         * @throws NoSuchEndpointException if an endpoint URI could not be resolved
267         * @return list of endpoints
268         */
269        public List<Endpoint> endpoints(String... uris) throws NoSuchEndpointException {
270            List<Endpoint> endpoints = new ArrayList<Endpoint>();
271            for (String uri : uris) {
272                endpoints.add(endpoint(uri));
273            }
274            return endpoints;
275        }
276    
277        /**
278         * Helper method to create a list of {@link Endpoint} instances
279         *
280         * @param endpoints  endpoints
281         * @return list of the given endpoints
282         */
283        public List<Endpoint> endpoints(Endpoint... endpoints) {
284            List<Endpoint> answer = new ArrayList<Endpoint>();
285            answer.addAll(Arrays.asList(endpoints));
286            return answer;
287        }
288    
289        /**
290         * Creates a default <a href="http://camel.apache.org/error-handler.html">error handler</a>.
291         *
292         * @return the builder
293         */
294        public DefaultErrorHandlerBuilder defaultErrorHandler() {
295            return new DefaultErrorHandlerBuilder();
296        }
297    
298        /**
299         * Creates a disabled <a href="http://camel.apache.org/error-handler.html">error handler</a>
300         * for removing the default error handler
301         *
302         * @return the builder
303         */
304        public NoErrorHandlerBuilder noErrorHandler() {
305            return new NoErrorHandlerBuilder();
306        }
307    
308        /**
309         * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a>
310         * which just logs errors
311         *
312         * @return the builder
313         */
314        public LoggingErrorHandlerBuilder loggingErrorHandler() {
315            return new LoggingErrorHandlerBuilder();
316        }
317    
318        /**
319         * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a>
320         * which just logs errors
321         *
322         * @return the builder
323         */
324        public LoggingErrorHandlerBuilder loggingErrorHandler(String log) {
325            return loggingErrorHandler(LogFactory.getLog(log));
326        }
327    
328        /**
329         * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a>
330         * which just logs errors
331         *
332         * @return the builder
333         */
334        public LoggingErrorHandlerBuilder loggingErrorHandler(Log log) {
335            return new LoggingErrorHandlerBuilder(log);
336        }
337    
338        /**
339         * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a>
340         * which just logs errors
341         *
342         * @return the builder
343         */
344        public LoggingErrorHandlerBuilder loggingErrorHandler(Log log, LoggingLevel level) {
345            return new LoggingErrorHandlerBuilder(log, level);
346        }
347    
348        /**
349         * <a href="http://camel.apache.org/dead-letter-channel.html">Dead Letter Channel EIP:</a>
350         * is a error handler for handling messages that could not be delivered to it's intended destination.
351         *
352         * @param deadLetterUri  uri to the dead letter endpoint storing dead messages
353         * @return the builder
354         */
355        public DeadLetterChannelBuilder deadLetterChannel(String deadLetterUri) {
356            return deadLetterChannel(endpoint(deadLetterUri));
357        }
358    
359        /**
360         * <a href="http://camel.apache.org/dead-letter-channel.html">Dead Letter Channel EIP:</a>
361         * is a error handler for handling messages that could not be delivered to it's intended destination.
362         *
363         * @param deadLetterEndpoint  dead letter endpoint storing dead messages
364         * @return the builder
365         */
366        public DeadLetterChannelBuilder deadLetterChannel(Endpoint deadLetterEndpoint) {
367            return new DeadLetterChannelBuilder(deadLetterEndpoint);
368        }
369    
370        // Properties
371        // -------------------------------------------------------------------------
372    
373        public CamelContext getContext() {
374            return context;
375        }
376    
377        public void setContext(CamelContext context) {
378            this.context = context;
379        }
380    
381        public ErrorHandlerBuilder getErrorHandlerBuilder() {
382            if (errorHandlerBuilder == null) {
383                errorHandlerBuilder = createErrorHandlerBuilder();
384            }
385            return errorHandlerBuilder;
386        }
387    
388        protected ErrorHandlerBuilder createErrorHandlerBuilder() {
389            return new DefaultErrorHandlerBuilder();
390        }
391    
392        /**
393         * Sets the error handler to use with processors created by this builder
394         */
395        public void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder) {
396            this.errorHandlerBuilder = errorHandlerBuilder;
397        }
398    
399    }