This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2e54a1202950cb8f760006967142a02a16a9f6b2 Author: Claus Ibsen <[email protected]> AuthorDate: Fri Jan 3 18:53:30 2020 +0100 CAMEL-14354: More optimizations in regards to object allocations --- .../org/apache/camel/support/MessageHelper.java | 40 +++++++++++--------- .../processor/DefaultExchangeFormatter.java | 43 ++++++++++++---------- 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java index 8bbb5a4..a794920 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java @@ -198,7 +198,7 @@ public final class MessageHelper { } } - return extractValueForLogging(value, message, "", streams, false, maxChars); + return extractValueForLogging(value, message, streams, false, maxChars); } /** @@ -257,14 +257,19 @@ public final class MessageHelper { * * @see org.apache.camel.Exchange#LOG_DEBUG_BODY_MAX_CHARS * @param message the message - * @param prepend a message to prepend + * @param prepend a message to prepend (optional) * @param allowStreams whether or not streams is allowed * @param allowFiles whether or not files is allowed (currently not in use) * @param maxChars limit to maximum number of chars. Use 0 for not limit, and -1 for turning logging message body off. * @return the logging message */ public static String extractBodyForLogging(Message message, String prepend, boolean allowStreams, boolean allowFiles, int maxChars) { - return extractValueForLogging(message.getBody(), message, prepend, allowStreams, allowFiles, maxChars); + String value = extractValueForLogging(message.getBody(), message, allowStreams, allowFiles, maxChars); + if (prepend != null) { + return prepend + value; + } else { + return value; + } } /** @@ -275,44 +280,43 @@ public final class MessageHelper { * @see org.apache.camel.Exchange#LOG_DEBUG_BODY_MAX_CHARS * @param obj the value * @param message the message - * @param prepend a message to prepend * @param allowStreams whether or not streams is allowed * @param allowFiles whether or not files is allowed (currently not in use) * @param maxChars limit to maximum number of chars. Use 0 for not limit, and -1 for turning logging message body off. * @return the logging message */ - public static String extractValueForLogging(Object obj, Message message, String prepend, boolean allowStreams, boolean allowFiles, int maxChars) { + public static String extractValueForLogging(Object obj, Message message, boolean allowStreams, boolean allowFiles, int maxChars) { if (maxChars < 0) { - return prepend + "[Body is not logged]"; + return "[Body is not logged]"; } if (obj == null) { - return prepend + "[Body is null]"; + return "[Body is null]"; } if (!allowStreams) { if (instanceOf(obj, "java.xml.transform.Source")) { - return prepend + "[Body is instance of java.xml.transform.Source]"; + return "[Body is instance of java.xml.transform.Source]"; } else if (obj instanceof StreamCache) { - return prepend + "[Body is instance of org.apache.camel.StreamCache]"; + return "[Body is instance of org.apache.camel.StreamCache]"; } else if (obj instanceof InputStream) { - return prepend + "[Body is instance of java.io.InputStream]"; + return "[Body is instance of java.io.InputStream]"; } else if (obj instanceof OutputStream) { - return prepend + "[Body is instance of java.io.OutputStream]"; + return "[Body is instance of java.io.OutputStream]"; } else if (obj instanceof Reader) { - return prepend + "[Body is instance of java.io.Reader]"; + return "[Body is instance of java.io.Reader]"; } else if (obj instanceof Writer) { - return prepend + "[Body is instance of java.io.Writer]"; + return "[Body is instance of java.io.Writer]"; } else if (obj instanceof WrappedFile || obj instanceof File) { if (!allowFiles) { - return prepend + "[Body is file based: " + obj + "]"; + return "[Body is file based: " + obj + "]"; } } } if (!allowFiles) { if (obj instanceof WrappedFile || obj instanceof File) { - return prepend + "[Body is file based: " + obj + "]"; + return "[Body is file based: " + obj + "]"; } } @@ -356,7 +360,7 @@ public final class MessageHelper { } if (body == null) { - return prepend + "[Body is null]"; + return "[Body is null]"; } // clip body if length enabled and the body is too big @@ -364,7 +368,7 @@ public final class MessageHelper { body = body.substring(0, maxChars) + "... [Body clipped after " + maxChars + " chars, total length is " + body.length() + "]"; } - return prepend + body; + return body; } private static boolean instanceOf(Object obj, String interfaceName) { @@ -479,7 +483,7 @@ public final class MessageHelper { } sb.append(">"); - String xml = extractBodyForLogging(message, "", allowStreams, allowFiles, maxChars); + String xml = extractBodyForLogging(message, null, allowStreams, allowFiles, maxChars); if (xml != null) { // must always xml encode sb.append(StringHelper.xmlEncode(xml)); diff --git a/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultExchangeFormatter.java b/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultExchangeFormatter.java index 2679243e..9c3a6aa 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultExchangeFormatter.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultExchangeFormatter.java @@ -80,18 +80,20 @@ public class DefaultExchangeFormatter implements ExchangeFormatter { @UriParam(label = "formatting", enums = "Default,Tab,Fixed", defaultValue = "Default", description = "Sets the outputs style to use.") private OutputStyle style = OutputStyle.Default; - private String style(String label) { + private StringBuilder style(StringBuilder sb, String label) { if (style == OutputStyle.Default) { if (multiline) { - return " " + label + ": "; + sb.append(" ").append(label).append(": "); } else { - return ", " + label + ": "; + sb.append(", ").append(label).append(": "); } } else if (style == OutputStyle.Tab) { - return "'t" + label + ": " ; + sb.append("\t").append(label).append(": "); } else { - return String.format("\t%-20s", label); + String s = String.format("\t%-20s", label); + sb.append(s); } + return sb; } @Override @@ -103,32 +105,32 @@ public class DefaultExchangeFormatter implements ExchangeFormatter { if (multiline) { sb.append(SEPARATOR); } - sb.append(style("Id")).append(exchange.getExchangeId()); + style(sb, "Id").append(exchange.getExchangeId()); } if (showAll || showExchangePattern) { if (multiline) { sb.append(SEPARATOR); } - sb.append(style("ExchangePattern")).append(exchange.getPattern()); + style(sb, "ExchangePattern").append(exchange.getPattern()); } if (showAll || showProperties) { if (multiline) { sb.append(SEPARATOR); } - sb.append(style("Properties")).append(sortMap(filterHeaderAndProperties(exchange.getProperties()))); + style(sb, "Properties").append(sortMap(filterHeaderAndProperties(exchange.getProperties()))); } if (showAll || showHeaders) { if (multiline) { sb.append(SEPARATOR); } - sb.append(style("Headers")).append(sortMap(filterHeaderAndProperties(in.getHeaders()))); + style(sb, "Headers").append(sortMap(filterHeaderAndProperties(in.getHeaders()))); } if (showAll || showBodyType) { if (multiline) { sb.append(SEPARATOR); } - sb.append(style("BodyType")).append(getBodyTypeAsString(in)); + style(sb, "BodyType").append(getBodyTypeAsString(in)); } if (showAll || showBody) { if (multiline) { @@ -138,7 +140,7 @@ public class DefaultExchangeFormatter implements ExchangeFormatter { if (skipBodyLineSeparator) { body = StringHelper.replaceAll(body, LS, ""); } - sb.append(style("Body")).append(body); + style(sb, "Body").append(body); } if (showAll || showException || showCaughtException) { @@ -157,21 +159,22 @@ public class DefaultExchangeFormatter implements ExchangeFormatter { sb.append(SEPARATOR); } if (caught) { - sb.append(style("CaughtExceptionType")).append(exception.getClass().getCanonicalName()); - sb.append(style("CaughtExceptionMessage")).append(exception.getMessage()); + style(sb, "CaughtExceptionType").append(exception.getClass().getCanonicalName()); + style(sb, "CaughtExceptionMessage").append(exception.getMessage()); } else { - sb.append(style("ExceptionType")).append(exception.getClass().getCanonicalName()); - sb.append(style("ExceptionMessage")).append(exception.getMessage()); + style(sb, "ExceptionType").append(exception.getClass().getCanonicalName()); + style(sb, "ExceptionMessage").append(exception.getMessage()); } if (showAll || showStackTrace) { StringWriter sw = new StringWriter(); exception.printStackTrace(new PrintWriter(sw)); - sb.append(style("StackTrace")).append(sw.toString()); + style(sb, "StackTrace").append(sw.toString()); } } } - if (maxChars > 0) { + // only cut if we hit max-chars limit + if (maxChars > 0 && sb.length() > maxChars) { StringBuilder answer = new StringBuilder(); for (String s : sb.toString().split(SEPARATOR)) { if (s != null) { @@ -196,8 +199,8 @@ public class DefaultExchangeFormatter implements ExchangeFormatter { sb.append("]"); return sb.toString(); } else { - // get rid of the leading space comma if needed - if (sb.length() > 0 && sb.charAt(0) == ',' && sb.charAt(1) == ' ') { + // get rid of the leading comma space if needed + if (sb.length() > 1 && sb.charAt(0) == ',' && sb.charAt(1) == ' ') { sb.replace(0, 2, ""); } sb.insert(0, "Exchange["); @@ -418,7 +421,7 @@ public class DefaultExchangeFormatter implements ExchangeFormatter { } } - return MessageHelper.extractBodyForLogging(message, "", isShowStreams(), isShowFiles(), getMaxChars(message)); + return MessageHelper.extractBodyForLogging(message, null, isShowStreams(), isShowFiles(), getMaxChars(message)); } private int getMaxChars(Message message) {
