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: 745602 $
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 /**
115 * Returns a value builder for the given system property
116 */
117 public ValueBuilder systemProperty(String name) {
118 return Builder.systemProperty(name);
119 }
120
121 /**
122 * Returns a value builder for the given system property
123 */
124 public ValueBuilder systemProperty(String name, String defaultValue) {
125 return Builder.systemProperty(name, defaultValue);
126 }
127
128 /**
129 * Returns a constant expression value builder
130 */
131 public ValueBuilder constant(Object value) {
132 return Builder.constant(value);
133 }
134
135 /**
136 * Returns an expression value builder that replaces all occurrences of the
137 * regular expression with the given replacement
138 */
139 public ValueBuilder regexReplaceAll(Expression content, String regex, String replacement) {
140 return Builder.regexReplaceAll(content, regex, replacement);
141 }
142
143 /**
144 * Returns an expression value builder that replaces all occurrences of the
145 * regular expression with the given replacement
146 */
147 public ValueBuilder regexReplaceAll(Expression content, String regex, Expression replacement) {
148 return Builder.regexReplaceAll(content, regex, replacement);
149 }
150
151 /**
152 * Resolves the given URI to an endpoint
153 *
154 * @throws NoSuchEndpointException if the endpoint URI could not be resolved
155 */
156 public Endpoint endpoint(String uri) throws NoSuchEndpointException {
157 ObjectHelper.notNull(uri, "uri");
158 Endpoint endpoint = getContext().getEndpoint(uri);
159 if (endpoint == null) {
160 throw new NoSuchEndpointException(uri);
161 }
162 return endpoint;
163 }
164
165 /**
166 * Resolves the given URI to an endpoint of the specified type
167 *
168 * @throws NoSuchEndpointException if the endpoint URI could not be resolved
169 */
170 public <T extends Endpoint> T endpoint(String uri, Class<T> type) throws NoSuchEndpointException {
171 ObjectHelper.notNull(uri, "uri");
172 T endpoint = getContext().getEndpoint(uri, type);
173 if (endpoint == null) {
174 throw new NoSuchEndpointException(uri);
175 }
176 return endpoint;
177 }
178
179 /**
180 * Resolves the list of URIs into a list of {@link Endpoint} instances
181 *
182 * @throws NoSuchEndpointException if an endpoint URI could not be resolved
183 */
184 public List<Endpoint> endpoints(String... uris) throws NoSuchEndpointException {
185 List<Endpoint> endpoints = new ArrayList<Endpoint>();
186 for (String uri : uris) {
187 endpoints.add(endpoint(uri));
188 }
189 return endpoints;
190 }
191
192 /**
193 * Helper method to create a list of {@link Endpoint} instances
194 */
195 public List<Endpoint> endpoints(Endpoint... endpoints) {
196 List<Endpoint> answer = new ArrayList<Endpoint>();
197 answer.addAll(Arrays.asList(endpoints));
198 return answer;
199 }
200
201 /**
202 * Creates a disabled error handler for removing the default error handler
203 */
204 public NoErrorHandlerBuilder noErrorHandler() {
205 return new NoErrorHandlerBuilder();
206 }
207
208 /**
209 * Creates an error handler which just logs errors
210 */
211 public LoggingErrorHandlerBuilder loggingErrorHandler() {
212 return new LoggingErrorHandlerBuilder();
213 }
214
215 /**
216 * Creates an error handler which just logs errors
217 */
218 public LoggingErrorHandlerBuilder loggingErrorHandler(String log) {
219 return loggingErrorHandler(LogFactory.getLog(log));
220 }
221
222 /**
223 * Creates an error handler which just logs errors
224 */
225 public LoggingErrorHandlerBuilder loggingErrorHandler(Log log) {
226 return new LoggingErrorHandlerBuilder(log);
227 }
228
229 /**
230 * Creates an error handler which just logs errors
231 */
232 public LoggingErrorHandlerBuilder loggingErrorHandler(Log log, LoggingLevel level) {
233 return new LoggingErrorHandlerBuilder(log, level);
234 }
235
236 public DeadLetterChannelBuilder deadLetterChannel() {
237 return new DeadLetterChannelBuilder();
238 }
239
240 public DeadLetterChannelBuilder deadLetterChannel(String deadLetterUri) {
241 return deadLetterChannel(endpoint(deadLetterUri));
242 }
243
244 public DeadLetterChannelBuilder deadLetterChannel(Endpoint deadLetterEndpoint) {
245 return new DeadLetterChannelBuilder(new SendProcessor(deadLetterEndpoint));
246 }
247
248 // Properties
249 // -------------------------------------------------------------------------
250 public CamelContext getContext() {
251 return context;
252 }
253
254 public void setContext(CamelContext context) {
255 this.context = context;
256 }
257
258 public ErrorHandlerBuilder getErrorHandlerBuilder() {
259 if (errorHandlerBuilder == null) {
260 errorHandlerBuilder = createErrorHandlerBuilder();
261 }
262 return errorHandlerBuilder;
263 }
264
265 protected ErrorHandlerBuilder createErrorHandlerBuilder() {
266 if (isInheritErrorHandler()) {
267 return new DeadLetterChannelBuilder();
268 } else {
269 return new NoErrorHandlerBuilder();
270 }
271 }
272
273 /**
274 * Sets the error handler to use with processors created by this builder
275 */
276 public void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder) {
277 this.errorHandlerBuilder = errorHandlerBuilder;
278 }
279
280 public boolean isInheritErrorHandler() {
281 return inheritErrorHandler;
282 }
283
284 public void setInheritErrorHandler(boolean inheritErrorHandler) {
285 this.inheritErrorHandler = inheritErrorHandler;
286 }
287 }