use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method randomExpression.
/**
* Returns a random number between min and max
*/
public static Expression randomExpression(final int min, final int max) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Random random = new Random();
int randomNum = random.nextInt(max - min) + min;
return randomNum;
}
@Override
public String toString() {
return "random";
}
};
}
use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method randomExpression.
/**
* Returns a random number between min and max
*/
public static Expression randomExpression(final String min, final String max) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
int num1 = simpleExpression(min).evaluate(exchange, Integer.class);
int num2 = simpleExpression(max).evaluate(exchange, Integer.class);
Random random = new Random();
int randomNum = random.nextInt(num2 - num1) + num1;
return randomNum;
}
@Override
public String toString() {
return "random";
}
};
}
use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method routeIdExpression.
/**
* Returns an Expression for the route id
*/
public static Expression routeIdExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
String answer = null;
UnitOfWork uow = exchange.getUnitOfWork();
RouteContext rc = uow != null ? uow.getRouteContext() : null;
if (rc != null) {
answer = rc.getRoute().getId();
}
if (answer == null) {
// fallback and get from route id on the exchange
answer = exchange.getFromRouteId();
}
return answer;
}
@Override
public String toString() {
return "routeId";
}
};
}
use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method propertiesComponentExpression.
public static Expression propertiesComponentExpression(final String key, final String locations, final String defaultValue) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
String text = simpleExpression(key).evaluate(exchange, String.class);
String text2 = simpleExpression(locations).evaluate(exchange, String.class);
try {
if (text2 != null) {
// the properties component is optional as we got locations
// getComponent will create a new component if none already exists
Component component = exchange.getContext().getComponent("properties");
PropertiesComponent pc = exchange.getContext().getTypeConverter().mandatoryConvertTo(PropertiesComponent.class, component);
// enclose key with {{ }} to force parsing
String[] paths = text2.split(",");
return pc.parseUri(pc.getPrefixToken() + text + pc.getSuffixToken(), paths);
} else {
// the properties component is mandatory if no locations provided
Component component = exchange.getContext().hasComponent("properties");
if (component == null) {
throw new IllegalArgumentException("PropertiesComponent with name properties must be defined" + " in CamelContext to support property placeholders in expressions");
}
PropertiesComponent pc = exchange.getContext().getTypeConverter().mandatoryConvertTo(PropertiesComponent.class, component);
// enclose key with {{ }} to force parsing
return pc.parseUri(pc.getPrefixToken() + text + pc.getSuffixToken());
}
} catch (Exception e) {
// property with key not found, use default value if provided
if (defaultValue != null) {
return defaultValue;
}
throw ObjectHelper.wrapRuntimeCamelException(e);
}
}
@Override
public String toString() {
return "properties(" + key + ")";
}
};
}
use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class RefLanguage method createExpression.
public Expression createExpression(final String expression) {
final Expression exp = RefLanguage.ref(expression);
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Expression target = null;
Object lookup = exp.evaluate(exchange, Object.class);
// must favor expression over predicate
if (lookup != null && lookup instanceof Expression) {
target = (Expression) lookup;
} else if (lookup != null && lookup instanceof Predicate) {
target = PredicateToExpressionAdapter.toExpression((Predicate) lookup);
}
if (target != null) {
return target.evaluate(exchange, Object.class);
} else {
throw new IllegalArgumentException("Cannot find expression or predicate in registry with ref: " + expression);
}
}
public String toString() {
return exp.toString();
}
};
}
Aggregations