8245306: Update sun.security.ssl.SSLLogger to use Immutable DateTimeFormatter

The fix updates sun.security.ssl.SSLLogger to use DateTimeFormatter to make it virtual thread friendly

Reviewed-by: alanb, jnimeh
This commit is contained in:
Rahul Yadav 2020-07-24 12:07:59 +01:00
parent 8b005fa74e
commit 778d8a45cb
2 changed files with 124 additions and 16 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -36,11 +36,13 @@ import java.security.cert.Certificate;
import java.security.cert.Extension;
import java.security.cert.X509Certificate;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import sun.security.action.GetPropertyAction;
import sun.security.util.HexDumpEncoder;
import sun.security.x509.*;
@ -254,13 +256,9 @@ public final class SSLLogger {
}
private static class SSLSimpleFormatter {
private static final ThreadLocal<SimpleDateFormat> dateFormat =
new ThreadLocal<SimpleDateFormat>() {
@Override protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(
"yyyy-MM-dd kk:mm:ss.SSS z", Locale.ENGLISH);
}
};
private static final String PATTERN = "yyyy-MM-dd kk:mm:ss.SSS z";
private static final DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern(PATTERN, Locale.ENGLISH)
.withZone(ZoneId.systemDefault());
private static final MessageFormat basicCertFormat = new MessageFormat(
"\"version\" : \"v{0}\",\n" +
@ -357,7 +355,7 @@ public final class SSLLogger {
level.getName(),
Utilities.toHexString(Thread.currentThread().getId()),
Thread.currentThread().getName(),
dateFormat.get().format(new Date(System.currentTimeMillis())),
dateTimeFormat.format(Instant.now()),
formatCaller(),
message
};
@ -374,7 +372,7 @@ public final class SSLLogger {
level.getName(),
Utilities.toHexString(Thread.currentThread().getId()),
Thread.currentThread().getName(),
dateFormat.get().format(new Date(System.currentTimeMillis())),
dateTimeFormat.format(Instant.now()),
formatCaller(),
message,
(logger.useCompactFormat ?
@ -476,8 +474,8 @@ public final class SSLLogger {
x509.getSerialNumber().toByteArray()),
x509.getSigAlgName(),
x509.getIssuerX500Principal().toString(),
dateFormat.get().format(x509.getNotBefore()),
dateFormat.get().format(x509.getNotAfter()),
dateTimeFormat.format(x509.getNotBefore().toInstant()),
dateTimeFormat.format(x509.getNotAfter().toInstant()),
x509.getSubjectX500Principal().toString(),
x509.getPublicKey().getAlgorithm()
};
@ -501,8 +499,8 @@ public final class SSLLogger {
x509.getSerialNumber().toByteArray()),
x509.getSigAlgName(),
x509.getIssuerX500Principal().toString(),
dateFormat.get().format(x509.getNotBefore()),
dateFormat.get().format(x509.getNotAfter()),
dateTimeFormat.format(x509.getNotBefore().toInstant()),
dateTimeFormat.format(x509.getNotAfter().toInstant()),
x509.getSubjectX500Principal().toString(),
x509.getPublicKey().getAlgorithm(),
Utilities.indent(extBuilder.toString())

View File

@ -0,0 +1,110 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8245306
* @summary Replace ThreadLocal date format with DateTimeFormatter
* @modules java.base/sun.security.ssl:+open
* @compile LoggerDateFormatterTest.java
* @run testng/othervm -Djavax.net.debug=all LoggerDateFormatterTest
*/
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import sun.security.ssl.SSLLogger;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.System.out;
import static org.testng.Assert.fail;
public class LoggerDateFormatterTest {
SSLPrintStream sslStream;
static String year = "(\\|\\d\\d\\d\\d-\\d\\d-\\d\\d";
static String hour = "\\s\\d\\d:\\d\\d:\\d\\d\\.\\d\\d\\d\\s";
static String zone = "\\w\\w\\w\\|)";
static Pattern pattern;
Matcher matcher;
@BeforeTest
public void setUp() {
sslStream = new SSLPrintStream(System.err);
System.setErr(sslStream);
String format = year + hour + zone;
pattern = Pattern.compile(format);
}
@Test
public void testDateFormat() {
SSLLogger.info("logging");
System.out.println("The value is: " + sslStream.bos.toString());
matcher = pattern.matcher(sslStream.bos.toString());
if (matcher.find()) {
out.println("Test Passed with value :" + matcher.group());
}
else {
fail("Test failed wrong SSL DateFormat");
}
}
public static class SSLPrintStream extends PrintStream {
public ByteArrayOutputStream bos; // Stream that accumulates System.err
public SSLPrintStream(OutputStream out) {
super(out);
bos = new ByteArrayOutputStream();
}
@Override
public void write(int b) {
super.write(b);
bos.write(b);
}
@Override
public void write(byte[] buf, int off, int len) {
super.write(buf, off, len);
bos.write(buf, off, len);
}
@Override
public void write(byte[] buf) throws IOException {
super.write(buf);
bos.write(buf);
}
@Override
public void writeBytes(byte[] buf) {
super.writeBytes(buf);
bos.writeBytes(buf);
}
}
}