Skip to content

Commit f0ce1b7

Browse files
committed
Replace use of Date with OffsetDateTime and Instant in Actuator
Closes spring-projectsgh-10976
1 parent 270acd4 commit f0ce1b7

File tree

29 files changed

+147
-157
lines changed

29 files changed

+147
-157
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@
353353
<artifactId>logback-classic</artifactId>
354354
<scope>test</scope>
355355
</dependency>
356+
<dependency>
357+
<groupId>com.fasterxml.jackson.datatype</groupId>
358+
<artifactId>jackson-datatype-jsr310</artifactId>
359+
</dependency>
356360
<dependency>
357361
<groupId>io.projectreactor</groupId>
358362
<artifactId>reactor-test</artifactId>

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/AuditEventsEndpointDocumentationTests.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,10 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.endpoint.web.documentation;
1818

19-
import java.time.ZonedDateTime;
19+
import java.time.OffsetDateTime;
2020
import java.time.format.DateTimeFormatter;
2121
import java.util.Arrays;
2222
import java.util.Collections;
23-
import java.util.Date;
2423

2524
import org.junit.Test;
2625

@@ -74,10 +73,9 @@ public void allAuditEventsAfter() throws Exception {
7473

7574
@Test
7675
public void filteredAuditEvents() throws Exception {
77-
ZonedDateTime now = ZonedDateTime.now();
76+
OffsetDateTime now = OffsetDateTime.now();
7877
String queryTimestamp = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(now);
79-
Date date = new Date(now.toEpochSecond() * 1000);
80-
given(this.repository.find("alice", date, "logout")).willReturn(
78+
given(this.repository.find("alice", now.toInstant(), "logout")).willReturn(
8179
Arrays.asList(new AuditEvent("alice", "logout", Collections.emptyMap())));
8280
this.mockMvc
8381
.perform(get("/actuator/auditevents").param("principal", "alice")
@@ -94,7 +92,7 @@ public void filteredAuditEvents() throws Exception {
9492
parameterWithName("type").description(
9593
"Restricts the events to those with the given "
9694
+ "type. Optional."))));
97-
verify(this.repository).find("alice", date, "logout");
95+
verify(this.repository).find("alice", now.toInstant(), "logout");
9896
}
9997

10098
@Configuration

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,12 +17,11 @@
1717
package org.springframework.boot.actuate.audit;
1818

1919
import java.io.Serializable;
20+
import java.time.Instant;
2021
import java.util.Collections;
21-
import java.util.Date;
2222
import java.util.HashMap;
2323
import java.util.Map;
2424

25-
import com.fasterxml.jackson.annotation.JsonFormat;
2625
import com.fasterxml.jackson.annotation.JsonInclude;
2726
import com.fasterxml.jackson.annotation.JsonInclude.Include;
2827

@@ -46,7 +45,7 @@
4645
@JsonInclude(Include.NON_EMPTY)
4746
public class AuditEvent implements Serializable {
4847

49-
private final Date timestamp;
48+
private final Instant timestamp;
5049

5150
private final String principal;
5251

@@ -61,7 +60,7 @@ public class AuditEvent implements Serializable {
6160
* @param data The event data
6261
*/
6362
public AuditEvent(String principal, String type, Map<String, Object> data) {
64-
this(new Date(), principal, type, data);
63+
this(Instant.now(), principal, type, data);
6564
}
6665

6766
/**
@@ -72,7 +71,7 @@ public AuditEvent(String principal, String type, Map<String, Object> data) {
7271
* @param data The event data in the form 'key=value' or simply 'key'
7372
*/
7473
public AuditEvent(String principal, String type, String... data) {
75-
this(new Date(), principal, type, convert(data));
74+
this(Instant.now(), principal, type, convert(data));
7675
}
7776

7877
/**
@@ -82,7 +81,7 @@ public AuditEvent(String principal, String type, String... data) {
8281
* @param type the event type
8382
* @param data The event data
8483
*/
85-
public AuditEvent(Date timestamp, String principal, String type,
84+
public AuditEvent(Instant timestamp, String principal, String type,
8685
Map<String, Object> data) {
8786
Assert.notNull(timestamp, "Timestamp must not be null");
8887
Assert.notNull(type, "Type must not be null");
@@ -107,11 +106,10 @@ private static Map<String, Object> convert(String[] data) {
107106
}
108107

109108
/**
110-
* Returns the date/time that the even was logged.
111-
* @return the time stamp
109+
* Returns the date/time that the event was logged.
110+
* @return the timestamp
112111
*/
113-
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssZ")
114-
public Date getTimestamp() {
112+
public Instant getTimestamp() {
115113
return this.timestamp;
116114
}
117115

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEventRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.actuate.audit;
1818

19-
import java.util.Date;
19+
import java.time.Instant;
2020
import java.util.List;
2121

2222
/**
@@ -43,6 +43,6 @@ public interface AuditEventRepository {
4343
* @return audit events of specified type relating to the principal
4444
* @since 1.4.0
4545
*/
46-
List<AuditEvent> find(String principal, Date after, String type);
46+
List<AuditEvent> find(String principal, Instant after, String type);
4747

4848
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEventsEndpoint.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.actuate.audit;
1818

19-
import java.util.Date;
19+
import java.time.OffsetDateTime;
2020
import java.util.List;
2121

2222
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
@@ -40,10 +40,10 @@ public AuditEventsEndpoint(AuditEventRepository auditEventRepository) {
4040
}
4141

4242
@ReadOperation
43-
public AuditEventsDescriptor eventsWithPrincipalDateAfterAndType(String principal,
44-
Date after, String type) {
45-
return new AuditEventsDescriptor(
46-
this.auditEventRepository.find(principal, after, type));
43+
public AuditEventsDescriptor events(String principal, OffsetDateTime after,
44+
String type) {
45+
return new AuditEventsDescriptor(this.auditEventRepository.find(principal,
46+
after == null ? null : after.toInstant(), type));
4747
}
4848

4949
/**

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEventsEndpointWebExtension.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.actuate.audit;
1818

19-
import java.util.Date;
19+
import java.time.OffsetDateTime;
2020

2121
import org.springframework.boot.actuate.audit.AuditEventsEndpoint.AuditEventsDescriptor;
2222
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
@@ -40,10 +40,9 @@ public AuditEventsEndpointWebExtension(AuditEventsEndpoint delegate) {
4040
}
4141

4242
@ReadOperation
43-
public WebEndpointResponse<AuditEventsDescriptor> eventsWithPrincipalDateAfterAndType(
44-
@Nullable String principal, Date after, @Nullable String type) {
45-
AuditEventsDescriptor auditEvents = this.delegate
46-
.eventsWithPrincipalDateAfterAndType(principal, after, type);
43+
public WebEndpointResponse<AuditEventsDescriptor> events(@Nullable String principal,
44+
OffsetDateTime after, @Nullable String type) {
45+
AuditEventsDescriptor auditEvents = this.delegate.events(principal, after, type);
4746
return new WebEndpointResponse<>(auditEvents);
4847
}
4948

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEventsJmxEndpointExtension.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.actuate.audit;
1818

19-
import java.util.Date;
19+
import java.time.OffsetDateTime;
2020

2121
import org.springframework.boot.actuate.audit.AuditEventsEndpoint.AuditEventsDescriptor;
2222
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
@@ -39,15 +39,14 @@ public AuditEventsJmxEndpointExtension(AuditEventsEndpoint delegate) {
3939
}
4040

4141
@ReadOperation
42-
public AuditEventsDescriptor eventsWithDateAfter(Date dateAfter) {
43-
return this.delegate.eventsWithPrincipalDateAfterAndType(null, dateAfter, null);
42+
public AuditEventsDescriptor eventsAfter(OffsetDateTime after) {
43+
return this.delegate.events(null, after, null);
4444
}
4545

4646
@ReadOperation
47-
public AuditEventsDescriptor eventsWithPrincipalAndDateAfter(String principal,
48-
Date dateAfter) {
49-
return this.delegate.eventsWithPrincipalDateAfterAndType(principal, dateAfter,
50-
null);
47+
public AuditEventsDescriptor eventsWithPrincipalAndAfter(String principal,
48+
OffsetDateTime after) {
49+
return this.delegate.events(principal, after, null);
5150
}
5251

5352
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/InMemoryAuditEventRepository.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.actuate.audit;
1818

19-
import java.util.Date;
19+
import java.time.Instant;
2020
import java.util.LinkedList;
2121
import java.util.List;
2222

@@ -70,7 +70,7 @@ public void add(AuditEvent event) {
7070
}
7171

7272
@Override
73-
public List<AuditEvent> find(String principal, Date after, String type) {
73+
public List<AuditEvent> find(String principal, Instant after, String type) {
7474
LinkedList<AuditEvent> events = new LinkedList<>();
7575
synchronized (this.monitor) {
7676
for (int i = 0; i < this.events.length; i++) {
@@ -83,10 +83,11 @@ public List<AuditEvent> find(String principal, Date after, String type) {
8383
return events;
8484
}
8585

86-
private boolean isMatch(String principal, Date after, String type, AuditEvent event) {
86+
private boolean isMatch(String principal, Instant after, String type,
87+
AuditEvent event) {
8788
boolean match = true;
8889
match = match && (principal == null || event.getPrincipal().equals(principal));
89-
match = match && (after == null || event.getTimestamp().compareTo(after) >= 0);
90+
match = match && (after == null || event.getTimestamp().isAfter(after));
9091
match = match && (type == null || event.getType().equals(type));
9192
return match;
9293
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/listener/AuditApplicationEvent.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.actuate.audit.listener;
1818

19-
import java.util.Date;
19+
import java.time.Instant;
2020
import java.util.Map;
2121

2222
import org.springframework.boot.actuate.audit.AuditEvent;
@@ -60,13 +60,13 @@ public AuditApplicationEvent(String principal, String type, String... data) {
6060
/**
6161
* Create a new {@link AuditApplicationEvent} that wraps a newly created
6262
* {@link AuditEvent}.
63-
* @param timestamp the time stamp
63+
* @param timestamp the timestamp
6464
* @param principal the principal
6565
* @param type the event type
6666
* @param data the event data
67-
* @see AuditEvent#AuditEvent(Date, String, String, Map)
67+
* @see AuditEvent#AuditEvent(Instant, String, String, Map)
6868
*/
69-
public AuditApplicationEvent(Date timestamp, String principal, String type,
69+
public AuditApplicationEvent(Instant timestamp, String principal, String type,
7070
Map<String, Object> data) {
7171
this(new AuditEvent(timestamp, principal, type, data));
7272
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/convert/IsoOffsetDateTimeConverter.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,29 +18,25 @@
1818

1919
import java.time.OffsetDateTime;
2020
import java.time.format.DateTimeFormatter;
21-
import java.util.Date;
22-
import java.util.concurrent.TimeUnit;
2321

2422
import org.springframework.core.convert.converter.Converter;
2523
import org.springframework.core.convert.converter.ConverterRegistry;
2624
import org.springframework.util.StringUtils;
2725

2826
/**
29-
* A {@link String} to {@link Date} {@link Converter} that uses
27+
* A {@link String} to {@link OffsetDateTime} {@link Converter} that uses
3028
* {@link DateTimeFormatter#ISO_OFFSET_DATE_TIME ISO offset} parsing.
3129
*
3230
* @author Andy Wilkinson
3331
* @author Stephane Nicoll
3432
* @since 2.0.0
3533
*/
36-
public class IsoOffsetDateTimeConverter implements Converter<String, Date> {
34+
public class IsoOffsetDateTimeConverter implements Converter<String, OffsetDateTime> {
3735

3836
@Override
39-
public Date convert(String source) {
37+
public OffsetDateTime convert(String source) {
4038
if (StringUtils.hasLength(source)) {
41-
OffsetDateTime offsetDateTime = OffsetDateTime.parse(source,
42-
DateTimeFormatter.ISO_OFFSET_DATE_TIME);
43-
return new Date(TimeUnit.SECONDS.toMillis(offsetDateTime.toEpochSecond()));
39+
return OffsetDateTime.parse(source, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
4440
}
4541
return null;
4642
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/annotation/JmxEndpointOperationFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,9 +18,9 @@
1818

1919
import java.lang.reflect.Method;
2020
import java.lang.reflect.Parameter;
21+
import java.time.Instant;
2122
import java.util.ArrayList;
2223
import java.util.Collections;
23-
import java.util.Date;
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.function.Supplier;
@@ -113,7 +113,7 @@ private Class<?> getJmxType(Class<?> type) {
113113
if (type.isEnum()) {
114114
return String.class;
115115
}
116-
if (Date.class.isAssignableFrom(type)) {
116+
if (Instant.class.isAssignableFrom(type)) {
117117
return String.class;
118118
}
119119
if (type.getName().startsWith("java.")) {

0 commit comments

Comments
 (0)