Skip to content

Commit ad7f27d

Browse files
committed
Change to use jaxb4 (javax -> jakarta), uplift xsd to 4.1.0
Change Date to Instant Add module-info Change module name
1 parent f6168e9 commit ad7f27d

36 files changed

+588
-96
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ buildscript {
1313
plugins {
1414
//plugin for downloading content from the 'net
1515
id "de.undercouch.download" version "3.4.3"
16-
id "io.github.gradle-nexus.publish-plugin" version "1.0.0"
16+
id "io.github.gradle-nexus.publish-plugin" version "1.2.0"
1717
//plugin for producing a tree of task dependencies, run task 'taskTree'
1818
id "com.dorongold.task-tree" version "1.5"
1919
id "signing"
@@ -67,7 +67,7 @@ ext.getMajorVersion = { versionStr ->
6767

6868
// Set this to the desired release version of the event-logging XML schema on github
6969
// *****************************************************************************
70-
def eventLoggingSchemaVer = "v4.0.0"
70+
def eventLoggingSchemaVer = "v4.1.0"
7171
// *****************************************************************************
7272

7373
// Set this to the last release of this repo on this branch, or earlier branches
@@ -246,7 +246,7 @@ subprojects {
246246

247247
// This means the reports from our integration tests won't over-write the reports from our unit tests.
248248
tasks.withType(Test) {
249-
reports.html.destination = file("${reporting.baseDir}/${name}")
249+
reports.html.outputLocation = file("${reporting.baseDir}/${name}")
250250

251251
//Use full logging for test exceptions so we can see where the failure occurred
252252
testLogging {

event-logging-api/build.gradle

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ apply plugin: 'maven-publish'
33
apply plugin: 'signing'
44
apply plugin: 'io.swagger.core.v3.swagger-gradle-plugin'
55

6-
ext.moduleName = 'event.logging.api'
6+
ext.moduleName = 'uk.gov.gchq.eventlogging'
77

88
def schemaDir = project.file('schema')
99

1010
// We want a jar like event-logging-5.0-beta.16_schema-v4.0-beta.3.jar,
1111
// not event-logging-api-5.0-beta.16_schema-v4.0-beta.3.jar
12-
archivesBaseName = "event-logging"
12+
base.archivesName = "event-logging"
1313

1414
dependencies {
1515
implementation libs.jaxb_api
@@ -61,6 +61,8 @@ jar {
6161
)
6262
}
6363
version versions.eventLogging
64+
// We want a jar like event-logging-5.0-beta.16_schema-v4.0-beta.3.jar,
65+
// not event-logging-api-5.0-beta.16_schema-v4.0-beta.3.jar
6466
}
6567

6668
javadoc {
@@ -188,12 +190,13 @@ tasks.build.dependsOn diffAgainstLatest
188190
task runExampleAppBuild(type: GradleBuild) {
189191
dependsOn publishToMavenLocal
190192

191-
buildFile = '../example-logged-application/build.gradle'
193+
// Defaults to build.gradle
194+
dir = '../example-logged-application'
192195
tasks = ['clean', 'build']
193196
startParameter.projectProperties = [mavenVersion: projectVersionForMaven]
194197

195198
doFirst {
196-
println "Running separate example application build [$buildFile]"
199+
println "Running separate example application build [$dir]"
197200
}
198201
}
199202

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module uk.gov.gchq.eventlogging {
2+
3+
exports event.logging;
4+
exports event.logging.impl;
5+
exports event.logging.jaxb;
6+
exports event.logging.jaxb.fluent;
7+
exports event.logging.util;
8+
9+
requires transitive jakarta.xml.bind;
10+
requires java.xml;
11+
requires org.slf4j;
12+
13+
opens event.logging to jakarta.xml.bind;
14+
}

event-logging-base/src/main/java/event/logging/base/EventLoggingService.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import event.logging.EventTime;
2222
import event.logging.Purpose;
2323

24-
import java.util.Date;
24+
import java.time.Instant;
2525
import java.util.function.Supplier;
2626

2727
/**
@@ -34,7 +34,7 @@ public interface EventLoggingService {
3434
* implementation being used. If this method is not implemented it will return an empty event by default.
3535
*
3636
* Using {@link EventLoggingService#createEvent(String, String, EventAction)} should be preferred.
37-
*
37+
*
3838
* @return An event that is ready to have additional properties set.
3939
*/
4040

@@ -77,7 +77,7 @@ default Event createEvent(final String typeId,
7777
final EventAction eventAction) {
7878
return Event.builder()
7979
.withEventTime(EventTime.builder()
80-
.withTimeCreated(new Date())
80+
.withTimeCreated(Instant.now())
8181
.build())
8282
.withEventDetail(EventDetail.builder()
8383
.withTypeId(typeId)
@@ -90,7 +90,7 @@ default Event createEvent(final String typeId,
9090

9191
/**
9292
* Logs an event.
93-
*
93+
*
9494
* @param event The event to log.
9595
*/
9696
void log(Event event);
@@ -139,17 +139,17 @@ default void log(final String typeId,
139139
* Set to true if the event logging service should validate the output XML against the schema. This option helps
140140
* identify areas of code that are producing invalid data. For performance reasons it is recommended that
141141
* validation is not performed in production.
142-
*
142+
*
143143
* If validate is set to null then the system property shall be used to determine if validation is performed.
144-
*
144+
*
145145
* @param validate
146146
* The validation flag.
147147
*/
148148
void setValidate(Boolean validate);
149149

150150
/**
151151
* Use to determine if the event logging service is set to validate output data against the XML schema.
152-
*
152+
*
153153
* @return True if the validate flag is set.
154154
*/
155155
boolean isValidate();

event-logging-base/src/main/java/event/logging/base/impl/DefaultEventLoggingService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public DefaultEventLoggingService(ErrorHandler schemaValidationErrorHandler,
8383
public void log(final Event event) {
8484
final String data = eventSerializer.serialize(event);
8585
final String trimmed = data.trim();
86-
if (trimmed.length() > 0) {
86+
if (!trimmed.isEmpty()) {
8787
// Validate data here if the configuration option is set.
8888
if (checkValidating()) {
8989
xmlValidator.validate(trimmed);
@@ -109,7 +109,7 @@ private boolean checkValidating() {
109109

110110
// If we aren't setting validate on .
111111
final String val = System.getProperty(VALIDATE);
112-
return Boolean.valueOf(val);
112+
return Boolean.parseBoolean(val);
113113
}
114114

115115
/**

event-logging-base/src/main/java/event/logging/base/impl/DefaultEventSerializer.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
import event.logging.Event;
1919
import jakarta.xml.bind.JAXBContext;
2020
import jakarta.xml.bind.Marshaller;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
2123

2224
import java.io.StringWriter;
2325

2426
public class DefaultEventSerializer implements EventSerializer {
25-
private static JAXBContext context;
27+
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultEventSerializer.class);
2628

2729
@Override
2830
public String serialize(final Event event) {
@@ -54,15 +56,26 @@ private static Marshaller getMarshaller() {
5456
}
5557
}
5658

57-
private synchronized static JAXBContext getContext() {
58-
try {
59-
if (context == null) {
60-
context = JAXBContext.newInstance(Event.class);
61-
}
59+
private static JAXBContext getContext() {
60+
// Initialize-on-Demand, Holder Class Idiom
61+
return Holder.JAXB_CONTEXT;
62+
}
6263

63-
return context;
64+
private static JAXBContext createContext() {
65+
try {
66+
LOGGER.info("Creating JAXB context");
67+
return JAXBContext.newInstance(Event.class);
6468
} catch (final Exception e) {
6569
throw new RuntimeException(e.getMessage(), e);
6670
}
6771
}
72+
73+
74+
// --------------------------------------------------------------------------------
75+
76+
77+
// Initialize-on-Demand, Holder Class Idiom
78+
private static class Holder {
79+
private static final JAXBContext JAXB_CONTEXT = createContext();
80+
}
6881
}

event-logging-base/src/main/java/event/logging/base/impl/EventLoggerBuilderImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,15 @@ String getDescription() {
6666
return description;
6767
}
6868

69+
@SuppressWarnings("unchecked")
6970
@Override
7071
public <T extends EventAction> EventLoggerBuilder.WorkStep<T> withDefaultEventAction(
7172
final T defaultEventAction) {
7273

7374
// At this point we are moving from the builder having unknown type to it having a known
7475
// EventAction so type casts are unavoidable.
7576

76-
//noinspection unchecked
7777
this.eventAction = (T_EVENT_ACTION) defaultEventAction;
78-
//noinspection unchecked
7978
return (EventLoggerBuilder.WorkStep<T>) this;
8079
}
8180

@@ -374,7 +373,7 @@ public void runActionAndLog() {
374373
basicBuilder.description,
375374
basicBuilder.purpose,
376375
basicBuilder.eventAction,
377-
loggedAction::apply,
376+
loggedAction,
378377
basicBuilder.exceptionHandler,
379378
basicBuilder.isLogEventRequired);
380379
}

event-logging-base/src/main/java/event/logging/base/jaxb/DateAdaptor.java renamed to event-logging-base/src/main/java/event/logging/base/jaxb/InstantAdapter.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,32 @@
2020
import org.slf4j.LoggerFactory;
2121

2222
import java.text.ParseException;
23-
import java.util.Date;
23+
import java.time.Instant;
2424

25-
public class DateAdaptor {
26-
private static final Logger LOGGER = LoggerFactory.getLogger(DateAdaptor.class);
25+
public class InstantAdapter {
26+
private static final Logger LOGGER = LoggerFactory.getLogger(InstantAdapter.class);
2727

28-
public static Date parseDate(final String string) {
28+
public static Instant parseDate(final String string) {
2929
try {
30-
return new Date(DateUtil.parseDateTimeString(string));
30+
final Long millis = DateUtil.parseDateTimeString(string);
31+
return millis == null
32+
? null
33+
: Instant.ofEpochMilli(millis);
3134
} catch (final ParseException e) {
3235
LOGGER.error(e.getMessage(), e);
3336
}
34-
3537
return null;
3638
}
3739

38-
public static String printDate(final Date date) {
40+
/**
41+
* @param date
42+
* @return The instant in the form "yyyy-MM-dd HH:mm:ss.SSS zzz"
43+
*/
44+
public static String printDate(final Instant date) {
3945
if (date == null) {
4046
return null;
4147
}
4248

43-
return DateUtil.createNormalDateTimeString(date.getTime());
49+
return DateUtil.createNormalDateTimeString(date.toEpochMilli());
4450
}
4551
}

event-logging-base/src/main/java/event/logging/base/util/DateUtil.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private DateUtil() {
4949

5050
/**
5151
* Create a 'file' format date string.
52-
*
52+
*
5353
* @param ms
5454
* The date to create the string for.
5555
* @return string The date as a 'file' format date string.
@@ -67,7 +67,7 @@ public static String createFileDateTimeString(final Long ms) {
6767

6868
/**
6969
* Create a 'normal' format date string.
70-
*
70+
*
7171
* @param ms
7272
* The date to create the string for.
7373
* @return string The date as a 'normal' format date string.
@@ -85,7 +85,7 @@ public static String createNormalDateTimeString(final Long ms) {
8585

8686
/**
8787
* Creates a string from a date given the appropriate time format and millisecond separator.
88-
*
88+
*
8989
* @param ms
9090
* The date to use.
9191
* @param timeFormat
@@ -122,7 +122,7 @@ private static String createDateTimeString(final long ms, final DateFormat timeF
122122
* @return A Date object set to the supplied date.
123123
*/
124124
public static Long parseDateTimeString(final String date) throws ParseException {
125-
if (NULL.equals(date) || date == null || date.length() == 0) {
125+
if (NULL.equals(date) || date == null || date.isEmpty()) {
126126
return null;
127127
}
128128

@@ -143,7 +143,7 @@ public static Long parseDateTimeString(final String date) throws ParseException
143143
* Utility method for padding a string to a certain number of characters. Characters are appended to the left hand
144144
* side of the string to make it the specified length. IF the string is already longer than the specified length is
145145
* is just returned and not truncated.
146-
*
146+
*
147147
* @param str
148148
* The string to pad.
149149
* @param size

event-logging-base/src/main/java/event/logging/base/util/EventLoggingUtil.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import event.logging.User;
2626

2727
import java.lang.reflect.InvocationTargetException;
28+
import java.time.Instant;
2829
import java.util.Date;
2930

3031
public final class EventLoggingUtil {
@@ -33,12 +34,32 @@ private EventLoggingUtil() {
3334
// Utility class.
3435
}
3536

37+
/**
38+
* Use {@link EventLoggingUtil#createEventTime(Instant)} instead.
39+
*/
40+
@Deprecated(forRemoval = true)
3641
public static EventTime createEventTime(final Date date) {
42+
final Instant instant = date != null
43+
? date.toInstant()
44+
: null;
45+
return createEventTime(instant);
46+
}
47+
48+
public static EventTime createEventTime(final Instant date) {
3749
return EventTime.builder()
3850
.withTimeCreated(date)
3951
.build();
4052
}
4153

54+
/**
55+
* @return An EventTime for the current time, i.e. {@link Instant#now()}
56+
*/
57+
public static EventTime createCurrentEventTime() {
58+
return EventTime.builder()
59+
.withTimeCreated(Instant.now())
60+
.build();
61+
}
62+
4263
public static User createUser(final String userId) {
4364
return User.builder()
4465
.withId(userId)

0 commit comments

Comments
 (0)