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