HHH-15415 Separate detection for Jackson Databind and Jackson XML, improve comments

This commit is contained in:
Sanne Grinovero 2022-07-22 09:41:34 +01:00 committed by Sanne Grinovero
parent 492b4aa540
commit 2f313edfab
2 changed files with 30 additions and 13 deletions

View File

@ -10,24 +10,23 @@ import org.hibernate.type.FormatMapper;
public final class JacksonIntegration { public final class JacksonIntegration {
// Implementation note: we rely on the following two fields to be folded as constants // Implementation note: we rely on the following fields to be folded as constants
// when GraalVM native image is initializing them. // when GraalVM native image is initializing them.
private static final boolean JACKSON_AVAILABLE = ableToLoadJackson(); private static final boolean JACKSON_XML_AVAILABLE = ableToLoadJacksonXMLMapper();
private static final JacksonXmlFormatMapper XML_FORMAT_MAPPER = JACKSON_AVAILABLE ? new JacksonXmlFormatMapper() : null; private static final boolean JACKSON_JSON_AVAILABLE = ableToLoadJacksonJSONMapper();
private static final JacksonJsonFormatMapper JSON_FORMAT_MAPPER = JACKSON_AVAILABLE ? new JacksonJsonFormatMapper() : null; private static final JacksonXmlFormatMapper XML_FORMAT_MAPPER = JACKSON_XML_AVAILABLE ? new JacksonXmlFormatMapper() : null;
private static final JacksonJsonFormatMapper JSON_FORMAT_MAPPER = JACKSON_JSON_AVAILABLE ? new JacksonJsonFormatMapper() : null;
private JacksonIntegration() { private JacksonIntegration() {
//To not be instantiated: static helpers only //To not be instantiated: static helpers only
} }
private static boolean ableToLoadJackson() { private static boolean ableToLoadJacksonJSONMapper() {
try { return canLoad( "com.fasterxml.jackson.databind.ObjectMapper" );
JacksonIntegration.class.getClassLoader().loadClass( "com.fasterxml.jackson.dataformat.xml.XmlMapper" ); }
return true;
} private static boolean ableToLoadJacksonXMLMapper() {
catch (ClassNotFoundException | LinkageError e) { return canLoad( "com.fasterxml.jackson.dataformat.xml.XmlMapper" );
return false;
}
} }
public static FormatMapper getXMLJacksonFormatMapperOrNull() { public static FormatMapper getXMLJacksonFormatMapperOrNull() {
@ -37,4 +36,18 @@ public final class JacksonIntegration {
public static FormatMapper getJsonJacksonFormatMapperOrNull() { public static FormatMapper getJsonJacksonFormatMapperOrNull() {
return JSON_FORMAT_MAPPER; return JSON_FORMAT_MAPPER;
} }
private static boolean canLoad(String name) {
try {
//N.B. intentionally not using the context classloader
// as we're storing these in static references;
// IMO it's reasonable to expect that such dependencies are made reachable from the ORM classloader.
// (we can change this if it's more problematic than expected).
JacksonIntegration.class.getClassLoader().loadClass( name );
return true;
}
catch (ClassNotFoundException | LinkageError e) {
return false;
}
}
} }

View File

@ -21,7 +21,11 @@ public final class JakartaJsonIntegration {
private static boolean ableToLoadJakartaJsonB() { private static boolean ableToLoadJakartaJsonB() {
try { try {
JakartaJsonIntegration.class.getClassLoader().loadClass( "jakarta.json.bind.Jsonb" ); //N.B. intentionally not using the context classloader
// as we're storing these in static references;
// IMO it's reasonable to expect that such dependencies are made reachable from the ORM classloader.
// (we can change this if it's more problematic than expected).
JakartaJsonIntegration.class.getClassLoader().loadClass( "jakarta.json.bind.JsonbBuilder" );
return true; return true;
} }
catch (ClassNotFoundException | LinkageError e) { catch (ClassNotFoundException | LinkageError e) {