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.List;
021    
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.Endpoint;
024    import org.apache.camel.processor.LoggingLevel;
025    import org.apache.camel.processor.SendProcessor;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    
029    /**
030     * Base class for implementation inheritance for different clauses in the 
031     * <a href="http://activemq.apache.org/camel/dsl.html">Java DSL</a>
032     *
033     * @version $Revision: $
034     */
035    public abstract class BuilderSupport {
036        private CamelContext context;
037        private ErrorHandlerBuilder errorHandlerBuilder;
038        private boolean inheritErrorHandler = true;
039    
040        protected BuilderSupport(CamelContext context) {
041            this.context = context;
042        }
043    
044        protected BuilderSupport(BuilderSupport parent) {
045            this.context = parent.getContext();
046            this.inheritErrorHandler = parent.inheritErrorHandler;
047            if (inheritErrorHandler && parent.errorHandlerBuilder != null) {
048                this.errorHandlerBuilder = parent.errorHandlerBuilder.copy();
049            }
050        }
051    
052        // Builder methods
053        //-------------------------------------------------------------------------
054    
055        /**
056         * Returns a value builder for the given header
057         */
058        @Fluent
059        public ValueBuilder header(@FluentArg("name")String name) {
060            return Builder.header(name);
061        }
062    
063        /**
064         * Returns a predicate and value builder for the inbound body on an exchange
065         */
066        @Fluent
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 specific type
073         */
074        @Fluent
075        public <T> ValueBuilder bodyAs(@FluentArg("class")Class<T> type) {
076            return Builder.bodyAs(type);
077        }
078    
079        /**
080         * Returns a predicate and value builder for the outbound body on an exchange
081         */
082        @Fluent
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 specific type
089         */
090        @Fluent
091        public <T> ValueBuilder outBody(@FluentArg("class")Class<T> type) {
092            return Builder.outBody(type);
093        }
094    
095        /**
096         * Returns a value builder for the given system property
097         */
098        @Fluent
099        public ValueBuilder systemProperty(@FluentArg("name")String name) {
100            return Builder.systemProperty(name);
101        }
102    
103        /**
104         * Returns a value builder for the given system property
105         */
106        @Fluent
107        public ValueBuilder systemProperty(
108                @FluentArg("name")String name, @FluentArg("defaultValue")String defaultValue) {
109            return Builder.systemProperty(name, defaultValue);
110        }
111    
112        /**
113         * Resolves the given URI to an endpoint
114         */
115        @Fluent
116        public Endpoint endpoint(@FluentArg("uri")String uri) {
117            return getContext().getEndpoint(uri);
118        }
119    
120        /**
121         * Resolves the list of URIs into a list of {@link Endpoint} instances
122         */
123        @Fluent
124        public List<Endpoint> endpoints(@FluentArg("uris")String... uris) {
125            List<Endpoint> endpoints = new ArrayList<Endpoint>();
126            for (String uri : uris) {
127                endpoints.add(endpoint(uri));
128            }
129            return endpoints;
130        }
131    
132        /**
133         * Helper method to create a list of {@link Endpoint} instances
134         */
135        @Fluent
136        public List<Endpoint> endpoints(@FluentArg("endpoints")Endpoint... endpoints) {
137            List<Endpoint> answer = new ArrayList<Endpoint>();
138            for (Endpoint endpoint : endpoints) {
139                answer.add(endpoint);
140            }
141            return answer;
142        }
143    
144        /**
145         * Creates a disabled error handler for removing the default error handler
146         */
147        @Fluent
148        public NoErrorHandlerBuilder noErrorHandler() {
149            return new NoErrorHandlerBuilder();
150        }
151    
152        /**
153         * Creates an error handler which just logs errors
154         */
155        @Fluent
156        public LoggingErrorHandlerBuilder loggingErrorHandler() {
157            return new LoggingErrorHandlerBuilder();
158        }
159    
160        /**
161         * Creates an error handler which just logs errors
162         */
163        @Fluent
164        public LoggingErrorHandlerBuilder loggingErrorHandler(@FluentArg("log")String log) {
165            return loggingErrorHandler(LogFactory.getLog(log));
166        }
167    
168        /**
169         * Creates an error handler which just logs errors
170         */
171        @Fluent
172        public LoggingErrorHandlerBuilder loggingErrorHandler(@FluentArg("log")Log log) {
173            return new LoggingErrorHandlerBuilder(log);
174        }
175    
176        /**
177         * Creates an error handler which just logs errors
178         */
179        @Fluent
180        public LoggingErrorHandlerBuilder loggingErrorHandler(
181                @FluentArg("log")Log log, @FluentArg("level")LoggingLevel level) {
182            return new LoggingErrorHandlerBuilder(log, level);
183        }
184    
185        @Fluent
186        public DeadLetterChannelBuilder deadLetterChannel() {
187            return new DeadLetterChannelBuilder();
188        }
189    
190        @Fluent
191        public DeadLetterChannelBuilder deadLetterChannel(@FluentArg("uri")String deadLetterUri) {
192            return deadLetterChannel(endpoint(deadLetterUri));
193        }
194    
195        @Fluent
196        public DeadLetterChannelBuilder deadLetterChannel(@FluentArg("ref")Endpoint deadLetterEndpoint) {
197            return new DeadLetterChannelBuilder(new SendProcessor(deadLetterEndpoint));
198        }
199    
200        // Properties
201        //-------------------------------------------------------------------------
202        public CamelContext getContext() {
203            return context;
204        }
205    
206        public void setContext(CamelContext context) {
207            this.context = context;
208        }
209    
210        public ErrorHandlerBuilder getErrorHandlerBuilder() {
211            if (errorHandlerBuilder == null) {
212                errorHandlerBuilder = createErrorHandlerBuilder();
213            }
214            return errorHandlerBuilder;
215        }
216    
217        protected ErrorHandlerBuilder createErrorHandlerBuilder() {
218            if (isInheritErrorHandler()) {
219                return new DeadLetterChannelBuilder();
220            }
221            else {
222                return new NoErrorHandlerBuilder();
223            }
224        }
225    
226        /**
227         * Sets the error handler to use with processors created by this builder
228         */
229        public void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder) {
230            this.errorHandlerBuilder = errorHandlerBuilder;
231        }
232    
233        public boolean isInheritErrorHandler() {
234            return inheritErrorHandler;
235        }
236    
237        public void setInheritErrorHandler(boolean inheritErrorHandler) {
238            this.inheritErrorHandler = inheritErrorHandler;
239        }
240    }