Skip to content

Commit

Permalink
CAMEL-9062: Java DSL should build expressions as languages so they mo…
Browse files Browse the repository at this point in the history
…del can be dumped, and build similar as we do in XML DSL
  • Loading branch information
davsclaus committed Aug 7, 2015
1 parent c81a051 commit c63c250
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 66 deletions.
87 changes: 58 additions & 29 deletions camel-core/src/main/java/org/apache/camel/builder/Builder.java
Expand Up @@ -17,12 +17,23 @@
package org.apache.camel.builder;

import org.apache.camel.Expression;
import org.apache.camel.model.language.ConstantExpression;
import org.apache.camel.model.language.ExchangePropertyExpression;
import org.apache.camel.model.language.HeaderExpression;
import org.apache.camel.model.language.LanguageExpression;
import org.apache.camel.model.language.MethodCallExpression;
import org.apache.camel.model.language.SimpleExpression;
import org.apache.camel.util.ObjectHelper;

/**
* A helper class for including portions of the <a
* href="http://camel.apache.org/expression.html">expression</a> and
* <a href="http://camel.apache.org/predicate.html">predicate</a> <a
* href="http://camel.apache.org/dsl.html">Java DSL</a>
* <p/>
* Implementation of this builder should favor build expressions using the definition classes
* from the <tt>org.apache.camel.model.language</tt> package, to build the routes using the same
* types as it would happen when using XML DSL.
*
* @version
*/
Expand Down Expand Up @@ -58,13 +69,13 @@ public static ValueBuilder bean(final Object beanOrBeanRef) {
* @return the builder
*/
public static ValueBuilder bean(Object beanOrBeanRef, String method) {
Expression expression;
Expression exp;
if (beanOrBeanRef instanceof String) {
expression = ExpressionBuilder.beanExpression((String) beanOrBeanRef, method);
exp = new MethodCallExpression((String) beanOrBeanRef, method);
} else {
expression = ExpressionBuilder.beanExpression(beanOrBeanRef, method);
exp = new MethodCallExpression(beanOrBeanRef, method);
}
return new ValueBuilder(expression);
return new ValueBuilder(exp);
}

/**
Expand All @@ -76,49 +87,54 @@ public static ValueBuilder bean(Object beanOrBeanRef, String method) {
* @return the builder
*/
public static ValueBuilder bean(Class<?> beanType, String method) {
Expression expression = ExpressionBuilder.beanExpression(beanType, method);
return new ValueBuilder(expression);
Expression exp = new MethodCallExpression(beanType, method);
return new ValueBuilder(exp);
}

/**
* Returns a constant expression
*/
public static ValueBuilder constant(Object value) {
Expression expression = ExpressionBuilder.constantExpression(value);
return new ValueBuilder(expression);
Expression exp;
if (value instanceof String) {
exp = new ConstantExpression((String) value);
} else {
exp = ExpressionBuilder.constantExpression(value);
}
return new ValueBuilder(exp);
}

/**
* Returns a constant expression
*/
public static ValueBuilder language(String language, String expression) {
Expression exp = ExpressionBuilder.languageExpression(language, expression);
Expression exp = new LanguageExpression(language, expression);
return new ValueBuilder(exp);
}

/**
* Returns a simple expression
*/
public static ValueBuilder simple(String value) {
Expression expression = ExpressionBuilder.simpleExpression(value);
return new ValueBuilder(expression);
Expression exp = new SimpleExpression(value);
return new ValueBuilder(exp);
}

/**
* Returns a simple expression
*/
public static ValueBuilder simple(String value, Class<?> resultType) {
Expression expression = ExpressionBuilder.simpleExpression(value);
expression = ExpressionBuilder.convertToExpression(expression, resultType);
return new ValueBuilder(expression);
SimpleExpression exp = new SimpleExpression(value);
exp.setResultType(resultType);
return new ValueBuilder(exp);
}

/**
* Returns a predicate and value builder for headers on an exchange
*/
public static ValueBuilder header(String name) {
Expression expression = ExpressionBuilder.headerExpression(name);
return new ValueBuilder(expression);
Expression exp = new HeaderExpression(name);
return new ValueBuilder(exp);
}

/**
Expand All @@ -135,40 +151,47 @@ public static ValueBuilder property(String name) {
* Returns a predicate and value builder for properties on an exchange
*/
public static ValueBuilder exchangeProperty(String name) {
Expression expression = ExpressionBuilder.exchangePropertyExpression(name);
return new ValueBuilder(expression);
Expression exp = new ExchangePropertyExpression(name);
return new ValueBuilder(exp);
}

/**
* Returns a predicate and value builder for the inbound body on an exchange
*/
public static ValueBuilder body() {
Expression expression = ExpressionBuilder.bodyExpression();
return new ValueBuilder(expression);
Expression exp = new SimpleExpression("${body}");
return new ValueBuilder(exp);
}

/**
* Returns a predicate and value builder for the inbound message body as a
* specific type
*/
public static <T> ValueBuilder bodyAs(Class<T> type) {
Expression expression = ExpressionBuilder.bodyExpression(type);
return new ValueBuilder(expression);
ObjectHelper.notNull(type, "type");
Expression exp = new SimpleExpression(String.format("${bodyAs(%s)}", type.getCanonicalName()));
return new ValueBuilder(exp);
}

/**
* Returns a predicate and value builder for the outbound body on an
* exchange
*
* @deprecated use {@link #body()}
*/
@Deprecated
public static ValueBuilder outBody() {
Expression expression = ExpressionBuilder.outBodyExpression();
return new ValueBuilder(expression);
Expression exp = new SimpleExpression("${out.body}");
return new ValueBuilder(exp);
}

/**
* Returns a predicate and value builder for the outbound message body as a
* specific type
*
* @deprecated use {@link #bodyAs(Class)}
*/
@Deprecated
public static <T> ValueBuilder outBodyAs(Class<T> type) {
Expression expression = ExpressionBuilder.outBodyExpression(type);
return new ValueBuilder(expression);
Expand All @@ -186,7 +209,10 @@ public static ValueBuilder faultBody() {
/**
* Returns a predicate and value builder for the fault message body as a
* specific type
*
* @deprecated use {@link #bodyAs(Class)}
*/
@Deprecated
public static <T> ValueBuilder faultBodyAs(Class<T> type) {
Expression expression = ExpressionBuilder.faultBodyExpression(type);
return new ValueBuilder(expression);
Expand All @@ -196,7 +222,8 @@ public static <T> ValueBuilder faultBodyAs(Class<T> type) {
* Returns an expression for the given system property
*/
public static ValueBuilder systemProperty(final String name) {
return systemProperty(name, null);
Expression exp = new SimpleExpression(String.format("${sys.%s}", name));
return new ValueBuilder(exp);
}

/**
Expand All @@ -210,16 +237,16 @@ public static ValueBuilder systemProperty(final String name, final String defaul
* Returns a predicate and value builder for the exception message on an exchange
*/
public static ValueBuilder exceptionMessage() {
Expression expression = ExpressionBuilder.exchangeExceptionMessageExpression();
return new ValueBuilder(expression);
Expression exp = new SimpleExpression("${exception.message}");
return new ValueBuilder(exp);
}

/**
* Returns a predicate and value builder for the exception stacktrace on an exchange
*/
public static ValueBuilder exceptionStackTrace() {
Expression expression = ExpressionBuilder.exchangeExceptionStackTraceExpression();
return new ValueBuilder(expression);
Expression exp = new SimpleExpression("${exception.stacktrace}");
return new ValueBuilder(exp);
}

/**
Expand All @@ -245,7 +272,9 @@ public static ValueBuilder regexReplaceAll(Expression content, String regex, Exp
*
* @param uri endpoint uri
* @return the builder
* @deprecated not in use, and not available in XML DSL
*/
@Deprecated
public static ValueBuilder sendTo(String uri) {
Expression expression = ExpressionBuilder.toExpression(uri);
return new ValueBuilder(expression);
Expand Down
Expand Up @@ -29,7 +29,6 @@
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.language.ExchangePropertyExpression;
import org.apache.camel.model.language.HeaderExpression;
import org.apache.camel.model.language.MethodCallExpression;
import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -58,8 +57,8 @@ protected BuilderSupport(CamelContext context) {
* Returns a value builder for the given header
*/
public ValueBuilder header(String name) {
HeaderExpression expression = new HeaderExpression(name);
return new ValueBuilder(expression);
Expression exp = new HeaderExpression(name);
return new ValueBuilder(exp);
}

/**
Expand All @@ -69,16 +68,16 @@ public ValueBuilder header(String name) {
*/
@Deprecated
public ValueBuilder property(String name) {
ExchangePropertyExpression expression = new ExchangePropertyExpression(name);
return new ValueBuilder(expression);
Expression exp = new ExchangePropertyExpression(name);
return new ValueBuilder(exp);
}

/**
* Returns a value builder for the given exchange property
*/
public ValueBuilder exchangeProperty(String name) {
ExchangePropertyExpression expression = new ExchangePropertyExpression(name);
return new ValueBuilder(expression);
Expression exp = new ExchangePropertyExpression(name);
return new ValueBuilder(exp);
}

/**
Expand All @@ -91,23 +90,40 @@ public ValueBuilder body() {
/**
* Returns a predicate and value builder for the inbound message body as a
* specific type
*
* @deprecated use {@link #bodyAs(Class)}
*/
@Deprecated
public <T> ValueBuilder body(Class<T> type) {
return bodyAs(type);
}

/**
* Returns a predicate and value builder for the inbound message body as a
* specific type
*/
public <T> ValueBuilder bodyAs(Class<T> type) {
return Builder.bodyAs(type);
}

/**
* Returns a predicate and value builder for the outbound body on an
* exchange
*
* @deprecated use {@link #body()}
*/
@Deprecated
public ValueBuilder outBody() {
return Builder.outBody();
}

/**
* Returns a predicate and value builder for the outbound message body as a
* specific type
*
* @deprecated use {@link #bodyAs(Class)}
*/
@Deprecated
public <T> ValueBuilder outBody(Class<T> type) {
return Builder.outBodyAs(type);
}
Expand All @@ -123,7 +139,10 @@ public ValueBuilder faultBody() {
/**
* Returns a predicate and value builder for the fault message body as a
* specific type
*
* @deprecated use {@link #bodyAs(Class)}
*/
@Deprecated
public <T> ValueBuilder faultBodyAs(Class<T> type) {
return Builder.faultBodyAs(type);
}
Expand Down Expand Up @@ -217,13 +236,7 @@ public ValueBuilder bean(Object beanOrBeanRef) {
*/
@Deprecated
public ValueBuilder bean(Object beanOrBeanRef, String method) {
MethodCallExpression expression;
if (beanOrBeanRef instanceof String) {
expression = new MethodCallExpression((String) beanOrBeanRef, method);
} else {
expression = new MethodCallExpression(beanOrBeanRef, method);
}
return new ValueBuilder(expression);
return Builder.bean(beanOrBeanRef, method);
}

/**
Expand All @@ -236,8 +249,7 @@ public ValueBuilder bean(Object beanOrBeanRef, String method) {
*/
@Deprecated
public ValueBuilder bean(Class<?> beanType) {
MethodCallExpression expression = new MethodCallExpression(beanType);
return new ValueBuilder(expression);
return Builder.bean(beanType);
}

/**
Expand All @@ -251,8 +263,7 @@ public ValueBuilder bean(Class<?> beanType) {
*/
@Deprecated
public ValueBuilder bean(Class<?> beanType, String method) {
MethodCallExpression expression = new MethodCallExpression(beanType, method);
return new ValueBuilder(expression);
return Builder.bean(beanType, method);
}

/**
Expand All @@ -279,13 +290,7 @@ public ValueBuilder method(Object beanOrBeanRef) {
* @return the builder
*/
public ValueBuilder method(Object beanOrBeanRef, String method) {
MethodCallExpression expression;
if (beanOrBeanRef instanceof String) {
expression = new MethodCallExpression((String) beanOrBeanRef, method);
} else {
expression = new MethodCallExpression(beanOrBeanRef, method);
}
return new ValueBuilder(expression);
return Builder.bean(beanOrBeanRef, method);
}

/**
Expand All @@ -296,8 +301,7 @@ public ValueBuilder method(Object beanOrBeanRef, String method) {
* @return the builder
*/
public ValueBuilder method(Class<?> beanType) {
MethodCallExpression expression = new MethodCallExpression(beanType);
return new ValueBuilder(expression);
return Builder.bean(beanType);
}

/**
Expand All @@ -309,16 +313,17 @@ public ValueBuilder method(Class<?> beanType) {
* @return the builder
*/
public ValueBuilder method(Class<?> beanType, String method) {
MethodCallExpression expression = new MethodCallExpression(beanType, method);
return new ValueBuilder(expression);
return Builder.bean(beanType, method);
}

/**
* Returns an expression processing the exchange to the given endpoint uri
*
* @param uri endpoint uri to send the exchange to
* @return the builder
* @deprecated not in use, and not available in XML DSL
*/
@Deprecated
public ValueBuilder sendTo(String uri) {
return Builder.sendTo(uri);
}
Expand Down
Expand Up @@ -24,7 +24,6 @@
import org.apache.camel.Expression;
import org.apache.camel.Predicate;
import org.apache.camel.builder.xml.Namespaces;
import org.apache.camel.model.language.ExpressionDefinition;
import org.apache.camel.spi.NamespaceAware;
import org.apache.camel.util.ExpressionToPredicateAdapter;

Expand Down

0 comments on commit c63c250

Please sign in to comment.