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