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