use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method messageHistoryExpression.
/**
* Returns the message history (including exchange details or not)
*/
public static Expression messageHistoryExpression(final boolean detailed) {
return new ExpressionAdapter() {
private ExchangeFormatter formatter;
public Object evaluate(Exchange exchange) {
ExchangeFormatter ef = null;
if (detailed) {
// use the exchange formatter to log exchange details
ef = getOrCreateExchangeFormatter(exchange.getContext());
}
return MessageHelper.dumpMessageHistoryStacktrace(exchange, ef, false);
}
private ExchangeFormatter getOrCreateExchangeFormatter(CamelContext camelContext) {
if (formatter == null) {
Set<ExchangeFormatter> formatters = camelContext.getRegistry().findByType(ExchangeFormatter.class);
if (formatters != null && formatters.size() == 1) {
formatter = formatters.iterator().next();
} else {
// setup exchange formatter to be used for message history dump
DefaultExchangeFormatter def = new DefaultExchangeFormatter();
def.setShowExchangeId(true);
def.setMultiline(true);
def.setShowHeaders(true);
def.setStyle(DefaultExchangeFormatter.OutputStyle.Fixed);
try {
Integer maxChars = CamelContextHelper.parseInteger(camelContext, camelContext.getGlobalOption(Exchange.LOG_DEBUG_BODY_MAX_CHARS));
if (maxChars != null) {
def.setMaxChars(maxChars);
}
} catch (Exception e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
formatter = def;
}
}
return formatter;
}
@Override
public String toString() {
return "messageHistory(" + detailed + ")";
}
};
}
Aggregations