Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
*/
package org.hibernate.type.format.jackson;

import java.util.List;

import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.checkerframework.checker.nullness.qual.Nullable;

import org.hibernate.type.format.FormatMapper;
import org.hibernate.type.format.FormatMapperCreationContext;

Expand Down Expand Up @@ -36,72 +41,37 @@ private static boolean ableToLoadJacksonXMLMapper() {
*/
private static boolean ableToLoadJacksonOSONFactory() {
return ableToLoadJacksonJSONMapper() &&
canLoad( "oracle.jdbc.provider.oson.OsonFactory" );
canLoad( "oracle.jdbc.provider.oson.OsonFactory" );
}

public static @Nullable FormatMapper getXMLJacksonFormatMapperOrNull(FormatMapperCreationContext creationContext) {
return JACKSON_XML_AVAILABLE
? createFormatMapper( "org.hibernate.type.format.jackson.JacksonXmlFormatMapper", creationContext )
? new JacksonXmlFormatMapper( creationContext )
: null;
}

public static @Nullable FormatMapper getJsonJacksonFormatMapperOrNull(FormatMapperCreationContext creationContext) {
return JACKSON_JSON_AVAILABLE
? createFormatMapper( "org.hibernate.type.format.jackson.JacksonJsonFormatMapper", creationContext )
? new JacksonJsonFormatMapper( creationContext )
: null;
}
public static @Nullable FormatMapper getOsonJacksonFormatMapperOrNull(FormatMapperCreationContext creationContext) {
return JACKSON_OSON_AVAILABLE
? createFormatMapper( "org.hibernate.type.format.jackson.JacksonOsonFormatMapper", creationContext )
? new JacksonOsonFormatMapper( creationContext )
: null;
}

public static @Nullable FormatMapper getXMLJacksonFormatMapperOrNull(boolean legacyFormat) {
if ( JACKSON_XML_AVAILABLE ) {
try {
final Class<?> formatMapperClass = JacksonIntegration.class.getClassLoader()
.loadClass( "org.hibernate.type.format.jackson.JacksonXmlFormatMapper" );
return (FormatMapper) formatMapperClass.getDeclaredConstructor( boolean.class )
.newInstance( legacyFormat );
}
catch (Exception e) {
throw new RuntimeException( "Couldn't instantiate Jackson XML FormatMapper", e );
}
}
return null;
}

public static @Nullable FormatMapper getJsonJacksonFormatMapperOrNull() {
return JACKSON_JSON_AVAILABLE
? createFormatMapper( "org.hibernate.type.format.jackson.JacksonJsonFormatMapper", null )
? new JacksonJsonFormatMapper()
: null;
}
public static @Nullable FormatMapper getOsonJacksonFormatMapperOrNull() {
return JACKSON_OSON_AVAILABLE
? createFormatMapper( "org.hibernate.type.format.jackson.JacksonOsonFormatMapper", null )
? new JacksonOsonFormatMapper()
: null;
}

private static FormatMapper createFormatMapper(String className, @Nullable FormatMapperCreationContext creationContext) {
try {
if ( creationContext == null ) {
final Class<?> formatMapperClass = JacksonIntegration.class.getClassLoader()
.loadClass( className );
return (FormatMapper) formatMapperClass.getDeclaredConstructor().newInstance();
}
else {
return (FormatMapper) creationContext.getBootstrapContext()
.getClassLoaderAccess()
.classForName( className )
.getDeclaredConstructor( FormatMapperCreationContext.class )
.newInstance( creationContext );
}
}
catch (Exception e) {
throw new RuntimeException( "Couldn't instantiate Jackson FormatMapper", e );
}
}

/**
* Checks that Oracle OSON extension available
*
Expand All @@ -124,4 +94,27 @@ private static boolean canLoad(String name) {
return false;
}
}

static List<Module> loadModules(FormatMapperCreationContext creationContext) {
final ClassLoader classLoader = JacksonIntegration.class.getClassLoader();
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if ( contextClassLoader != null && classLoader != contextClassLoader ) {
try {
// The context class loader represents the application class loader in a Jakarta EE deployment.
// We have to check if the ObjectMapper that is visible to Hibernate ORM is the same that is visible
// to the application class loader. Only if it is, we can use the application class loader or rather
// our AggregatedClassLoader for loading Jackson Module via ServiceLoader, as otherwise the loaded
// Jackson Module instances would have a different class loader, leading to a ClassCastException.
if ( ObjectMapper.class == contextClassLoader.loadClass( "com.fasterxml.jackson.databind.ObjectMapper" ) ) {
return creationContext.getBootstrapContext()
.getClassLoaderService()
.<List<Module>>workWithClassLoader( ObjectMapper::findModules );
}
}
catch (ClassNotFoundException | LinkageError e) {
// Ignore if the context/application class loader doesn't know Jackson classes
}
}
return ObjectMapper.findModules( classLoader );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ public JacksonJsonFormatMapper() {
}

public JacksonJsonFormatMapper(FormatMapperCreationContext creationContext) {
this(
creationContext.getBootstrapContext()
.getClassLoaderService()
.<List<Module>>workWithClassLoader( ObjectMapper::findModules )
);
this( JacksonIntegration.loadModules( creationContext ) );
}

private JacksonJsonFormatMapper(List<Module> modules) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,15 @@ public JacksonXmlFormatMapper() {

public JacksonXmlFormatMapper(boolean legacyFormat) {
this(
createXmlMapper( XmlMapper.findModules( JacksonXmlFormatMapper.class.getClassLoader() ), legacyFormat ),
createXmlMapper( ObjectMapper.findModules( JacksonXmlFormatMapper.class.getClassLoader() ), legacyFormat ),
legacyFormat
);
}

public JacksonXmlFormatMapper(FormatMapperCreationContext creationContext) {
this(
createXmlMapper(
creationContext.getBootstrapContext()
.getClassLoaderService()
.<List<Module>>workWithClassLoader( XmlMapper::findModules ),
JacksonIntegration.loadModules( creationContext ),
creationContext.getBootstrapContext()
.getMetadataBuildingOptions()
.isXmlFormatMapperLegacyFormatEnabled()
Expand Down