Skip to content

Commit 57feebf

Browse files
authored
Change licence expiration date pattern Backport(#39681) #39782
Due to migration from joda to java.time licence expiration 'full date' format has to use 4-char pattern (MMMM). Also since jdk9 the date with ROOT locale will still return abbreviated days and month names. closes #39136 backport #39681
1 parent a5600e0 commit 57feebf

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseService.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public class LicenseService extends AbstractLifecycleComponent implements Cluste
114114

115115
public static final String LICENSE_JOB = "licenseJob";
116116

117-
private static final DateFormatter DATE_FORMATTER = DateFormatter.forPattern("EEEE, MMMMM dd, yyyy");
117+
private static final DateFormatter DATE_FORMATTER = DateFormatter.forPattern("EEEE, MMMM dd, yyyy");
118118

119119
private static final String ACKNOWLEDGEMENT_HEADER = "This license update requires acknowledgement. To acknowledge the license, " +
120120
"please read the following messages and update the license again, this time with the \"acknowledge=true\" parameter:";
@@ -134,11 +134,15 @@ public LicenseService(Settings settings, ClusterService clusterService, Clock cl
134134
}
135135

136136
private void logExpirationWarning(long expirationMillis, boolean expired) {
137+
logger.warn("{}", buildExpirationMessage(expirationMillis, expired));
138+
}
139+
140+
static CharSequence buildExpirationMessage(long expirationMillis, boolean expired) {
137141
String expiredMsg = expired ? "expired" : "will expire";
138142
String general = LoggerMessageFormat.format(null, "License [{}] on [{}].\n" +
139-
"# If you have a new license, please update it. Otherwise, please reach out to\n" +
140-
"# your support contact.\n" +
141-
"# ", expiredMsg, DATE_FORMATTER.formatMillis(expirationMillis));
143+
"# If you have a new license, please update it. Otherwise, please reach out to\n" +
144+
"# your support contact.\n" +
145+
"# ", expiredMsg, DATE_FORMATTER.formatMillis(expirationMillis));
142146
if (expired) {
143147
general = general.toUpperCase(Locale.ROOT);
144148
}
@@ -161,7 +165,7 @@ private void logExpirationWarning(long expirationMillis, boolean expired) {
161165
}
162166
}
163167
});
164-
logger.warn("{}", builder);
168+
return builder;
165169
}
166170

167171
private void populateExpirationCallbacks() {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.license;
7+
8+
9+
import org.elasticsearch.bootstrap.JavaVersion;
10+
import org.elasticsearch.test.ESTestCase;
11+
12+
import java.time.LocalDate;
13+
import java.time.ZoneOffset;
14+
15+
import static org.hamcrest.Matchers.startsWith;
16+
17+
/**
18+
* Due to changes in JDK9 where locale data is used from CLDR, the licence message will differ in jdk 8 and jdk9+
19+
* https://openjdk.java.net/jeps/252
20+
*/
21+
public class LicenseServiceTests extends ESTestCase {
22+
23+
public void testLogExpirationWarningOnJdk9AndNewer() {
24+
assumeTrue("this is for JDK9+", JavaVersion.current().compareTo(JavaVersion.parse("9")) >= 0);
25+
26+
long time = LocalDate.of(2018, 11, 15).atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli();
27+
final boolean expired = randomBoolean();
28+
final String message = LicenseService.buildExpirationMessage(time, expired).toString();
29+
if (expired) {
30+
assertThat(message, startsWith("LICENSE [EXPIRED] ON [THU, NOV 15, 2018].\n"));
31+
} else {
32+
assertThat(message, startsWith("License [will expire] on [Thu, Nov 15, 2018].\n"));
33+
}
34+
}
35+
36+
public void testLogExpirationWarningOnJdk8() {
37+
assumeTrue("this is for JDK8 only", JavaVersion.current().equals(JavaVersion.parse("8")));
38+
39+
long time = LocalDate.of(2018, 11, 15).atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli();
40+
final boolean expired = randomBoolean();
41+
final String message = LicenseService.buildExpirationMessage(time, expired).toString();
42+
if (expired) {
43+
assertThat(message, startsWith("LICENSE [EXPIRED] ON [THURSDAY, NOVEMBER 15, 2018].\n"));
44+
} else {
45+
assertThat(message, startsWith("License [will expire] on [Thursday, November 15, 2018].\n"));
46+
}
47+
}
48+
49+
}

0 commit comments

Comments
 (0)