From 7448865001da21b7300ebda66d8cc632974061a8 Mon Sep 17 00:00:00 2001 From: Tony Baines Date: Thu, 18 Feb 2016 16:28:58 +0000 Subject: [PATCH 1/4] [LOG4J2-1279] guard against null entries in arrays from DateFormatSymbols.getZoneStrings --- .../core/util/datetime/FastDateParser.java | 54 ++++++++++--------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FastDateParser.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FastDateParser.java index c5f9fdcd3a6..7c21aed9c05 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FastDateParser.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FastDateParser.java @@ -119,7 +119,7 @@ int modify(final int iValue) { *

* Constructs a new FastDateParser. *

- * + * * Use {@link FastDateFormat#getInstance(String, TimeZone, Locale)} or another variation of the factory methods of * {@link FastDateFormat} to get a cached FastDateParser instance. * @@ -236,7 +236,7 @@ private void init(final Calendar definingCalendar) { // ----------------------------------------------------------------------- /* * (non-Javadoc) - * + * * @see org.apache.commons.lang3.time.DateParser#getPattern() */ @Override @@ -246,7 +246,7 @@ public String getPattern() { /* * (non-Javadoc) - * + * * @see org.apache.commons.lang3.time.DateParser#getTimeZone() */ @Override @@ -256,7 +256,7 @@ public TimeZone getTimeZone() { /* * (non-Javadoc) - * + * * @see org.apache.commons.lang3.time.DateParser#getLocale() */ @Override @@ -334,7 +334,7 @@ private void readObject(final ObjectInputStream in) throws IOException, ClassNot /* * (non-Javadoc) - * + * * @see org.apache.commons.lang3.time.DateParser#parseObject(java.lang.String) */ @Override @@ -344,7 +344,7 @@ public Object parseObject(final String source) throws ParseException { /* * (non-Javadoc) - * + * * @see org.apache.commons.lang3.time.DateParser#parse(java.lang.String) */ @Override @@ -363,7 +363,7 @@ public Date parse(final String source) throws ParseException { /* * (non-Javadoc) - * + * * @see org.apache.commons.lang3.time.DateParser#parseObject(java.lang.String, java.text.ParsePosition) */ @Override @@ -431,7 +431,7 @@ private static StringBuilder simpleQuote(final StringBuilder sb, final String va /** * Escape constant fields into regular expression - * + * * @param regex The destination regex * @param value The source field * @param unquote If true, replace two success quotes ('') with single quote (') @@ -457,7 +457,7 @@ private static StringBuilder escapeRegex(final StringBuilder regex, final String /* * If we have found \E, we replace it with \E\\E\Q, i.e. we stop the quoting, quote the \ in \E, then * restart the quoting. - * + * * Otherwise we just output the two characters. In each case the initial \ needs to be output and the * final char is done at the end */ @@ -479,7 +479,7 @@ private static StringBuilder escapeRegex(final StringBuilder regex, final String /** * Get the short and long values displayed for a field - * + * * @param field The field of interest * @param definingCalendar The calendar to obtain the short and long values * @param locale The locale of display names @@ -492,7 +492,7 @@ private static Map getDisplayNames(final int field, final Calen /** * Adjust dates to be within appropriate century - * + * * @param twoDigitYear The year to adjust * @return A value between centuryStart(inclusive) to centuryStart+100(exclusive) */ @@ -503,7 +503,7 @@ private int adjustYear(final int twoDigitYear) { /** * Is the next field a number? - * + * * @return true, if next field will be a number */ boolean isNextNumber() { @@ -512,7 +512,7 @@ boolean isNextNumber() { /** * What is the width of the current field? - * + * * @return The number of characters in the current format field */ int getFieldWidth() { @@ -549,7 +549,7 @@ void setCalendar(final FastDateParser parser, final Calendar cal, final String v /** * Generate a Pattern regular expression to the StringBuilder which will accept this * field - * + * * @param parser The parser calling this strategy * @param regex The StringBuilder to append to * @return true, if this field will set the calendar; false, if this field is a constant value @@ -566,7 +566,7 @@ void setCalendar(final FastDateParser parser, final Calendar cal, final String v /** * Obtain a Strategy given a field from a SimpleDateFormat pattern - * + * * @param formatField A sub-sequence of the SimpleDateFormat pattern * @param definingCalendar The calendar to obtain the short and long values * @return The Strategy that will handle parsing for the field @@ -635,7 +635,7 @@ private Strategy getStrategy(final String formatField, final Calendar definingCa /** * Get a cache of Strategies for a particular field - * + * * @param field The Calendar field * @return a cache of Locale to Strategy */ @@ -650,7 +650,7 @@ private static ConcurrentMap getCache(final int field) { /** * Construct a Strategy that parses a Text field - * + * * @param field The Calendar field * @param definingCalendar The calendar to obtain the short and long values * @return a TextStrategy for the field and Locale @@ -677,7 +677,7 @@ private static class CopyQuotedStrategy extends Strategy { /** * Construct a Strategy that ensures the formatField has literal text - * + * * @param formatField The literal text to match */ CopyQuotedStrategy(final String formatField) { @@ -716,7 +716,7 @@ private static class CaseInsensitiveTextStrategy extends Strategy { /** * Construct a Strategy that parses a Text field - * + * * @param field The Calendar field * @param definingCalendar The Calendar to use * @param locale The Locale to use @@ -772,7 +772,7 @@ private static class NumberStrategy extends Strategy { /** * Construct a Strategy that parses a Number field - * + * * @param field The Calendar field */ NumberStrategy(final int field) { @@ -811,7 +811,7 @@ void setCalendar(final FastDateParser parser, final Calendar cal, final String v /** * Make any modifications to parsed integer - * + * * @param iValue The parsed integer * @return The modified value */ @@ -838,7 +838,7 @@ static class TimeZoneStrategy extends Strategy { /** * Construct a Strategy that parses a TimeZone - * + * * @param locale The Locale */ TimeZoneStrategy(final Locale locale) { @@ -855,7 +855,11 @@ static class TimeZoneStrategy extends Strategy { } final TimeZone tz = TimeZone.getTimeZone(tzId); for (int i = 1; i < zoneNames.length; ++i) { - final String zoneName = zoneNames[i].toLowerCase(locale); + final String currentZoneName = zoneNames[i]; + if (currentZoneName == null) { + continue; + } + final String zoneName = currentZoneName.toLowerCase(locale); if (!tzNames.containsKey(zoneName)) { tzNames.put(zoneName, tz); simpleQuote(sb.append('|'), zoneName); @@ -902,7 +906,7 @@ private static class ISO8601TimeZoneStrategy extends Strategy { /** * Construct a Strategy that parses a TimeZone - * + * * @param pattern The Pattern */ ISO8601TimeZoneStrategy(final String pattern) { @@ -936,7 +940,7 @@ void setCalendar(final FastDateParser parser, final Calendar cal, final String v /** * Factory method for ISO8601TimeZoneStrategies. - * + * * @param tokenLen a token indicating the length of the TimeZone String to be formatted. * @return a ISO8601TimeZoneStrategy that can format TimeZone String of length {@code tokenLen}. If no such * strategy exists, an IllegalArgumentException will be thrown. From bf9c397571195edf5caa6892dcd8724f1d57916d Mon Sep 17 00:00:00 2001 From: rpopma Date: Fri, 19 Feb 2016 01:09:15 +0900 Subject: [PATCH 2/4] improve error reporting when test fails --- .../log4j/core/pattern/MessagePatternConverterTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MessagePatternConverterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MessagePatternConverterTest.java index 5bce949f03b..478a9af5732 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MessagePatternConverterTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MessagePatternConverterTest.java @@ -49,7 +49,7 @@ public void testPattern() throws Exception { .setMessage(null).build(); sb = new StringBuilder(); converter.format(event, sb); - assertEquals("Incorrect length: " + sb.length(), 0, sb.length()); + assertEquals("Incorrect length: " + sb, 0, sb.length()); msg = new SimpleMessage(null); event = Log4jLogEvent.newBuilder() // .setLoggerName("MyLogger") // @@ -57,7 +57,7 @@ public void testPattern() throws Exception { .setMessage(msg).build(); sb = new StringBuilder(); converter.format(event, sb); - assertEquals("Incorrect length: " + sb.length(), 4, sb.length()); + assertEquals("Incorrect length: " + sb, 4, sb.length()); } @@ -79,7 +79,7 @@ public void testPatternWithConfiguration() throws Exception { .setMessage(null).build(); sb = new StringBuilder(); converter.format(event, sb); - assertEquals("Incorrect length: " + sb.length(), 0, sb.length()); + assertEquals("Incorrect length: " + sb, 0, sb.length()); msg = new SimpleMessage(null); event = Log4jLogEvent.newBuilder() // .setLoggerName("MyLogger") // @@ -87,6 +87,6 @@ public void testPatternWithConfiguration() throws Exception { .setMessage(msg).build(); sb = new StringBuilder(); converter.format(event, sb); - assertEquals("Incorrect length: " + sb.length(), 4, sb.length()); + assertEquals("Incorrect length: " + sb, 4, sb.length()); } } From d3487f2937c163effaedf9c545b8a17eb5487d2f Mon Sep 17 00:00:00 2001 From: rpopma Date: Fri, 19 Feb 2016 01:10:21 +0900 Subject: [PATCH 3/4] javadoc: add -XX:+PrintGCApplicationStoppedTime to recommended command line options --- .../main/java/org/apache/logging/log4j/perf/nogc/Test.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/nogc/Test.java b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/nogc/Test.java index 9e7ceaeb5f3..a36ead2210d 100644 --- a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/nogc/Test.java +++ b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/nogc/Test.java @@ -23,10 +23,10 @@ * Tests the classic Log4j2 components. *

* Run CLASSIC test (varargs, ParameterizedMessage, PatternLayout(%m)): - * java -Xms64M -Xmx64M -cp log4j-perf/target/benchmarks.jar -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps org.apache.logging.log4j.perf.nogc.Test Classic + * java -Xms64M -Xmx64M -cp log4j-perf/target/benchmarks.jar -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCApplicationStoppedTime org.apache.logging.log4j.perf.nogc.Test Classic * * Run NOGC test (unrolled varargs, StringBuilderMessage, NoGcLayout(%m)): - * java -Xms64M -Xmx64M -cp log4j-perf/target/benchmarks.jar -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps org.apache.logging.log4j.perf.nogc.Test NoGC + * java -Xms64M -Xmx64M -cp log4j-perf/target/benchmarks.jar -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCApplicationStoppedTime org.apache.logging.log4j.perf.nogc.Test NoGC * *

*/ From bf23260fe60e9cc3d0cf41829244936000bf2f6a Mon Sep 17 00:00:00 2001 From: rpopma Date: Fri, 19 Feb 2016 01:11:33 +0900 Subject: [PATCH 4/4] LOG4J2-1255 remove unnecessary AbstractMessage class --- .../log4j/message/AbstractMessage.java | 37 ------------------- .../log4j/message/AbstractMessageFactory.java | 14 +++---- .../logging/log4j/message/JsonMessage.java | 4 +- 3 files changed, 9 insertions(+), 46 deletions(-) delete mode 100644 log4j-api/src/main/java/org/apache/logging/log4j/message/AbstractMessage.java diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/AbstractMessage.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/AbstractMessage.java deleted file mode 100644 index 39815159a4d..00000000000 --- a/log4j-api/src/main/java/org/apache/logging/log4j/message/AbstractMessage.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ -package org.apache.logging.log4j.message; - -/** - * Common services for Message implementors. - * - * @since 2.6 - */ -public abstract class AbstractMessage implements Message { - - private static final long serialVersionUID = 1L; - - /** - * Converts this message to it's formatted message String. - * - * @return this message as formatted message String. - */ - @Override - public String toString() { - return getFormattedMessage(); - } -} diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/AbstractMessageFactory.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/AbstractMessageFactory.java index f3431117e71..5e2c357b639 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/message/AbstractMessageFactory.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/AbstractMessageFactory.java @@ -47,8 +47,8 @@ public AbstractMessageFactory(final String entryText, final String exitText) { private final String entryText; private final String exitText; - - private static class AbstractFlowMessage extends AbstractMessage implements FlowMessage { + + private static class AbstractFlowMessage implements FlowMessage { private static final long serialVersionUID = 1L; private final Message message; @@ -146,7 +146,7 @@ public String getFormattedMessage() { return formattedMessage + ": " + result; } } - + private static final long serialVersionUID = 1L; /** @@ -176,7 +176,7 @@ public String getExitText() { public EntryMessage newEntryMessage(final Message message) { return new SimpleEntryMessage(entryText, message); } - + /* * (non-Javadoc) * @@ -186,7 +186,7 @@ public EntryMessage newEntryMessage(final Message message) { public ExitMessage newExitMessage(final EntryMessage message) { return new SimpleExitMessage(exitText, message); } - + /* * (non-Javadoc) * @@ -196,7 +196,7 @@ public ExitMessage newExitMessage(final EntryMessage message) { public ExitMessage newExitMessage(final Object object, final EntryMessage message) { return new SimpleExitMessage(exitText, object, message); } - + /* * (non-Javadoc) * @@ -206,7 +206,7 @@ public ExitMessage newExitMessage(final Object object, final EntryMessage messag public ExitMessage newExitMessage(final Object object, final Message message) { return new SimpleExitMessage(exitText, object, message); } - + /* * (non-Javadoc) * diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/message/JsonMessage.java b/log4j-api/src/test/java/org/apache/logging/log4j/message/JsonMessage.java index b0294b65f4a..7d228b87db0 100644 --- a/log4j-api/src/test/java/org/apache/logging/log4j/message/JsonMessage.java +++ b/log4j-api/src/test/java/org/apache/logging/log4j/message/JsonMessage.java @@ -24,7 +24,7 @@ /** * Converts an Object to a JSON String. */ -public class JsonMessage extends AbstractMessage { +public class JsonMessage implements Message { private static final long serialVersionUID = 1L; private static final ObjectMapper mapper = new ObjectMapper(); @@ -32,7 +32,7 @@ public class JsonMessage extends AbstractMessage { /** * Constructs a JsonMessage. - * + * * @param object the Object to serialize. */ public JsonMessage(final Object object) {