Refactor extract ebean-json-node module - support for Jackson JsonNode (into separate module)

This commit is contained in:
Rob Bygrave committed 2022-09-07 21:39:36 +12:00
1 parent 39de9d56db
commit 5c1b478085
27 files changed
+266 -84

No files matched your search

@@ -2,6 +2,7 @@ package io.ebeaninternal.server.persist;
import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.core.type.DataReader;
import io.ebean.core.type.PostgresHelper;
import io.ebean.core.type.ScalarType;
import io.ebeaninternal.api.BindParams;
import io.ebeaninternal.api.CoreLog;
@@ -1,6 +1,5 @@
package io.ebeaninternal.server.type;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.ebean.annotation.*;
import io.ebean.config.DatabaseConfig;
@@ -9,9 +8,7 @@ import io.ebean.config.PlatformConfig;
import io.ebean.config.ScalarTypeConverter;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.core.type.DocPropertyType;
import io.ebean.core.type.ExtraTypeFactory;
import io.ebean.core.type.ScalarType;
import io.ebean.core.type.*;
import io.ebean.types.Cidr;
import io.ebean.types.Inet;
import io.ebean.util.AnnotationUtil;
@@ -51,6 +48,7 @@ public final class DefaultTypeManager implements TypeManager {
private static final System.Logger log = CoreLog.internal;
private final Map<Class<?>, ScalarTypeSet<?>> typeSets = new HashMap<>();
private final ConcurrentHashMap<Class<?>, ScalarType<?>> typeMap;
private final ConcurrentHashMap<Integer, ScalarType<?>> nativeMap;
private final ConcurrentHashMap<String, ScalarType<?>> logicalMap;
@@ -71,28 +69,7 @@ public final class DefaultTypeManager implements TypeManager {
private final EnumType defaultEnumType;
private final DatabasePlatform databasePlatform;
// OPTIONAL ScalarTypes registered if Jackson/JsonNode is in the classpath
/**
* Jackson's JsonNode storage to Clob.
*/
private ScalarType<?> jsonNodeClob;
/**
* Jackson's JsonNode storage to Blob.
*/
private ScalarType<?> jsonNodeBlob;
/**
* Jackson's JsonNode storage to Varchar.
*/
private ScalarType<?> jsonNodeVarchar;
/**
* Jackson's JsonNode storage to Postgres JSON or Clob.
*/
private ScalarType<?> jsonNodeJson;
/**
* Jackson's JsonNode storage to Postgres JSONB or Clob.
*/
private ScalarType<?> jsonNodeJsonb;
private final PlatformArrayTypeFactory arrayTypeListFactory;
private final PlatformArrayTypeFactory arrayTypeSetFactory;
@@ -120,7 +97,6 @@ public final class DefaultTypeManager implements TypeManager {
initialiseStandard(config);
initialiseJavaTimeTypes(config);
initialiseJacksonTypes();
loadTypesFromProviders(config, objectMapper);
loadGeoTypeBinder(config);
@@ -160,16 +136,21 @@ public final class DefaultTypeManager implements TypeManager {
* Load custom scalar types registered via ExtraTypeFactory and ServiceLoader.
*/
private void loadTypesFromProviders(DatabaseConfig config, Object objectMapper) {
ServiceLoader<ExtraTypeFactory> factories = ServiceLoader.load(ExtraTypeFactory.class);
Iterator<ExtraTypeFactory> iterator = factories.iterator();
if (iterator.hasNext()) {
// use the cacheFactory (via classpath service loader)
ExtraTypeFactory plugin = iterator.next();
List<? extends ScalarType<?>> types = plugin.createTypes(config, objectMapper);
for (ScalarType<?> type : types) {
for (ExtraTypeFactory plugin : ServiceLoader.load(ExtraTypeFactory.class)) {
for (ScalarType<?> type : plugin.createTypes(config, objectMapper)) {
add(type);
}
}
for (ScalarTypeSetFactory factory : ServiceLoader.load(ScalarTypeSetFactory.class)) {
ScalarTypeSet<?> typeSet = factory.createTypeSet(config, objectMapper);
if (typeSet != null) {
typeSets.put(typeSet.type(), typeSet);
ScalarType<?> defaultType = typeSet.defaultType();
if (defaultType != null) {
typeMap.put(typeSet.type(), defaultType);
}
}
}
}
private boolean isPostgresCompatible(DatabasePlatform databasePlatform) {
@@ -350,19 +331,9 @@ public final class DefaultTypeManager implements TypeManager {
}
}
if (objectMapperPresent && prop.getMutationDetection() == MutationDetection.DEFAULT) {
if (type.equals(JsonNode.class)) {
switch (dbType) {
case Types.VARCHAR:
return jsonNodeVarchar;
case Types.BLOB:
return jsonNodeBlob;
case Types.CLOB:
return jsonNodeClob;
case DbPlatformType.JSONB:
return jsonNodeJsonb;
default:
return jsonNodeJson;
}
ScalarTypeSet<?> typeSet = typeSets.get(type);
if (typeSet != null) {
return typeSet.forType(dbType);
}
}
return createJsonObjectMapperType(prop, dbType, DocPropertyType.OBJECT);
@@ -709,25 +680,6 @@ public final class DefaultTypeManager implements TypeManager {
}
}
/**
* Add support for Jackson's JsonNode mapping to Clob, Blob, Varchar, JSON and JSONB.
*/
private void initialiseJacksonTypes() {
if (objectMapper != null) {
ObjectMapper mapper = (ObjectMapper) objectMapper;
jsonNodeClob = new ScalarTypeJsonNode.Clob(mapper);
jsonNodeBlob = new ScalarTypeJsonNode.Blob(mapper);
jsonNodeVarchar = new ScalarTypeJsonNode.Varchar(mapper);
jsonNodeJson = jsonNodeClob; // Default for non-Postgres databases
jsonNodeJsonb = jsonNodeClob; // Default for non-Postgres databases
if (postgres) {
jsonNodeJson = new ScalarTypeJsonNodePostgres.JSON(mapper);
jsonNodeJsonb = new ScalarTypeJsonNodePostgres.JSONB(mapper);
}
// add as default mapping for JsonNode (when not annotated with @DbJson etc)
typeMap.put(JsonNode.class, jsonNodeJson);
}
}
private void initialiseJavaTimeTypes(DatabaseConfig config) {
@@ -1,34 +0,0 @@
package io.ebeaninternal.server.type;
import org.postgresql.util.PGobject;
import java.sql.SQLException;
public final class PostgresHelper {
/**
* The Postgres JSON DB type.
*/
static final String JSON_TYPE = "json";
/**
* The Postgres JSONB DB type.
*/
static final String JSONB_TYPE = "jsonb";
static final String INET_TYPE = "inet";
public static Object asInet(String value) throws SQLException {
return asObject(INET_TYPE, value);
}
/**
* Construct and return Postgres specific PG object.
*/
public static Object asObject(String pgType, String rawJson) throws SQLException {
PGobject pgo = new PGobject();
pgo.setType(pgType);
pgo.setValue(rawJson);
return pgo;
}
}
@@ -2,6 +2,7 @@ package io.ebeaninternal.server.type;
import io.ebean.config.dbplatform.ExtraDbTypes;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.PostgresHelper;
import io.ebean.core.type.ScalarTypeBaseVarchar;
import io.ebean.types.Cidr;
@@ -2,6 +2,7 @@ package io.ebeaninternal.server.type;
import io.ebean.config.dbplatform.ExtraDbTypes;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.PostgresHelper;
import io.ebean.core.type.ScalarTypeBaseVarchar;
import io.ebean.types.Inet;
@@ -2,6 +2,7 @@ package io.ebeaninternal.server.type;
import io.ebean.config.dbplatform.ExtraDbTypes;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.PostgresHelper;
import io.ebean.core.type.ScalarTypeBaseVarchar;
import io.ebean.text.TextException;
@@ -3,10 +3,7 @@ package io.ebeaninternal.server.type;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader;
import io.ebean.core.type.DocPropertyType;
import io.ebean.core.type.ScalarType;
import io.ebean.core.type.*;
import io.ebean.text.TextException;
import io.ebean.text.json.EJson;
import io.ebeaninternal.json.ModifyAwareList;
@@ -2,6 +2,7 @@ package io.ebeaninternal.server.type;
import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.PostgresHelper;
import java.sql.SQLException;
@@ -1,204 +0,0 @@
package io.ebeaninternal.server.type;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader;
import io.ebean.core.type.DocPropertyType;
import io.ebean.core.type.ScalarTypeBase;
import io.ebean.text.TextException;
import io.ebean.util.IOUtils;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.sql.Types;
/**
* Type which maps Jackson's JsonNode to various DB types (Clob, Varchar, Blob) in JSON format.
*/
abstract class ScalarTypeJsonNode extends ScalarTypeBase<JsonNode> {
/**
* Clob storage based implementation.
*/
static final class Clob extends ScalarTypeJsonNode {
Clob(ObjectMapper objectMapper) {
super(objectMapper, Types.CLOB);
}
@Override
public JsonNode read(DataReader reader) throws SQLException {
String content = reader.getStringFromStream();
if (content == null) {
return null;
}
return parse(content);
}
}
/**
* Varchar storage based implementation.
*/
static final class Varchar extends ScalarTypeJsonNode {
public Varchar(ObjectMapper objectMapper) {
super(objectMapper, Types.VARCHAR);
}
}
/**
* Blob storage based implementation.
*/
static class Blob extends ScalarTypeJsonNode {
Blob(ObjectMapper objectMapper) {
super(objectMapper, Types.BLOB);
}
@Override
public JsonNode read(DataReader dataReader) throws SQLException {
InputStream is = dataReader.getBinaryStream();
if (is == null) {
return null;
}
try (Reader reader = IOUtils.newReader(is)) {
return parse(reader);
} catch (IOException e) {
throw new SQLException("Error reading Blob stream from DB", e);
}
}
@Override
public void bind(DataBinder binder, JsonNode value) throws SQLException {
if (value == null) {
binder.setNull(Types.BLOB);
} else {
String rawJson = formatValue(value);
binder.setBlob(rawJson.getBytes(StandardCharsets.UTF_8));
}
}
}
/**
* Jackson's ObjectMapper used to read / write JsonNode
*/
final ObjectMapper objectMapper;
ScalarTypeJsonNode(ObjectMapper objectMapper, int jdbcType) {
super(JsonNode.class, false, jdbcType);
this.objectMapper = objectMapper;
}
/**
* Map is a mutable type. Use the isDirty() method to check for dirty state.
*/
@Override
public boolean mutable() {
return true;
}
/**
* Return true if the value should be considered dirty (and included in an update).
*/
@Override
public boolean isDirty(Object value) {
return true;
}
@Override
public JsonNode read(DataReader reader) throws SQLException {
String rawJson = reader.getString();
if (rawJson == null) {
return null;
}
return parse(rawJson);
}
@Override
public void bind(DataBinder binder, JsonNode value) throws SQLException {
if (value == null) {
binder.setNull(Types.VARCHAR);
} else {
String rawJson = formatValue(value);
binder.setString(rawJson);
}
}
@Override
public Object toJdbcType(Object value) {
return value;
}
@Override
public JsonNode toBeanType(Object value) {
return (JsonNode) value;
}
@Override
public String formatValue(JsonNode jsonNode) {
try {
return objectMapper.writeValueAsString(jsonNode);
} catch (IOException e) {
throw new TextException(e);
}
}
@Override
public JsonNode parse(String value) {
try {
return objectMapper.readValue(value, JsonNode.class);
} catch (IOException e) {
throw new TextException("Failed to parse JSON [{}] as JsonNode", value, e);
}
}
public JsonNode parse(Reader reader) {
try {
return objectMapper.readValue(reader, JsonNode.class);
} catch (IOException e) {
throw new TextException(e);
}
}
@Override
public JsonNode readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
return null;
} else {
return parse(dataInput.readUTF());
}
}
@Override
public void writeData(DataOutput dataOutput, JsonNode value) throws IOException {
if (value == null) {
dataOutput.writeBoolean(false);
} else {
ScalarHelp.writeUTF(dataOutput, format(value));
}
}
@Override
public void jsonWrite(JsonGenerator writer, JsonNode value) throws IOException {
objectMapper.writeTree(writer, value);
}
@Override
public JsonNode jsonRead(JsonParser parser) throws IOException {
return objectMapper.readValue(parser, JsonNode.class);
}
@Override
public DocPropertyType docType() {
return DocPropertyType.OBJECT;
}
}
@@ -1,49 +0,0 @@
package io.ebeaninternal.server.type;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.core.type.DataBinder;
import java.sql.SQLException;
/**
* Support for mapping JsonNode to Postgres DB types JSON and JSONB.
*/
abstract class ScalarTypeJsonNodePostgres extends ScalarTypeJsonNode {
final ObjectMapper objectMapper;
final String postgresType;
ScalarTypeJsonNodePostgres(ObjectMapper objectMapper, int jdbcType, String postgresType) {
super(objectMapper, jdbcType);
this.objectMapper = objectMapper;
this.postgresType = postgresType;
}
@Override
public void bind(DataBinder binder, JsonNode value) throws SQLException {
String rawJson = (value == null) ? null : formatValue(value);
binder.setObject(PostgresHelper.asObject(postgresType, rawJson));
}
/**
* ScalarType mapping JsonNode to Postgres JSON database type.
*/
static final class JSON extends ScalarTypeJsonNodePostgres {
public JSON(ObjectMapper objectMapper) {
super(objectMapper, DbPlatformType.JSON, PostgresHelper.JSON_TYPE);
}
}
/**
* ScalarType mapping JsonNode to Postgres JSONB database type.
*/
static final class JSONB extends ScalarTypeJsonNodePostgres {
public JSONB(ObjectMapper objectMapper) {
super(objectMapper, DbPlatformType.JSONB, PostgresHelper.JSONB_TYPE);
}
}
}
@@ -3,10 +3,7 @@ package io.ebeaninternal.server.type;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader;
import io.ebean.core.type.DocPropertyType;
import io.ebean.core.type.ScalarType;
import io.ebean.core.type.*;
import io.ebean.text.TextException;
import io.ebean.text.json.EJson;
import io.ebeaninternal.json.ModifyAwareSet;
@@ -3,6 +3,7 @@ package io.ebeaninternal.server.type;
import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.config.dbplatform.ExtraDbTypes;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.PostgresHelper;
import io.ebean.core.type.ScalarType;
import java.sql.SQLException;
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.ebean.ModifyAwareType;
import io.ebean.annotation.MutationDetection;
import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.core.type.PostgresHelper;
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
final class TypeJsonManager {
@@ -8,6 +8,7 @@ module io.ebean.core {
uses io.ebean.config.dbplatform.DatabasePlatformProvider;
uses io.ebean.datasource.DataSourceAlertFactory;
uses io.ebean.core.type.ExtraTypeFactory;
uses io.ebean.core.type.ScalarTypeSetFactory;
uses io.ebeanservice.docstore.api.DocStoreFactory;
uses io.ebean.migration.auto.AutoMigrationRunner;
uses io.avaje.classpath.scanner.ClassPathScannerFactory;