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.NoSuchEndpointException;
027    import org.apache.camel.model.LoggingLevel;
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://activemq.apache.org/camel/dsl.html">Java DSL</a>
036     *
037     * @version $Revision: 13811 $
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 predicate and value builder for the inbound body on an exchange
068         */
069        public ValueBuilder body() {
070            return Builder.body();
071        }
072    
073        /**
074         * Returns a predicate and value builder for the inbound message body as a
075         * specific type
076         */
077        public <T> ValueBuilder body(Class<T> type) {
078            return Builder.bodyAs(type);
079        }
080    
081        /**
082         * Returns a predicate and value builder for the outbound body on an
083         * exchange
084         */
085        public ValueBuilder outBody() {
086            return Builder.outBody();
087        }
088    
089        /**
090         * Returns a predicate and value builder for the outbound message body as a
091         * specific type
092         */
093        public <T> ValueBuilder outBody(Class<T> type) {
094            return Builder.outBodyAs(type);
095        }
096    
097        /**
098         * Returns a predicate and value builder for the fault body on an
099         * exchange
100         */
101        public ValueBuilder faultBody() {
102            return Builder.faultBody();
103        }
104    
105        /**
106         * Returns a predicate and value builder for the fault message body as a
107         * specific type
108         */
109        public <T> ValueBuilder faultBodyAs(Class<T> type) {
110            return Builder.faultBodyAs(type);
111        }
112    
113        /**
114         * Returns a value builder for the given property
115         */
116        public ValueBuilder property(String name) {
117            return Builder.property(name);
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 an expression value builder that replaces all occurrences of the
143         * regular expression with the given replacement
144         */
145        public ValueBuilder regexReplaceAll(Expression content, String regex, String replacement) {
146            return Builder.regexReplaceAll(content, regex, replacement);
147        }
148    
149        /**
150         * Returns an expression value builder that replaces all occurrences of the
151         * regular expression with the given replacement
152         */
153        public ValueBuilder regexReplaceAll(Expression content, String regex, Expression replacement) {
154            return Builder.regexReplaceAll(content, regex, replacement);
155        }    
156    
157        /**
158         * Resolves the given URI to an endpoint
159         *
160         * @throws NoSuchEndpointException if the endpoint URI could not be resolved
161         */
162        public Endpoint endpoint(String uri) throws NoSuchEndpointException {
163            ObjectHelper.notNull(uri, "uri");
164            Endpoint endpoint = getContext().getEndpoint(uri);
165            if (endpoint == null) {
166                throw new NoSuchEndpointException(uri);
167            }
168            return endpoint;
169        }
170    
171        /**
172         * Resolves the given URI to an endpoint of the specified type
173         *
174         * @throws NoSuchEndpointException if the endpoint URI could not be resolved
175         */
176        public <T extends Endpoint> T endpoint(String uri, Class<T> type) throws NoSuchEndpointException {
177            ObjectHelper.notNull(uri, "uri");
178            T endpoint = getContext().getEndpoint(uri, type);
179            if (endpoint == null) {
180                throw new NoSuchEndpointException(uri);
181            }
182            return endpoint;
183        }
184    
185        /**
186         * Resolves the list of URIs into a list of {@link Endpoint} instances
187         *
188         * @throws NoSuchEndpointException if an endpoint URI could not be resolved
189         */
190        public List<Endpoint> endpoints(String... uris) throws NoSuchEndpointException {
191            List<Endpoint> endpoints = new ArrayList<Endpoint>();
192            for (String uri : uris) {
193                endpoints.add(endpoint(uri));
194            }
195            return endpoints;
196        }
197    
198        /**
199         * Helper method to create a list of {@link Endpoint} instances
200         */
201        public List<Endpoint> endpoints(Endpoint... endpoints) {
202            List<Endpoint> answer = new ArrayList<Endpoint>();
203            answer.addAll(Arrays.asList(endpoints));
204            return answer;
205        }
206    
207        /**
208         * Creates a disabled error handler for removing the default error handler
209         */
210        public NoErrorHandlerBuilder noErrorHandler() {
211            return new NoErrorHandlerBuilder();
212        }
213    
214        /**
215         * Creates an error handler which just logs errors
216         */
217        public LoggingErrorHandlerBuilder loggingErrorHandler() {
218            return new LoggingErrorHandlerBuilder();
219        }
220    
221        /**
222         * Creates an error handler which just logs errors
223         */
224        public LoggingErrorHandlerBuilder loggingErrorHandler(String log) {
225            return loggingErrorHandler(LogFactory.getLog(log));
226        }
227    
228        /**
229         * Creates an error handler which just logs errors
230         */
231        public LoggingErrorHandlerBuilder loggingErrorHandler(Log log) {
232            return new LoggingErrorHandlerBuilder(log);
233        }
234    
235        /**
236         * Creates an error handler which just logs errors
237         */
238        public LoggingErrorHandlerBuilder loggingErrorHandler(Log log, LoggingLevel level) {
239            return new LoggingErrorHandlerBuilder(log, level);
240        }
241    
242        public DeadLetterChannelBuilder deadLetterChannel() {
243            return new DeadLetterChannelBuilder();
244        }
245    
246        public DeadLetterChannelBuilder deadLetterChannel(String deadLetterUri) {
247            return deadLetterChannel(endpoint(deadLetterUri));
248        }
249    
250        public DeadLetterChannelBuilder deadLetterChannel(Endpoint deadLetterEndpoint) {
251            return new DeadLetterChannelBuilder(new SendProcessor(deadLetterEndpoint));
252        }
253    
254        // Properties
255        // -------------------------------------------------------------------------
256        public CamelContext getContext() {
257            return context;
258        }
259    
260        public void setContext(CamelContext context) {
261            this.context = context;
262        }
263    
264        public ErrorHandlerBuilder getErrorHandlerBuilder() {
265            if (errorHandlerBuilder == null) {
266                errorHandlerBuilder = createErrorHandlerBuilder();
267            }
268            return errorHandlerBuilder;
269        }
270    
271        protected ErrorHandlerBuilder createErrorHandlerBuilder() {
272            if (isInheritErrorHandler()) {
273                return new DeadLetterChannelBuilder();
274            } else {
275                return new NoErrorHandlerBuilder();
276            }
277        }
278    
279        /**
280         * Sets the error handler to use with processors created by this builder
281         */
282        public void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder) {
283            this.errorHandlerBuilder = errorHandlerBuilder;
284        }
285    
286        public boolean isInheritErrorHandler() {
287            return inheritErrorHandler;
288        }
289    
290        public void setInheritErrorHandler(boolean inheritErrorHandler) {
291            this.inheritErrorHandler = inheritErrorHandler;
292        }
293    }