diff --git a/src/main/java/com/avaje/ebean/config/ClassLoadConfig.java b/src/main/java/com/avaje/ebean/config/ClassLoadConfig.java index 6519d1b05..1adf6d32c 100644 --- a/src/main/java/com/avaje/ebean/config/ClassLoadConfig.java +++ b/src/main/java/com/avaje/ebean/config/ClassLoadConfig.java @@ -91,7 +91,7 @@ public class ClassLoadConfig { /** * Return true if the given class is present. */ - protected boolean isPresent(String className) { + public boolean isPresent(String className) { try { forName(className); return true; diff --git a/src/main/java/com/avaje/ebean/config/ServerConfig.java b/src/main/java/com/avaje/ebean/config/ServerConfig.java index f66c56111..44e5dab02 100644 --- a/src/main/java/com/avaje/ebean/config/ServerConfig.java +++ b/src/main/java/com/avaje/ebean/config/ServerConfig.java @@ -112,6 +112,11 @@ public class ServerConfig { */ private boolean disableClasspathSearch; + /** + * The Geometry SRID value (default 4326). + */ + private int geometrySRID = 4326; + /** * List of interesting classes such as entities, embedded, ScalarTypes, * Listeners, Finders, Controllers etc. @@ -876,6 +881,20 @@ public class ServerConfig { this.migrationConfig = migrationConfig; } + /** + * Return the Geometry SRID. + */ + public int getGeometrySRID() { + return geometrySRID; + } + + /** + * Set the Geometry SRID. + */ + public void setGeometrySRID(int geometrySRID) { + this.geometrySRID = geometrySRID; + } + /** * Return the time zone to use when reading/writing Timestamps via JDBC. *

@@ -2363,6 +2382,7 @@ public class ServerConfig { } loadDocStoreSettings(p); + geometrySRID = p.getInt("geometrySRID", geometrySRID); disableL2Cache = p.getBoolean("disableL2Cache", disableL2Cache); explicitTransactionBeginMode = p.getBoolean("explicitTransactionBeginMode", explicitTransactionBeginMode); autoCommitMode = p.getBoolean("autoCommitMode", autoCommitMode); diff --git a/src/main/java/com/avaje/ebean/config/dbplatform/DbPlatformTypeMapping.java b/src/main/java/com/avaje/ebean/config/dbplatform/DbPlatformTypeMapping.java index 6a7e77683..57e7847f0 100644 --- a/src/main/java/com/avaje/ebean/config/dbplatform/DbPlatformTypeMapping.java +++ b/src/main/java/com/avaje/ebean/config/dbplatform/DbPlatformTypeMapping.java @@ -19,6 +19,10 @@ public class DbPlatformTypeMapping { private static final DbPlatformType JSON_BLOB_PLACEHOLDER = new DbPlatformType("jsonBlobPlaceholder"); private static final DbPlatformType JSON_VARCHAR_PLACEHOLDER = new DbPlatformType("jsonVarcharPlaceholder"); + private static final DbPlatformType POINT = new DbPlatformType("point"); + private static final DbPlatformType POLYGON = new DbPlatformType("polygon"); + private static final DbPlatformType MULTIPOLYGON = new DbPlatformType("multipolygon"); + private final Map typeMap = new HashMap(); /** @@ -62,6 +66,10 @@ public class DbPlatformTypeMapping { // most commonly real maps to db float put(DbType.REAL, new DbPlatformType("float")); + put(DbType.POINT, POINT); + put(DbType.POLYGON, POLYGON); + put(DbType.MULTIPOLYGON, MULTIPOLYGON); + if (logicalTypes) { // keep it logical for 2 layer DDL generation put(DbType.VARCHAR, new DbPlatformType("varchar")); diff --git a/src/main/java/com/avaje/ebean/config/dbplatform/DbType.java b/src/main/java/com/avaje/ebean/config/dbplatform/DbType.java index 616f7f0bf..95c7d931e 100644 --- a/src/main/java/com/avaje/ebean/config/dbplatform/DbType.java +++ b/src/main/java/com/avaje/ebean/config/dbplatform/DbType.java @@ -36,6 +36,10 @@ public enum DbType { UUID(ExtraDbTypes.UUID), + POINT(ExtraDbTypes.POINT), + POLYGON(ExtraDbTypes.POLYGON), + MULTIPOLYGON(ExtraDbTypes.MULTIPOLYGON), + HSTORE(ExtraDbTypes.HSTORE), JSON(ExtraDbTypes.JSON), JSONB(ExtraDbTypes.JSONB), diff --git a/src/main/java/com/avaje/ebean/config/dbplatform/ExtraDbTypes.java b/src/main/java/com/avaje/ebean/config/dbplatform/ExtraDbTypes.java index f8eac425a..6f51a86b5 100644 --- a/src/main/java/com/avaje/ebean/config/dbplatform/ExtraDbTypes.java +++ b/src/main/java/com/avaje/ebean/config/dbplatform/ExtraDbTypes.java @@ -40,4 +40,19 @@ public interface ExtraDbTypes { */ int JSONBlob = 5005; + /** + * Geo Point + */ + int POINT = 6000; + + /** + * Geo Polygon + */ + int POLYGON = 6001; + + /** + * Geo MultiPolygon + */ + int MULTIPOLYGON = 6002; + } diff --git a/src/main/java/com/avaje/ebean/config/dbplatform/PostgresPlatform.java b/src/main/java/com/avaje/ebean/config/dbplatform/PostgresPlatform.java index 700b0499e..78d16f328 100644 --- a/src/main/java/com/avaje/ebean/config/dbplatform/PostgresPlatform.java +++ b/src/main/java/com/avaje/ebean/config/dbplatform/PostgresPlatform.java @@ -74,6 +74,17 @@ public class PostgresPlatform extends DatabasePlatform { dbTypeMap.put(DbType.TIMESTAMP, new DbPlatformType(tsType)); } } + addGeoTypes(serverConfig.getGeometrySRID()); + } + + private void addGeoTypes(int srid) { + dbTypeMap.put(DbType.POINT, geoType("point",srid)); + dbTypeMap.put(DbType.POLYGON, geoType("polygon",srid)); + dbTypeMap.put(DbType.MULTIPOLYGON, geoType("multipolygon",srid)); + } + + private DbPlatformType geoType(String type, int srid) { + return new DbPlatformType("geometry("+type+","+srid+")"); } /** diff --git a/src/main/java/com/avaje/ebean/plugin/ExtraTypeFactory.java b/src/main/java/com/avaje/ebean/plugin/ExtraTypeFactory.java index ff501fb1c..be43b26a9 100644 --- a/src/main/java/com/avaje/ebean/plugin/ExtraTypeFactory.java +++ b/src/main/java/com/avaje/ebean/plugin/ExtraTypeFactory.java @@ -1,4 +1,17 @@ package com.avaje.ebean.plugin; -public class ExtraTypeFactory { +import com.avaje.ebean.config.ServerConfig; +import com.avaje.ebeaninternal.server.type.ScalarType; + +import java.util.List; + +/** + * A factory that provides extra types to Ebean. + */ +public interface ExtraTypeFactory { + + /** + * Provide extra types to Ebean. + */ + List createTypes(ServerConfig config, Object objectMapper); } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeManager.java b/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeManager.java index b260d9cb7..53e754ad1 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeManager.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeManager.java @@ -12,6 +12,7 @@ import com.avaje.ebean.config.ServerConfig; import com.avaje.ebean.config.dbplatform.DatabasePlatform; import com.avaje.ebean.config.dbplatform.DbPlatformType; import com.avaje.ebean.dbmigration.DbOffline; +import com.avaje.ebean.plugin.ExtraTypeFactory; import com.avaje.ebeaninternal.server.core.bootup.BootupClasses; import com.avaje.ebeaninternal.server.type.reflect.CheckImmutable; import com.avaje.ebeaninternal.server.type.reflect.CheckImmutableResponse; @@ -65,9 +66,11 @@ import java.util.Arrays; import java.util.Calendar; import java.util.Currency; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.ServiceLoader; import java.util.Set; import java.util.TimeZone; import java.util.UUID; @@ -208,6 +211,8 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable { initialiseJodaTypes(jsonDateTime, config); initialiseJacksonTypes(config); + loadTypesFromProviders(config, objectMapper); + if (bootupClasses != null) { initialiseCustomScalarTypes(jsonDateTime, bootupClasses); initialiseScalarConverters(bootupClasses); @@ -215,6 +220,24 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable { } } + /** + * Load custom scalar types registered via ExtraTypeFactory and ServiceLoader. + */ + private void loadTypesFromProviders(ServerConfig config, Object objectMapper) { + + ServiceLoader factories = ServiceLoader.load(ExtraTypeFactory.class); + Iterator iterator = factories.iterator(); + if (iterator.hasNext()) { + // use the cacheFactory (via classpath service loader) + ExtraTypeFactory plugin = iterator.next(); + List types = plugin.createTypes(config, objectMapper); + for (ScalarType type : types) { + logger.debug("adding ScalarType {}", type.getClass()); + addCustomType(type); + } + } + } + private boolean isPostgres(DatabasePlatform databasePlatform) { return databasePlatform.getName().toLowerCase().startsWith("postgre"); } @@ -707,8 +730,7 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable { } } - add(scalarType); - customScalarTypes.add(scalarType); + addCustomType(scalarType); } catch (Exception e) { String msg = "Error loading ScalarType [" + cls.getName() + "]"; @@ -717,6 +739,11 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable { } } + private void addCustomType(ScalarType scalarType) { + add(scalarType); + customScalarTypes.add(scalarType); + } + private Object initObjectMapper(ServerConfig serverConfig) { Object objectMapper = serverConfig.getObjectMapper();