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