Skip to content

Commit

Permalink
Change generated code to use jakarta.xml, uplift to J11
Browse files Browse the repository at this point in the history
  • Loading branch information
at055612 committed Jun 5, 2024
1 parent 606c227 commit 2b88c4f
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 68 deletions.
29 changes: 15 additions & 14 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ext.isPropertySet = { propName ->
}
}

ext.ensurePropertyIsSet = { propName ->
ext.ensurePropertyIsSet = { propName ->
if (!isPropertySet(propName)) {
throw new GradleException(
"Expecting project property [${propName}] or env var [ORG_GRADLE_PROJECT_${propName}] to be set.")
Expand All @@ -45,7 +45,7 @@ ext.ensurePropertyIsSet = { propName ->

//if the project has a value for the passed property (i.e from the cmd line via -PpropName=xxx)
//use that, else use a default value
ext.getPropertyOrDefault = { propName, defaultValue ->
ext.getPropertyOrDefault = { propName, defaultValue ->
def val;
if (isPropertySet(propName)) {
val = project.getProperty(propName)
Expand Down Expand Up @@ -75,7 +75,7 @@ def eventLoggingSchemaVer = "v4.0.0"
// see if/how the java model has changed following schema changes or changes
// to the jaxb code generation.
// *****************************************************************************
ext.previousReleaseVersion = "v5.0-beta.32_schema-v4.0-beta.10"
ext.previousReleaseVersion = "v5.0.3_schema-v4.0.0"
// *****************************************************************************


Expand Down Expand Up @@ -165,10 +165,10 @@ ext.versions = [
]

// NOTE
// Using beta versions of jaxb libs as they resolve the split pkg problem between jaxb-core and jaxb-runtime
// for running on j9+
// >= v2.3.2 of the jaxb bom pull in the jakarta.xml.bind deps where as the v2.4.0-b one & <v2.3.2 use the old
// javax.xml.bind one.
// XJC code gen is still being done using JAXB 2, but we find/replace javax.xml => jakarta.xml
// so the published -api module can have JAXB 4 deps. Thus there is a mix of JAXB 2/4 deps
// below. Once jaxb-rich-contract-plugin is fixed so we can use its v4.2.0.0+ we can switch
// to all JAXB 4 deps (see https://github.com/mklemm/jaxb-rich-contract-plugin/issues/78)

ext.libs = [
assertj_core : "org.assertj:assertj-core:3.20.2",
Expand All @@ -177,12 +177,13 @@ ext.libs = [
jackson_databind : "com.fasterxml.jackson.core:jackson-databind:2.13.3",
jakarta_servlet_api : "jakarta.servlet:jakarta.servlet-api:4.0.4",
jakarta_ws_rs_api : "jakarta.ws.rs:jakarta.ws.rs-api:2.1.6",
jaxb_api : "javax.xml.bind:jaxb-api", // Version set by jaxb_bom
jaxb_api : "jakarta.xml.bind:jakarta.xml.bind-api:4.0.2",
jaxb_basics : "org.jvnet.jaxb2_commons:jaxb2-basics:0.12.0",
jaxb_rich_contract_plugin : "net.codesup.util:jaxb2-rich-contract-plugin:2.1.0",
jaxb_runtime : "org.glassfish.jaxb:jaxb-runtime", // Version set by jaxb_bom
jaxb_runtime_2 : "org.glassfish.jaxb:jaxb-runtime", // Version set by jaxb_bom
jaxb_impl : "com.sun.xml.bind:jaxb-impl:4.0.4",
jaxb_xjc : "org.glassfish.jaxb:jaxb-xjc", // Version set by jaxb_bom
jaxb_bom : "org.glassfish.jaxb:jaxb-bom:2.4.0-b180830.0438",
jaxb_bom_2 : "org.glassfish.jaxb:jaxb-bom:2.4.0-b180830.0438",
junit_bom : "org.junit:junit-bom:5.10.0",
junit_jupiter_api : "org.junit.jupiter:junit-jupiter-api", // ver controlled by junit_bom
junit_jupiter_engine : "org.junit.jupiter:junit-jupiter-engine", // ver controlled by junit_bom
Expand All @@ -198,7 +199,7 @@ ext.libs = [
]

// NOTE
//
//

allprojects {
group "uk.gov.gchq.eventlogging" // no spaces as java pkg name convention
Expand All @@ -217,13 +218,13 @@ subprojects {

java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
languageVersion = JavaLanguageVersion.of(11)
vendor = JvmVendorSpec.ADOPTIUM
}
}

dependencies {
implementation platform(libs.jaxb_bom)
implementation platform(libs.jaxb_bom_2)
}

//configurations {
Expand Down Expand Up @@ -253,7 +254,7 @@ subprojects {
tasks.withType(Test) {
reports.html.destination = file("${reporting.baseDir}/${name}")

//Use full logging for test exceptions so we can see where the failure occurred
//Use full logging for test exceptions so we can see where the failure occurred
testLogging {
events "failed"
exceptionFormat = 'full'
Expand Down
5 changes: 2 additions & 3 deletions event-logging-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ dependencies {
implementation libs.jakarta_ws_rs_api
implementation libs.jaxb_api
implementation libs.swagger_annotations
// The JAXB implementation required for event serialisation to XML
runtimeOnly libs.jaxb_impl

// The production code uses the SLF4J logging API at compile time
implementation libs.slf4j_api

// The JAXB implementation required for event serialisation to XML
runtimeOnly libs.jaxb_runtime

testImplementation libs.classgraph
testImplementation(platform(libs.junit_bom))
testImplementation libs.junit_jupiter_api
Expand Down
1 change: 1 addition & 0 deletions event-logging-base/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
implementation libs.jaxb_api
implementation libs.slf4j_api
implementation libs.swagger_annotations
runtimeOnly libs.jaxb_impl

testImplementation libs.classgraph
testImplementation(platform(libs.junit_bom))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
package event.logging.base.impl;

import event.logging.Event;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.Marshaller;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class DefaultEventSerializer implements EventSerializer {
private static JAXBContext context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package event.logging.base;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Payload")
public class Payload {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,12 @@
import event.logging.TermCondition;
import event.logging.User;
import event.logging.base.EventLoggingService;
import event.logging.base.Payload;
import event.logging.util.DeviceUtil;
import event.logging.util.EventLoggingUtil;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -585,24 +577,24 @@ void testMetaWithXML() throws Exception {
java.lang.System.out.println("Total time = " + (java.lang.System.currentTimeMillis() - time));
}

private Node buildPayload() throws JAXBException, ParserConfigurationException {

final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
final DocumentBuilder db = dbf.newDocumentBuilder();
final org.w3c.dom.Document doc = db.newDocument();

final JAXBContext jaxbContext = JAXBContext.newInstance(Payload.class);

final Payload payload = new Payload();
payload.setValue("thisIsMyValue");

jaxbContext.createMarshaller().marshal(payload, java.lang.System.out);

jaxbContext.createMarshaller().marshal(payload, doc);

final Element docElm = doc.getDocumentElement();

return docElm;
}
// private Node buildPayload() throws JAXBException, ParserConfigurationException {
//
// final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// dbf.setNamespaceAware(false);
// final DocumentBuilder db = dbf.newDocumentBuilder();
// final org.w3c.dom.Document doc = db.newDocument();
//
// final JAXBContext jaxbContext = JAXBContext.newInstance(Payload.class);
//
// final Payload payload = new Payload();
// payload.setValue("thisIsMyValue");
//
// jaxbContext.createMarshaller().marshal(payload, java.lang.System.out);
//
// jaxbContext.createMarshaller().marshal(payload, doc);
//
// final Element docElm = doc.getDocumentElement();
//
// return docElm;
// }
}
2 changes: 1 addition & 1 deletion event-logging-generator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies {
implementation libs.jaxb_xjc
implementation libs.slf4j_api

runtimeOnly libs.jaxb_runtime
runtimeOnly libs.jaxb_runtime_2
runtimeOnly libs.jaxb_rich_contract_plugin
runtimeOnly libs.jaxb_basics
runtimeOnly libs.saxon_he
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Class for generating the JAXB classes from a source schema. The schema is first
Expand All @@ -39,11 +40,6 @@
public class GenClasses {
private static final Charset UTF8 = StandardCharsets.UTF_8;

//private static final String PUBLIC_ABSTRACT_CLASS = "public abstract class ";

//private static final String PUBLIC_CLASS = "public class ";


private static final String SOURCE_SCHEMA_REGEX = "event-logging-v.*\\.xsd";
private static final Pattern SOURCE_SCHEMA_PATTERN = Pattern.compile(SOURCE_SCHEMA_REGEX);
private static final String GENERATOR_PROJECT_NAME = "event-logging-generator";
Expand All @@ -52,6 +48,7 @@ public class GenClasses {
private static final String SCHEMA_DIR_NAME = "schema";
private static final String PACKAGE_NAME = "event.logging";
private static final Pattern EVENT_LOGGING_BASE_PATTERN = Pattern.compile("event\\.logging\\.base");
private static final Pattern JAVAX_XML_PATTERN = Pattern.compile("javax\\.xml\\.");
private static final Pattern COMPLEX_TYPE_PATTERN = Pattern.compile("ComplexType");
private static final Pattern SIMPLE_TYPE_PATTERN = Pattern.compile("SimpleType");
private static final Pattern COMMENT_PATTERN = Pattern.compile("/\\*\\*");
Expand Down Expand Up @@ -85,26 +82,29 @@ private void processXSDFile(final Path rootDir) throws Exception {
final Path modXsd = schemaDir.resolve("schema.mod.xsd");
final Path bindingFile = generatorProjectDir.resolve("simple-binding.xjb");

List<Path> sourceSchemas = Files.find(schemaDir, 1, (path, atts) ->
List<Path> sourceSchemas;
try (final Stream<Path> pathStream = Files.find(schemaDir, 1, (path, atts) ->
SOURCE_SCHEMA_PATTERN.matcher(path.getFileName().toString()).matches()
).collect(Collectors.toList());
)) {
sourceSchemas = pathStream.collect(Collectors.toList());
}

Path xsdFile = null;
if (sourceSchemas.size() == 0) {
if (sourceSchemas.isEmpty()) {
System.out.printf("ERROR - No source schema found in %s matching '%s'%n",
schemaDir.toAbsolutePath().toString(),
schemaDir.toAbsolutePath(),
SOURCE_SCHEMA_REGEX);
System.exit(1);
} else if (sourceSchemas.size() > 1) {
System.out.printf("ERROR - Too many source schemas found in %s matching '%s'%n",
schemaDir.toAbsolutePath().toString(),
schemaDir.toAbsolutePath(),
SOURCE_SCHEMA_REGEX);
System.exit(1);
} else {
xsdFile = schemaDir.resolve(sourceSchemas.get(0).getFileName().toString());
}

String xsd = new String(Files.readAllBytes(xsdFile), UTF8);
String xsd = Files.readString(xsdFile, UTF8);
//Remove 'ComplexType' from the type names to make the class names cleaner
xsd = COMPLEX_TYPE_PATTERN.matcher(xsd).replaceAll("");
//Remove 'SimpleType' from the type names to make the class names cleaner
Expand All @@ -123,6 +123,10 @@ private void processXSDFile(final Path rootDir) throws Exception {
clean(srcDir.resolve("main/resources/event/logging"));
clean(srcDir.resolve("test/java/event/logging"));

// -Xfluent-builder comes from the jaxb-rich-contract-plugin lib and give us the nice
// fluent builder API.
// -Xinheritance comes from the jaxb-plugins lib and lets us make jaxb classes
// implement an interface.
final String[] xjcOptions = new String[]{
"-xmlschema",
"-extension",
Expand Down Expand Up @@ -316,7 +320,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO

private void modifyFile(final Path javaFile) {
try {
String java = new String(Files.readAllBytes(javaFile), UTF8);
String java = Files.readString(javaFile, UTF8);

// Remove top JAXB comments.
java = removeComments(java);
Expand All @@ -331,6 +335,11 @@ private void modifyFile(final Path javaFile) {
if (javaFile.getFileName().toString().contains("ObjectFactory")) {
java = fixObjectFactory(java);
}
// TODO remove this replaceAll once we uplift jaxb-rich-contract-plugin to
// >= 4.2.0.0 which is using jakarta.xml
// Replace javax.xml. => jakarta.xml. so we can ship our lib with jaxb4 deps
java = JAVAX_XML_PATTERN.matcher(java)
.replaceAll("jakarta.xml.");

Files.write(javaFile, java.getBytes(UTF8));
} catch (final IOException e) {
Expand Down Expand Up @@ -363,7 +372,7 @@ private String sortLines(final String java, final String pattern, final boolean
}
}

if (commentLines.size() > 0) {
if (!commentLines.isEmpty()) {
Collections.sort(commentLines);
for (int j = 0; j < commentLines.size(); j++) {
String line = commentLines.get(j);
Expand Down
6 changes: 3 additions & 3 deletions example-logged-application/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ ext.getPropertyOrDefault = { propName, defaultValue ->
return val;
}

def DEFAULT_EVENT_LOGGING_VERSION = '5.0-beta.29_schema-v4.0-beta.3'
//def DEFAULT_EVENT_LOGGING_VERSION = 'schema-v4.0-beta.3-SNAPSHOT'
//def DEFAULT_EVENT_LOGGING_VERSION = '5.0-beta.29_schema-v4.0-beta.3'
def DEFAULT_EVENT_LOGGING_VERSION = 'schema-v4.0.0-SNAPSHOT'

ext.versions = [
eventLogging: getPropertyOrDefault('mavenVersion', DEFAULT_EVENT_LOGGING_VERSION),
Expand All @@ -42,7 +42,7 @@ ext.versions = [

java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
languageVersion = JavaLanguageVersion.of(11)
vendor = JvmVendorSpec.ADOPTIUM
}
}
Expand Down

0 comments on commit 2b88c4f

Please sign in to comment.