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.NoSuchEndpointException;
026    import org.apache.camel.processor.LoggingLevel;
027    import org.apache.camel.processor.SendProcessor;
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://activemq.apache.org/camel/dsl.html">Java DSL</a>
035     *
036     * @version $Revision: 45030 $
037     */
038    public abstract class BuilderSupport {
039        private CamelContext context;
040        private ErrorHandlerBuilder errorHandlerBuilder;
041        private boolean inheritErrorHandler = true;
042    
043        protected BuilderSupport(CamelContext context) {
044            this.context = context;
045        }
046    
047        protected BuilderSupport(BuilderSupport parent) {
048            this.context = parent.getContext();
049            this.inheritErrorHandler = parent.inheritErrorHandler;
050            if (inheritErrorHandler && parent.errorHandlerBuilder != null) {
051                this.errorHandlerBuilder = parent.errorHandlerBuilder.copy();
052            }
053        }
054    
055        // Builder methods
056        // -------------------------------------------------------------------------
057    
058        /**
059         * Returns a value builder for the given header
060         */
061        public ValueBuilder header(String name) {
062            return Builder.header(name);
063        }
064    
065        /**
066         * Returns a predicate and value builder for the inbound body on an exchange
067         */
068        public ValueBuilder body() {
069            return Builder.body();
070        }
071    
072        /**
073         * Returns a predicate and value builder for the inbound message body as a
074         * specific type
075         */
076        public <T> ValueBuilder body(Class<T> type) {
077            return Builder.bodyAs(type);
078        }
079    
080        /**
081         * Returns a predicate and value builder for the outbound body on an
082         * exchange
083         */
084        public ValueBuilder outBody() {
085            return Builder.outBody();
086        }
087    
088        /**
089         * Returns a predicate and value builder for the outbound message body as a
090         * specific type
091         */
092        public <T> ValueBuilder outBody(Class<T> type) {
093            return Builder.outBodyAs(type);
094        }
095    
096        /**
097         * Returns a predicate and value builder for the fault body on an
098         * exchange
099         */
100        public ValueBuilder faultBody() {
101            return Builder.faultBody();
102        }
103    
104        /**
105         * Returns a predicate and value builder for the fault message body as a
106         * specific type
107         */
108        public <T> ValueBuilder faultBodyAs(Class<T> type) {
109            return Builder.faultBodyAs(type);
110        }
111    
112    
113        /**
114         * Returns a value builder for the given system property
115         */
116        public ValueBuilder systemProperty(String name) {
117            return Builder.systemProperty(name);
118        }
119    
120        /**
121         * Returns a value builder for the given system property
122         */
123        public ValueBuilder systemProperty(String name, String defaultValue) {
124            return Builder.systemProperty(name, defaultValue);
125        }
126    
127        /**
128         * Returns a constant expression value builder
129         */
130        public ValueBuilder constant(Object value) {
131            return Builder.constant(value);
132        }
133    
134        /**
135         * Resolves the given URI to an endpoint
136         *
137         * @throws NoSuchEndpointException if the endpoint URI could not be resolved
138         */
139        public Endpoint endpoint(String uri) throws NoSuchEndpointException {
140            ObjectHelper.notNull(uri, "uri");
141            Endpoint endpoint = getContext().getEndpoint(uri);
142            if (endpoint == null) {
143                throw new NoSuchEndpointException(uri);
144            }
145            return endpoint;
146        }
147    
148        /**
149         * Resolves the given URI to an endpoint of the specified type
150         *
151         * @throws NoSuchEndpointException if the endpoint URI could not be resolved
152         */
153        public <T extends Endpoint> T endpoint(String uri, Class<T> type) throws NoSuchEndpointException {
154            ObjectHelper.notNull(uri, "uri");
155            T endpoint = getContext().getEndpoint(uri, type);
156            if (endpoint == null) {
157                throw new NoSuchEndpointException(uri);
158            }
159            return endpoint;
160        }
161    
162        /**
163         * Resolves the list of URIs into a list of {@link Endpoint} instances
164         *
165         * @throws NoSuchEndpointException if an endpoint URI could not be resolved
166         */
167        public List<Endpoint> endpoints(String... uris) throws NoSuchEndpointException {
168            List<Endpoint> endpoints = new ArrayList<Endpoint>();
169            for (String uri : uris) {
170                endpoints.add(endpoint(uri));
171            }
172            return endpoints;
173        }
174    
175        /**
176         * Helper method to create a list of {@link Endpoint} instances
177         */
178        public List<Endpoint> endpoints(Endpoint... endpoints) {
179            List<Endpoint> answer = new ArrayList<Endpoint>();
180            answer.addAll(Arrays.asList(endpoints));
181            return answer;
182        }
183    
184        /**
185         * Creates a disabled error handler for removing the default error handler
186         */
187        public NoErrorHandlerBuilder noErrorHandler() {
188            return new NoErrorHandlerBuilder();
189        }
190    
191        /**
192         * Creates an error handler which just logs errors
193         */
194        public LoggingErrorHandlerBuilder loggingErrorHandler() {
195            return new LoggingErrorHandlerBuilder();
196        }
197    
198        /**
199         * Creates an error handler which just logs errors
200         */
201        public LoggingErrorHandlerBuilder loggingErrorHandler(String log) {
202            return loggingErrorHandler(LogFactory.getLog(log));
203        }
204    
205        /**
206         * Creates an error handler which just logs errors
207         */
208        public LoggingErrorHandlerBuilder loggingErrorHandler(Log log) {
209            return new LoggingErrorHandlerBuilder(log);
210        }
211    
212        /**
213         * Creates an error handler which just logs errors
214         */
215        public LoggingErrorHandlerBuilder loggingErrorHandler(Log log, LoggingLevel level) {
216            return new LoggingErrorHandlerBuilder(log, level);
217        }
218    
219        public DeadLetterChannelBuilder deadLetterChannel() {
220            return new DeadLetterChannelBuilder();
221        }
222    
223        public DeadLetterChannelBuilder deadLetterChannel(String deadLetterUri) {
224            return deadLetterChannel(endpoint(deadLetterUri));
225        }
226    
227        public DeadLetterChannelBuilder deadLetterChannel(Endpoint deadLetterEndpoint) {
228            return new DeadLetterChannelBuilder(new SendProcessor(deadLetterEndpoint));
229        }
230    
231        // Properties
232        // -------------------------------------------------------------------------
233        public CamelContext getContext() {
234            return context;
235        }
236    
237        public void setContext(CamelContext context) {
238            this.context = context;
239        }
240    
241        public ErrorHandlerBuilder getErrorHandlerBuilder() {
242            if (errorHandlerBuilder == null) {
243                errorHandlerBuilder = createErrorHandlerBuilder();
244            }
245            return errorHandlerBuilder;
246        }
247    
248        protected ErrorHandlerBuilder createErrorHandlerBuilder() {
249            if (isInheritErrorHandler()) {
250                return new DeadLetterChannelBuilder();
251            } else {
252                return new NoErrorHandlerBuilder();
253            }
254        }
255    
256        /**
257         * Sets the error handler to use with processors created by this builder
258         */
259        public void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder) {
260            this.errorHandlerBuilder = errorHandlerBuilder;
261        }
262    
263        public boolean isInheritErrorHandler() {
264            return inheritErrorHandler;
265        }
266    
267        public void setInheritErrorHandler(boolean inheritErrorHandler) {
268            this.inheritErrorHandler = inheritErrorHandler;
269        }
270    }