From 6cb3662b5c0f471807ff516bc90500bb5900fa7f Mon Sep 17 00:00:00 2001 From: inrgihc Date: Wed, 26 Jul 2023 21:53:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=80=BB=E8=BE=91=E6=A2=B3?= =?UTF-8?q?=E7=90=86=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dbswitch/common/type/ProductTypeEnum.java | 63 ++++++++ .../gitee/dbswitch/annotation/Product.java | 14 ++ .../provider/AbstractFactoryProvider.java | 12 ++ .../gitee/dbswitch/schema/ColumnMetaData.java | 15 +- dbswitch-dist/src/main/assembly/assembly.xml | 7 +- .../product/db2/DB2FactoryProvider.java | 7 +- .../META-INF/services/dbswitch.providers | 1 + .../product/dm/DmFactoryProvider.java | 7 +- .../META-INF/services/dbswitch.providers | 1 + .../product/gbase/GbaseFactoryProvider.java | 10 +- .../gbase/GbaseMetadataQueryProvider.java | 35 +++++ .../META-INF/services/dbswitch.providers | 1 + .../product/hive/HiveFactoryProvider.java | 7 +- .../META-INF/services/dbswitch.providers | 1 + .../kingbase/KingbaseFactoryProvider.java | 7 +- .../META-INF/services/dbswitch.providers | 1 + .../mariadb/MariadbFactoryProvider.java | 7 +- .../META-INF/services/dbswitch.providers | 1 + .../product/mysql/MysqlFactoryProvider.java | 7 +- .../META-INF/services/dbswitch.providers | 1 + .../product/oracle/OracleFactoryProvider.java | 7 +- .../META-INF/services/dbswitch.providers | 1 + .../product/oscar/OscarFactoryProvider.java | 7 +- .../META-INF/services/dbswitch.providers | 1 + .../postgresql/PostgresFactoryProvider.java | 7 +- .../META-INF/services/dbswitch.providers | 1 + .../ProductRegisterAutoConfiguration.java | 55 ------- .../product/sqlite/SqliteFactoryProvider.java | 7 +- .../META-INF/services/dbswitch.providers | 1 + .../sqlserver/SqlserverFactoryProvider.java | 7 +- .../META-INF/services/dbswitch.providers | 1 + .../product/sybase/SybaseFactoryProvider.java | 7 +- .../META-INF/services/dbswitch.providers | 1 + .../pom.xml | 1 + .../ProductRegisterAutoConfiguration.java | 135 ++++++++++++++++++ .../main/resources/META-INF/spring.factories | 0 dbswitch-product/pom.xml | 2 +- 37 files changed, 309 insertions(+), 137 deletions(-) create mode 100644 dbswitch-core/src/main/java/com/gitee/dbswitch/annotation/Product.java create mode 100644 dbswitch-product/dbswitch-product-db2/src/main/resources/META-INF/services/dbswitch.providers create mode 100644 dbswitch-product/dbswitch-product-dm/src/main/resources/META-INF/services/dbswitch.providers create mode 100644 dbswitch-product/dbswitch-product-gbase/src/main/java/com/gitee/dbswitch/product/gbase/GbaseMetadataQueryProvider.java create mode 100644 dbswitch-product/dbswitch-product-gbase/src/main/resources/META-INF/services/dbswitch.providers create mode 100644 dbswitch-product/dbswitch-product-hive/src/main/resources/META-INF/services/dbswitch.providers create mode 100644 dbswitch-product/dbswitch-product-kingbase/src/main/resources/META-INF/services/dbswitch.providers create mode 100644 dbswitch-product/dbswitch-product-mariadb/src/main/resources/META-INF/services/dbswitch.providers create mode 100644 dbswitch-product/dbswitch-product-mysql/src/main/resources/META-INF/services/dbswitch.providers create mode 100644 dbswitch-product/dbswitch-product-oracle/src/main/resources/META-INF/services/dbswitch.providers create mode 100644 dbswitch-product/dbswitch-product-oscar/src/main/resources/META-INF/services/dbswitch.providers create mode 100644 dbswitch-product/dbswitch-product-postgresql/src/main/resources/META-INF/services/dbswitch.providers delete mode 100644 dbswitch-product/dbswitch-product-repository/src/main/java/com/gitee/dbswitch/product/repository/ProductRegisterAutoConfiguration.java create mode 100644 dbswitch-product/dbswitch-product-sqlite/src/main/resources/META-INF/services/dbswitch.providers create mode 100644 dbswitch-product/dbswitch-product-sqlserver/src/main/resources/META-INF/services/dbswitch.providers create mode 100644 dbswitch-product/dbswitch-product-sybase/src/main/resources/META-INF/services/dbswitch.providers rename dbswitch-product/{dbswitch-product-repository => dbswitch-register-product}/pom.xml (97%) create mode 100644 dbswitch-product/dbswitch-register-product/src/main/java/com/gitee/dbswitch/product/repository/ProductRegisterAutoConfiguration.java rename dbswitch-product/{dbswitch-product-repository => dbswitch-register-product}/src/main/resources/META-INF/spring.factories (100%) diff --git a/dbswitch-common/src/main/java/com/gitee/dbswitch/common/type/ProductTypeEnum.java b/dbswitch-common/src/main/java/com/gitee/dbswitch/common/type/ProductTypeEnum.java index c8564269..77da917e 100644 --- a/dbswitch-common/src/main/java/com/gitee/dbswitch/common/type/ProductTypeEnum.java +++ b/dbswitch-common/src/main/java/com/gitee/dbswitch/common/type/ProductTypeEnum.java @@ -170,10 +170,67 @@ public enum ProductTypeEnum { return String.format("%s%s%s.%s%s%s", quote, schema, quote, quote, table, quote); } + /** + * 类似于PostgreSQL系列的数据库类型 + * + * @return boolean + */ + public boolean isLikePostgres() { + return this == POSTGRESQL || this == KINGBASE; + } + + /** + * 类似于MySQL系列的数据库类型 + * + * @return boolean + */ + public boolean isLikeMysql() { + return this == MYSQL || this == MARIADB || this == GBASE8A; + } + + /** + * 类似于Oracle系列的数据库类型 + * + * @return boolean + */ + public boolean isLikeOracle() { + return this == ORACLE || this == DM; + } + + /** + * 类似于SQL Server系列的数据库类型 + * + * @return boolean + */ + public boolean isLikeSqlServer() { + return this == SQLSERVER || this == SYBASE; + } + + /** + * 类似于Hive系列的数据库类型 + * + * @return boolean + */ + public boolean isLikeHive() { + return this == HIVE; + } + + /** + * 是否存在指定字符串名称的数据库类型 + * + * @param name 字符串名称 + * @return boolean + */ public static boolean exists(String name) { return Arrays.stream(values()).anyMatch(item -> item.name().equalsIgnoreCase(name)); } + /** + * 将字符串名称转换为枚举值 + * + * @param name 字符串名称 + * @return ProductTypeEnum + */ public static ProductTypeEnum of(String name) { if (!StringUtils.isEmpty(name)) { for (ProductTypeEnum type : ProductTypeEnum.values()) { @@ -186,6 +243,12 @@ public enum ProductTypeEnum { throw new IllegalArgumentException("cannot find enum name: " + name); } + /** + * 针对SQLite数据库的URL连接串判断 + * + * @param url SQLite数据库的URL连接串 + * @return boolean + */ public static boolean isUnsupportedTargetSqlite(String url) { String prefix1 = "jdbc:sqlite::resource:"; //String prefix2 = "jdbc:sqlite::memory:"; diff --git a/dbswitch-core/src/main/java/com/gitee/dbswitch/annotation/Product.java b/dbswitch-core/src/main/java/com/gitee/dbswitch/annotation/Product.java new file mode 100644 index 00000000..115ab2b2 --- /dev/null +++ b/dbswitch-core/src/main/java/com/gitee/dbswitch/annotation/Product.java @@ -0,0 +1,14 @@ +package com.gitee.dbswitch.annotation; + +import com.gitee.dbswitch.common.type.ProductTypeEnum; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Product { + + ProductTypeEnum value(); +} diff --git a/dbswitch-core/src/main/java/com/gitee/dbswitch/provider/AbstractFactoryProvider.java b/dbswitch-core/src/main/java/com/gitee/dbswitch/provider/AbstractFactoryProvider.java index dee44c3c..f84cff13 100644 --- a/dbswitch-core/src/main/java/com/gitee/dbswitch/provider/AbstractFactoryProvider.java +++ b/dbswitch-core/src/main/java/com/gitee/dbswitch/provider/AbstractFactoryProvider.java @@ -9,7 +9,10 @@ ///////////////////////////////////////////////////////////// package com.gitee.dbswitch.provider; +import com.gitee.dbswitch.annotation.Product; +import com.gitee.dbswitch.common.type.ProductTypeEnum; import com.gitee.dbswitch.common.util.ExamineUtils; +import java.util.Objects; import javax.sql.DataSource; public abstract class AbstractFactoryProvider implements ProductFactoryProvider { @@ -25,4 +28,13 @@ public abstract class AbstractFactoryProvider implements ProductFactoryProvider return this.dataSource; } + @Override + public final ProductTypeEnum getProductType() { + Product annotation = getClass().getAnnotation(Product.class); + if (Objects.isNull(annotation)) { + throw new RuntimeException("Should use Product annotation for class :" + getClass().getName()); + } + return annotation.value(); + } + } diff --git a/dbswitch-core/src/main/java/com/gitee/dbswitch/schema/ColumnMetaData.java b/dbswitch-core/src/main/java/com/gitee/dbswitch/schema/ColumnMetaData.java index da620d94..0b84b2a0 100644 --- a/dbswitch-core/src/main/java/com/gitee/dbswitch/schema/ColumnMetaData.java +++ b/dbswitch-core/src/main/java/com/gitee/dbswitch/schema/ColumnMetaData.java @@ -10,7 +10,6 @@ package com.gitee.dbswitch.schema; import com.gitee.dbswitch.common.consts.Constants; -import com.gitee.dbswitch.common.type.ProductTypeEnum; /** * 数据库表列的元信息 @@ -349,8 +348,7 @@ public class ColumnMetaData { } // If we're dealing with PostgreSQL and double precision types - if ((desc.getProductType() == ProductTypeEnum.POSTGRESQL - || desc.getProductType() == ProductTypeEnum.KINGBASE) + if ((desc.getProductType().isLikePostgres()) && type == java.sql.Types.DOUBLE && precision >= 16 && length >= 16) { @@ -360,8 +358,7 @@ public class ColumnMetaData { // MySQL: max resolution is double precision floating point (double) // The (12,31) that is given back is not correct - if (desc.getProductType() == ProductTypeEnum.MYSQL - || desc.getProductType() == ProductTypeEnum.MARIADB) { + if (desc.getProductType().isLikeMysql()) { if (precision >= length) { precision = -1; length = -1; @@ -369,7 +366,7 @@ public class ColumnMetaData { } // If we're dealing with Hive and double/float precision types - if (desc.getProductType() == ProductTypeEnum.HIVE) { + if (desc.getProductType().isLikeHive()) { if (type == java.sql.Types.DOUBLE && precision >= 15 && length >= 15) { @@ -400,8 +397,7 @@ public class ColumnMetaData { } } - if (desc.getProductType() == ProductTypeEnum.POSTGRESQL - || desc.getProductType() == ProductTypeEnum.KINGBASE) { + if (desc.getProductType().isLikePostgres()) { // undefined size => arbitrary precision if (type == java.sql.Types.NUMERIC && length == 0 && precision == 0) { valtype = ColumnMetaData.TYPE_BIGNUMBER; @@ -410,8 +406,7 @@ public class ColumnMetaData { } } - if (desc.getProductType() == ProductTypeEnum.ORACLE || - desc.getProductType() == ProductTypeEnum.DM) { + if (desc.getProductType().isLikeOracle()) { if (precision == 0 && length == 38) { valtype = ColumnMetaData.TYPE_INTEGER; } diff --git a/dbswitch-dist/src/main/assembly/assembly.xml b/dbswitch-dist/src/main/assembly/assembly.xml index c403268c..5b9cc8d8 100644 --- a/dbswitch-dist/src/main/assembly/assembly.xml +++ b/dbswitch-dist/src/main/assembly/assembly.xml @@ -69,7 +69,7 @@ - + ${project.parent.basedir}/dbswitch-data/target lib @@ -88,9 +88,9 @@ - + - ${project.parent.basedir}/dbswitch-product/dbswitch-product-repository/target + ${project.parent.basedir}/dbswitch-product/dbswitch-register-product/target lib @@ -98,7 +98,6 @@ - ${project.parent.basedir}/drivers/ diff --git a/dbswitch-product/dbswitch-product-db2/src/main/java/com/gitee/dbswitch/product/db2/DB2FactoryProvider.java b/dbswitch-product/dbswitch-product-db2/src/main/java/com/gitee/dbswitch/product/db2/DB2FactoryProvider.java index 3b934540..16054aee 100644 --- a/dbswitch-product/dbswitch-product-db2/src/main/java/com/gitee/dbswitch/product/db2/DB2FactoryProvider.java +++ b/dbswitch-product/dbswitch-product-db2/src/main/java/com/gitee/dbswitch/product/db2/DB2FactoryProvider.java @@ -9,6 +9,7 @@ ///////////////////////////////////////////////////////////// package com.gitee.dbswitch.product.db2; +import com.gitee.dbswitch.annotation.Product; import com.gitee.dbswitch.common.type.ProductTypeEnum; import com.gitee.dbswitch.features.ProductFeatures; import com.gitee.dbswitch.provider.AbstractFactoryProvider; @@ -18,17 +19,13 @@ import com.gitee.dbswitch.provider.sync.TableDataSynchronizer; import com.gitee.dbswitch.provider.write.TableDataWriteProvider; import javax.sql.DataSource; +@Product(ProductTypeEnum.DB2) public class DB2FactoryProvider extends AbstractFactoryProvider { public DB2FactoryProvider(DataSource dataSource) { super(dataSource); } - @Override - public ProductTypeEnum getProductType() { - return ProductTypeEnum.DB2; - } - public ProductFeatures getProductFeatures() { return new DB2Features(); } diff --git a/dbswitch-product/dbswitch-product-db2/src/main/resources/META-INF/services/dbswitch.providers b/dbswitch-product/dbswitch-product-db2/src/main/resources/META-INF/services/dbswitch.providers new file mode 100644 index 00000000..23412a77 --- /dev/null +++ b/dbswitch-product/dbswitch-product-db2/src/main/resources/META-INF/services/dbswitch.providers @@ -0,0 +1 @@ +com.gitee.dbswitch.product.db2.DB2FactoryProvider \ No newline at end of file diff --git a/dbswitch-product/dbswitch-product-dm/src/main/java/com/gitee/dbswitch/product/dm/DmFactoryProvider.java b/dbswitch-product/dbswitch-product-dm/src/main/java/com/gitee/dbswitch/product/dm/DmFactoryProvider.java index 8dbffa81..cafd0de1 100644 --- a/dbswitch-product/dbswitch-product-dm/src/main/java/com/gitee/dbswitch/product/dm/DmFactoryProvider.java +++ b/dbswitch-product/dbswitch-product-dm/src/main/java/com/gitee/dbswitch/product/dm/DmFactoryProvider.java @@ -9,6 +9,7 @@ ///////////////////////////////////////////////////////////// package com.gitee.dbswitch.product.dm; +import com.gitee.dbswitch.annotation.Product; import com.gitee.dbswitch.common.type.ProductTypeEnum; import com.gitee.dbswitch.features.ProductFeatures; import com.gitee.dbswitch.provider.AbstractFactoryProvider; @@ -18,17 +19,13 @@ import com.gitee.dbswitch.provider.sync.TableDataSynchronizer; import com.gitee.dbswitch.provider.write.TableDataWriteProvider; import javax.sql.DataSource; +@Product(ProductTypeEnum.DM) public class DmFactoryProvider extends AbstractFactoryProvider { public DmFactoryProvider(DataSource dataSource) { super(dataSource); } - @Override - public ProductTypeEnum getProductType() { - return ProductTypeEnum.DM; - } - public ProductFeatures getProductFeatures() { return new DmFeatures(); } diff --git a/dbswitch-product/dbswitch-product-dm/src/main/resources/META-INF/services/dbswitch.providers b/dbswitch-product/dbswitch-product-dm/src/main/resources/META-INF/services/dbswitch.providers new file mode 100644 index 00000000..566bb788 --- /dev/null +++ b/dbswitch-product/dbswitch-product-dm/src/main/resources/META-INF/services/dbswitch.providers @@ -0,0 +1 @@ +com.gitee.dbswitch.product.dm.DmFactoryProvider \ No newline at end of file diff --git a/dbswitch-product/dbswitch-product-gbase/src/main/java/com/gitee/dbswitch/product/gbase/GbaseFactoryProvider.java b/dbswitch-product/dbswitch-product-gbase/src/main/java/com/gitee/dbswitch/product/gbase/GbaseFactoryProvider.java index 9f9fb85a..1dcb3721 100644 --- a/dbswitch-product/dbswitch-product-gbase/src/main/java/com/gitee/dbswitch/product/gbase/GbaseFactoryProvider.java +++ b/dbswitch-product/dbswitch-product-gbase/src/main/java/com/gitee/dbswitch/product/gbase/GbaseFactoryProvider.java @@ -9,33 +9,29 @@ ///////////////////////////////////////////////////////////// package com.gitee.dbswitch.product.gbase; +import com.gitee.dbswitch.annotation.Product; import com.gitee.dbswitch.common.type.ProductTypeEnum; import com.gitee.dbswitch.features.ProductFeatures; -import com.gitee.dbswitch.product.mysql.MysqlMetadataQueryProvider; import com.gitee.dbswitch.product.mysql.MysqlTableSynchronizer; import com.gitee.dbswitch.provider.AbstractFactoryProvider; import com.gitee.dbswitch.provider.meta.MetadataProvider; import com.gitee.dbswitch.provider.sync.TableDataSynchronizer; import javax.sql.DataSource; +@Product(ProductTypeEnum.GBASE8A) public class GbaseFactoryProvider extends AbstractFactoryProvider { public GbaseFactoryProvider(DataSource dataSource) { super(dataSource); } - @Override - public ProductTypeEnum getProductType() { - return ProductTypeEnum.GBASE8A; - } - public ProductFeatures getProductFeatures() { return new GbaseFeatures(); } @Override public MetadataProvider createMetadataQueryProvider() { - return new MysqlMetadataQueryProvider(this); + return new GbaseMetadataQueryProvider(this); } @Override diff --git a/dbswitch-product/dbswitch-product-gbase/src/main/java/com/gitee/dbswitch/product/gbase/GbaseMetadataQueryProvider.java b/dbswitch-product/dbswitch-product-gbase/src/main/java/com/gitee/dbswitch/product/gbase/GbaseMetadataQueryProvider.java new file mode 100644 index 00000000..f6f9f1d5 --- /dev/null +++ b/dbswitch-product/dbswitch-product-gbase/src/main/java/com/gitee/dbswitch/product/gbase/GbaseMetadataQueryProvider.java @@ -0,0 +1,35 @@ +package com.gitee.dbswitch.product.gbase; + +import com.gitee.dbswitch.common.util.JdbcUrlUtils; +import com.gitee.dbswitch.product.mysql.MysqlMetadataQueryProvider; +import com.gitee.dbswitch.provider.ProductFactoryProvider; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Collections; +import java.util.List; +import java.util.regex.Matcher; + +public class GbaseMetadataQueryProvider extends MysqlMetadataQueryProvider { + + public GbaseMetadataQueryProvider(ProductFactoryProvider factoryProvider) { + super(factoryProvider); + } + + @Override + public List querySchemaList(Connection connection) { + try { + final Matcher matcher = JdbcUrlUtils + .getPattern("jdbc:gbase:://{host}[:{port}]/[{database}][\\?{params}]") + .matcher(connection.getMetaData().getURL()); + if (matcher.matches()) { + return Collections.singletonList(matcher.group("database")); + } + throw new RuntimeException("get database name from jdbc url failed!"); + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (RuntimeException e) { + throw e; + } + } + +} diff --git a/dbswitch-product/dbswitch-product-gbase/src/main/resources/META-INF/services/dbswitch.providers b/dbswitch-product/dbswitch-product-gbase/src/main/resources/META-INF/services/dbswitch.providers new file mode 100644 index 00000000..7296d045 --- /dev/null +++ b/dbswitch-product/dbswitch-product-gbase/src/main/resources/META-INF/services/dbswitch.providers @@ -0,0 +1 @@ +com.gitee.dbswitch.product.gbase.GbaseFactoryProvider \ No newline at end of file diff --git a/dbswitch-product/dbswitch-product-hive/src/main/java/com/gitee/dbswitch/product/hive/HiveFactoryProvider.java b/dbswitch-product/dbswitch-product-hive/src/main/java/com/gitee/dbswitch/product/hive/HiveFactoryProvider.java index 87b03995..deeb8778 100644 --- a/dbswitch-product/dbswitch-product-hive/src/main/java/com/gitee/dbswitch/product/hive/HiveFactoryProvider.java +++ b/dbswitch-product/dbswitch-product-hive/src/main/java/com/gitee/dbswitch/product/hive/HiveFactoryProvider.java @@ -9,6 +9,7 @@ ///////////////////////////////////////////////////////////// package com.gitee.dbswitch.product.hive; +import com.gitee.dbswitch.annotation.Product; import com.gitee.dbswitch.common.type.ProductTypeEnum; import com.gitee.dbswitch.features.ProductFeatures; import com.gitee.dbswitch.provider.AbstractFactoryProvider; @@ -16,17 +17,13 @@ import com.gitee.dbswitch.provider.meta.MetadataProvider; import com.gitee.dbswitch.provider.query.TableDataQueryProvider; import javax.sql.DataSource; +@Product(ProductTypeEnum.HIVE) public class HiveFactoryProvider extends AbstractFactoryProvider { public HiveFactoryProvider(DataSource dataSource) { super(dataSource); } - @Override - public ProductTypeEnum getProductType() { - return ProductTypeEnum.HIVE; - } - public ProductFeatures getProductFeatures() { return new HiveFeatures(); } diff --git a/dbswitch-product/dbswitch-product-hive/src/main/resources/META-INF/services/dbswitch.providers b/dbswitch-product/dbswitch-product-hive/src/main/resources/META-INF/services/dbswitch.providers new file mode 100644 index 00000000..00ccd41f --- /dev/null +++ b/dbswitch-product/dbswitch-product-hive/src/main/resources/META-INF/services/dbswitch.providers @@ -0,0 +1 @@ +com.gitee.dbswitch.product.hive.HiveFactoryProvider \ No newline at end of file diff --git a/dbswitch-product/dbswitch-product-kingbase/src/main/java/com/gitee/dbswitch/product/kingbase/KingbaseFactoryProvider.java b/dbswitch-product/dbswitch-product-kingbase/src/main/java/com/gitee/dbswitch/product/kingbase/KingbaseFactoryProvider.java index 3a302dd5..a399ca88 100644 --- a/dbswitch-product/dbswitch-product-kingbase/src/main/java/com/gitee/dbswitch/product/kingbase/KingbaseFactoryProvider.java +++ b/dbswitch-product/dbswitch-product-kingbase/src/main/java/com/gitee/dbswitch/product/kingbase/KingbaseFactoryProvider.java @@ -9,6 +9,7 @@ ///////////////////////////////////////////////////////////// package com.gitee.dbswitch.product.kingbase; +import com.gitee.dbswitch.annotation.Product; import com.gitee.dbswitch.common.type.ProductTypeEnum; import com.gitee.dbswitch.features.ProductFeatures; import com.gitee.dbswitch.product.postgresql.PostgresTableOperateProvider; @@ -20,17 +21,13 @@ import com.gitee.dbswitch.provider.sync.TableDataSynchronizer; import com.gitee.dbswitch.provider.write.TableDataWriteProvider; import javax.sql.DataSource; +@Product(ProductTypeEnum.KINGBASE) public class KingbaseFactoryProvider extends AbstractFactoryProvider { public KingbaseFactoryProvider(DataSource dataSource) { super(dataSource); } - @Override - public ProductTypeEnum getProductType() { - return ProductTypeEnum.KINGBASE; - } - public ProductFeatures getProductFeatures() { return new KingbaseFeatures(); } diff --git a/dbswitch-product/dbswitch-product-kingbase/src/main/resources/META-INF/services/dbswitch.providers b/dbswitch-product/dbswitch-product-kingbase/src/main/resources/META-INF/services/dbswitch.providers new file mode 100644 index 00000000..fcc0ca33 --- /dev/null +++ b/dbswitch-product/dbswitch-product-kingbase/src/main/resources/META-INF/services/dbswitch.providers @@ -0,0 +1 @@ +com.gitee.dbswitch.product.kingbase.KingbaseFactoryProvider \ No newline at end of file diff --git a/dbswitch-product/dbswitch-product-mariadb/src/main/java/com/gitee/dbswitch/product/mariadb/MariadbFactoryProvider.java b/dbswitch-product/dbswitch-product-mariadb/src/main/java/com/gitee/dbswitch/product/mariadb/MariadbFactoryProvider.java index fda44ced..42cf00ab 100644 --- a/dbswitch-product/dbswitch-product-mariadb/src/main/java/com/gitee/dbswitch/product/mariadb/MariadbFactoryProvider.java +++ b/dbswitch-product/dbswitch-product-mariadb/src/main/java/com/gitee/dbswitch/product/mariadb/MariadbFactoryProvider.java @@ -9,6 +9,7 @@ ///////////////////////////////////////////////////////////// package com.gitee.dbswitch.product.mariadb; +import com.gitee.dbswitch.annotation.Product; import com.gitee.dbswitch.common.type.ProductTypeEnum; import com.gitee.dbswitch.features.ProductFeatures; import com.gitee.dbswitch.product.mysql.MysqlMetadataQueryProvider; @@ -18,17 +19,13 @@ import com.gitee.dbswitch.provider.meta.MetadataProvider; import com.gitee.dbswitch.provider.sync.TableDataSynchronizer; import javax.sql.DataSource; +@Product(ProductTypeEnum.MARIADB) public class MariadbFactoryProvider extends AbstractFactoryProvider { public MariadbFactoryProvider(DataSource dataSource) { super(dataSource); } - @Override - public ProductTypeEnum getProductType() { - return ProductTypeEnum.MARIADB; - } - public ProductFeatures getProductFeatures() { return new MariadbFeatures(); } diff --git a/dbswitch-product/dbswitch-product-mariadb/src/main/resources/META-INF/services/dbswitch.providers b/dbswitch-product/dbswitch-product-mariadb/src/main/resources/META-INF/services/dbswitch.providers new file mode 100644 index 00000000..3aa62335 --- /dev/null +++ b/dbswitch-product/dbswitch-product-mariadb/src/main/resources/META-INF/services/dbswitch.providers @@ -0,0 +1 @@ +com.gitee.dbswitch.product.mariadb.MariadbFactoryProvider \ No newline at end of file diff --git a/dbswitch-product/dbswitch-product-mysql/src/main/java/com/gitee/dbswitch/product/mysql/MysqlFactoryProvider.java b/dbswitch-product/dbswitch-product-mysql/src/main/java/com/gitee/dbswitch/product/mysql/MysqlFactoryProvider.java index 8540bbb4..0ca67479 100644 --- a/dbswitch-product/dbswitch-product-mysql/src/main/java/com/gitee/dbswitch/product/mysql/MysqlFactoryProvider.java +++ b/dbswitch-product/dbswitch-product-mysql/src/main/java/com/gitee/dbswitch/product/mysql/MysqlFactoryProvider.java @@ -9,6 +9,7 @@ ///////////////////////////////////////////////////////////// package com.gitee.dbswitch.product.mysql; +import com.gitee.dbswitch.annotation.Product; import com.gitee.dbswitch.common.type.ProductTypeEnum; import com.gitee.dbswitch.features.ProductFeatures; import com.gitee.dbswitch.provider.AbstractFactoryProvider; @@ -16,17 +17,13 @@ import com.gitee.dbswitch.provider.meta.MetadataProvider; import com.gitee.dbswitch.provider.sync.TableDataSynchronizer; import javax.sql.DataSource; +@Product(ProductTypeEnum.MYSQL) public class MysqlFactoryProvider extends AbstractFactoryProvider { public MysqlFactoryProvider(DataSource dataSource) { super(dataSource); } - @Override - public ProductTypeEnum getProductType() { - return ProductTypeEnum.MYSQL; - } - public ProductFeatures getProductFeatures() { return new MysqlFeatures(); } diff --git a/dbswitch-product/dbswitch-product-mysql/src/main/resources/META-INF/services/dbswitch.providers b/dbswitch-product/dbswitch-product-mysql/src/main/resources/META-INF/services/dbswitch.providers new file mode 100644 index 00000000..c3053607 --- /dev/null +++ b/dbswitch-product/dbswitch-product-mysql/src/main/resources/META-INF/services/dbswitch.providers @@ -0,0 +1 @@ +com.gitee.dbswitch.product.mysql.MysqlFactoryProvider \ No newline at end of file diff --git a/dbswitch-product/dbswitch-product-oracle/src/main/java/com/gitee/dbswitch/product/oracle/OracleFactoryProvider.java b/dbswitch-product/dbswitch-product-oracle/src/main/java/com/gitee/dbswitch/product/oracle/OracleFactoryProvider.java index e636e84d..48ecd633 100644 --- a/dbswitch-product/dbswitch-product-oracle/src/main/java/com/gitee/dbswitch/product/oracle/OracleFactoryProvider.java +++ b/dbswitch-product/dbswitch-product-oracle/src/main/java/com/gitee/dbswitch/product/oracle/OracleFactoryProvider.java @@ -9,6 +9,7 @@ ///////////////////////////////////////////////////////////// package com.gitee.dbswitch.product.oracle; +import com.gitee.dbswitch.annotation.Product; import com.gitee.dbswitch.common.type.ProductTypeEnum; import com.gitee.dbswitch.features.ProductFeatures; import com.gitee.dbswitch.provider.AbstractFactoryProvider; @@ -18,17 +19,13 @@ import com.gitee.dbswitch.provider.sync.TableDataSynchronizer; import com.gitee.dbswitch.provider.write.TableDataWriteProvider; import javax.sql.DataSource; +@Product(ProductTypeEnum.ORACLE) public class OracleFactoryProvider extends AbstractFactoryProvider { public OracleFactoryProvider(DataSource dataSource) { super(dataSource); } - @Override - public ProductTypeEnum getProductType() { - return ProductTypeEnum.ORACLE; - } - public ProductFeatures getProductFeatures() { return new OracleFeatures(); } diff --git a/dbswitch-product/dbswitch-product-oracle/src/main/resources/META-INF/services/dbswitch.providers b/dbswitch-product/dbswitch-product-oracle/src/main/resources/META-INF/services/dbswitch.providers new file mode 100644 index 00000000..34c9d05e --- /dev/null +++ b/dbswitch-product/dbswitch-product-oracle/src/main/resources/META-INF/services/dbswitch.providers @@ -0,0 +1 @@ +com.gitee.dbswitch.product.oracle.OracleFactoryProvider \ No newline at end of file diff --git a/dbswitch-product/dbswitch-product-oscar/src/main/java/com/gitee/dbswitch/product/oscar/OscarFactoryProvider.java b/dbswitch-product/dbswitch-product-oscar/src/main/java/com/gitee/dbswitch/product/oscar/OscarFactoryProvider.java index fc6be83c..63e5917c 100644 --- a/dbswitch-product/dbswitch-product-oscar/src/main/java/com/gitee/dbswitch/product/oscar/OscarFactoryProvider.java +++ b/dbswitch-product/dbswitch-product-oscar/src/main/java/com/gitee/dbswitch/product/oscar/OscarFactoryProvider.java @@ -9,6 +9,7 @@ ///////////////////////////////////////////////////////////// package com.gitee.dbswitch.product.oscar; +import com.gitee.dbswitch.annotation.Product; import com.gitee.dbswitch.common.type.ProductTypeEnum; import com.gitee.dbswitch.features.ProductFeatures; import com.gitee.dbswitch.provider.AbstractFactoryProvider; @@ -18,17 +19,13 @@ import com.gitee.dbswitch.provider.sync.TableDataSynchronizer; import com.gitee.dbswitch.provider.write.TableDataWriteProvider; import javax.sql.DataSource; +@Product(ProductTypeEnum.OSCAR) public class OscarFactoryProvider extends AbstractFactoryProvider { public OscarFactoryProvider(DataSource dataSource) { super(dataSource); } - @Override - public ProductTypeEnum getProductType() { - return ProductTypeEnum.OSCAR; - } - public ProductFeatures getProductFeatures() { return new OscarFeatures(); } diff --git a/dbswitch-product/dbswitch-product-oscar/src/main/resources/META-INF/services/dbswitch.providers b/dbswitch-product/dbswitch-product-oscar/src/main/resources/META-INF/services/dbswitch.providers new file mode 100644 index 00000000..6bc60724 --- /dev/null +++ b/dbswitch-product/dbswitch-product-oscar/src/main/resources/META-INF/services/dbswitch.providers @@ -0,0 +1 @@ +com.gitee.dbswitch.product.oscar.OscarFactoryProvider \ No newline at end of file diff --git a/dbswitch-product/dbswitch-product-postgresql/src/main/java/com/gitee/dbswitch/product/postgresql/PostgresFactoryProvider.java b/dbswitch-product/dbswitch-product-postgresql/src/main/java/com/gitee/dbswitch/product/postgresql/PostgresFactoryProvider.java index f4958f2d..f5df8188 100644 --- a/dbswitch-product/dbswitch-product-postgresql/src/main/java/com/gitee/dbswitch/product/postgresql/PostgresFactoryProvider.java +++ b/dbswitch-product/dbswitch-product-postgresql/src/main/java/com/gitee/dbswitch/product/postgresql/PostgresFactoryProvider.java @@ -9,6 +9,7 @@ ///////////////////////////////////////////////////////////// package com.gitee.dbswitch.product.postgresql; +import com.gitee.dbswitch.annotation.Product; import com.gitee.dbswitch.common.type.ProductTypeEnum; import com.gitee.dbswitch.features.ProductFeatures; import com.gitee.dbswitch.provider.AbstractFactoryProvider; @@ -18,17 +19,13 @@ import com.gitee.dbswitch.provider.sync.TableDataSynchronizer; import com.gitee.dbswitch.provider.write.TableDataWriteProvider; import javax.sql.DataSource; +@Product(ProductTypeEnum.POSTGRESQL) public class PostgresFactoryProvider extends AbstractFactoryProvider { public PostgresFactoryProvider(DataSource dataSource) { super(dataSource); } - @Override - public ProductTypeEnum getProductType() { - return ProductTypeEnum.POSTGRESQL; - } - public ProductFeatures getProductFeatures() { return new PostgresFeatures(); } diff --git a/dbswitch-product/dbswitch-product-postgresql/src/main/resources/META-INF/services/dbswitch.providers b/dbswitch-product/dbswitch-product-postgresql/src/main/resources/META-INF/services/dbswitch.providers new file mode 100644 index 00000000..296881b6 --- /dev/null +++ b/dbswitch-product/dbswitch-product-postgresql/src/main/resources/META-INF/services/dbswitch.providers @@ -0,0 +1 @@ +com.gitee.dbswitch.product.postgresql.PostgresFactoryProvider \ No newline at end of file diff --git a/dbswitch-product/dbswitch-product-repository/src/main/java/com/gitee/dbswitch/product/repository/ProductRegisterAutoConfiguration.java b/dbswitch-product/dbswitch-product-repository/src/main/java/com/gitee/dbswitch/product/repository/ProductRegisterAutoConfiguration.java deleted file mode 100644 index c0dcdfd3..00000000 --- a/dbswitch-product/dbswitch-product-repository/src/main/java/com/gitee/dbswitch/product/repository/ProductRegisterAutoConfiguration.java +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright tang. All rights reserved. -// https://gitee.com/inrgihc/dbswitch -// -// Use of this source code is governed by a BSD-style license -// -// Author: tang (inrgihc@126.com) -// Date : 2020/1/2 -// Location: beijing , china -///////////////////////////////////////////////////////////// -package com.gitee.dbswitch.product.repository; - -import com.gitee.dbswitch.common.type.ProductTypeEnum; -import com.gitee.dbswitch.product.db2.DB2FactoryProvider; -import com.gitee.dbswitch.product.dm.DmFactoryProvider; -import com.gitee.dbswitch.product.gbase.GbaseFactoryProvider; -import com.gitee.dbswitch.product.hive.HiveFactoryProvider; -import com.gitee.dbswitch.product.kingbase.KingbaseFactoryProvider; -import com.gitee.dbswitch.product.mariadb.MariadbFactoryProvider; -import com.gitee.dbswitch.product.mysql.MysqlFactoryProvider; -import com.gitee.dbswitch.product.oracle.OracleFactoryProvider; -import com.gitee.dbswitch.product.oscar.OscarFactoryProvider; -import com.gitee.dbswitch.product.postgresql.PostgresFactoryProvider; -import com.gitee.dbswitch.product.sqlite.SqliteFactoryProvider; -import com.gitee.dbswitch.product.sqlserver.SqlserverFactoryProvider; -import com.gitee.dbswitch.product.sybase.SybaseFactoryProvider; -import com.gitee.dbswitch.provider.ProductProviderFactory; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.context.annotation.Configuration; - -@Slf4j -@Configuration -@ConditionalOnClass(ProductProviderFactory.class) -public class ProductRegisterAutoConfiguration implements InitializingBean { - - @Override - public void afterPropertiesSet() throws Exception { - log.info("Register database product now ..."); - ProductProviderFactory.register(ProductTypeEnum.MYSQL, MysqlFactoryProvider.class.getName()); - ProductProviderFactory.register(ProductTypeEnum.ORACLE, OracleFactoryProvider.class.getName()); - ProductProviderFactory.register(ProductTypeEnum.POSTGRESQL, PostgresFactoryProvider.class.getName()); - ProductProviderFactory.register(ProductTypeEnum.SQLSERVER, SqlserverFactoryProvider.class.getName()); - ProductProviderFactory.register(ProductTypeEnum.DB2, DB2FactoryProvider.class.getName()); - ProductProviderFactory.register(ProductTypeEnum.DM, DmFactoryProvider.class.getName()); - ProductProviderFactory.register(ProductTypeEnum.SYBASE, SybaseFactoryProvider.class.getName()); - ProductProviderFactory.register(ProductTypeEnum.KINGBASE, KingbaseFactoryProvider.class.getName()); - ProductProviderFactory.register(ProductTypeEnum.OSCAR, OscarFactoryProvider.class.getName()); - ProductProviderFactory.register(ProductTypeEnum.SQLITE3, SqliteFactoryProvider.class.getName()); - ProductProviderFactory.register(ProductTypeEnum.MARIADB, MariadbFactoryProvider.class.getName()); - ProductProviderFactory.register(ProductTypeEnum.GBASE8A, GbaseFactoryProvider.class.getName()); - ProductProviderFactory.register(ProductTypeEnum.HIVE, HiveFactoryProvider.class.getName()); - log.info("Finish to register database product "); - } -} diff --git a/dbswitch-product/dbswitch-product-sqlite/src/main/java/com/gitee/dbswitch/product/sqlite/SqliteFactoryProvider.java b/dbswitch-product/dbswitch-product-sqlite/src/main/java/com/gitee/dbswitch/product/sqlite/SqliteFactoryProvider.java index bbaa0720..90195604 100644 --- a/dbswitch-product/dbswitch-product-sqlite/src/main/java/com/gitee/dbswitch/product/sqlite/SqliteFactoryProvider.java +++ b/dbswitch-product/dbswitch-product-sqlite/src/main/java/com/gitee/dbswitch/product/sqlite/SqliteFactoryProvider.java @@ -9,6 +9,7 @@ ///////////////////////////////////////////////////////////// package com.gitee.dbswitch.product.sqlite; +import com.gitee.dbswitch.annotation.Product; import com.gitee.dbswitch.common.type.ProductTypeEnum; import com.gitee.dbswitch.features.ProductFeatures; import com.gitee.dbswitch.provider.AbstractFactoryProvider; @@ -18,17 +19,13 @@ import com.gitee.dbswitch.provider.sync.TableDataSynchronizer; import com.gitee.dbswitch.provider.write.TableDataWriteProvider; import javax.sql.DataSource; +@Product(ProductTypeEnum.SQLITE3) public class SqliteFactoryProvider extends AbstractFactoryProvider { public SqliteFactoryProvider(DataSource dataSource) { super(dataSource); } - @Override - public ProductTypeEnum getProductType() { - return ProductTypeEnum.SQLITE3; - } - public ProductFeatures getProductFeatures() { return new SqliteFeatures(); } diff --git a/dbswitch-product/dbswitch-product-sqlite/src/main/resources/META-INF/services/dbswitch.providers b/dbswitch-product/dbswitch-product-sqlite/src/main/resources/META-INF/services/dbswitch.providers new file mode 100644 index 00000000..4a33ea00 --- /dev/null +++ b/dbswitch-product/dbswitch-product-sqlite/src/main/resources/META-INF/services/dbswitch.providers @@ -0,0 +1 @@ +com.gitee.dbswitch.product.sqlite.SqliteFactoryProvider \ No newline at end of file diff --git a/dbswitch-product/dbswitch-product-sqlserver/src/main/java/com/gitee/dbswitch/product/sqlserver/SqlserverFactoryProvider.java b/dbswitch-product/dbswitch-product-sqlserver/src/main/java/com/gitee/dbswitch/product/sqlserver/SqlserverFactoryProvider.java index 9449daf6..900673cc 100644 --- a/dbswitch-product/dbswitch-product-sqlserver/src/main/java/com/gitee/dbswitch/product/sqlserver/SqlserverFactoryProvider.java +++ b/dbswitch-product/dbswitch-product-sqlserver/src/main/java/com/gitee/dbswitch/product/sqlserver/SqlserverFactoryProvider.java @@ -9,6 +9,7 @@ ///////////////////////////////////////////////////////////// package com.gitee.dbswitch.product.sqlserver; +import com.gitee.dbswitch.annotation.Product; import com.gitee.dbswitch.common.type.ProductTypeEnum; import com.gitee.dbswitch.features.ProductFeatures; import com.gitee.dbswitch.provider.AbstractFactoryProvider; @@ -17,17 +18,13 @@ import com.gitee.dbswitch.provider.operate.TableOperateProvider; import com.gitee.dbswitch.provider.sync.TableDataSynchronizer; import javax.sql.DataSource; +@Product(ProductTypeEnum.SQLSERVER) public class SqlserverFactoryProvider extends AbstractFactoryProvider { public SqlserverFactoryProvider(DataSource dataSource) { super(dataSource); } - @Override - public ProductTypeEnum getProductType() { - return ProductTypeEnum.SQLSERVER; - } - public ProductFeatures getProductFeatures() { return new SqlserverFeatures(); } diff --git a/dbswitch-product/dbswitch-product-sqlserver/src/main/resources/META-INF/services/dbswitch.providers b/dbswitch-product/dbswitch-product-sqlserver/src/main/resources/META-INF/services/dbswitch.providers new file mode 100644 index 00000000..eaf3f58d --- /dev/null +++ b/dbswitch-product/dbswitch-product-sqlserver/src/main/resources/META-INF/services/dbswitch.providers @@ -0,0 +1 @@ +com.gitee.dbswitch.product.sqlserver.SqlserverFactoryProvider \ No newline at end of file diff --git a/dbswitch-product/dbswitch-product-sybase/src/main/java/com/gitee/dbswitch/product/sybase/SybaseFactoryProvider.java b/dbswitch-product/dbswitch-product-sybase/src/main/java/com/gitee/dbswitch/product/sybase/SybaseFactoryProvider.java index 93091d42..2e8be206 100644 --- a/dbswitch-product/dbswitch-product-sybase/src/main/java/com/gitee/dbswitch/product/sybase/SybaseFactoryProvider.java +++ b/dbswitch-product/dbswitch-product-sybase/src/main/java/com/gitee/dbswitch/product/sybase/SybaseFactoryProvider.java @@ -9,6 +9,7 @@ ///////////////////////////////////////////////////////////// package com.gitee.dbswitch.product.sybase; +import com.gitee.dbswitch.annotation.Product; import com.gitee.dbswitch.common.type.ProductTypeEnum; import com.gitee.dbswitch.features.ProductFeatures; import com.gitee.dbswitch.product.sqlserver.SqlserverTableOperateProvider; @@ -19,17 +20,13 @@ import com.gitee.dbswitch.provider.operate.TableOperateProvider; import com.gitee.dbswitch.provider.sync.TableDataSynchronizer; import javax.sql.DataSource; +@Product(ProductTypeEnum.SYBASE) public class SybaseFactoryProvider extends AbstractFactoryProvider { public SybaseFactoryProvider(DataSource dataSource) { super(dataSource); } - @Override - public ProductTypeEnum getProductType() { - return ProductTypeEnum.SYBASE; - } - public ProductFeatures getProductFeatures() { return new SybaseFeatures(); } diff --git a/dbswitch-product/dbswitch-product-sybase/src/main/resources/META-INF/services/dbswitch.providers b/dbswitch-product/dbswitch-product-sybase/src/main/resources/META-INF/services/dbswitch.providers new file mode 100644 index 00000000..03ff1330 --- /dev/null +++ b/dbswitch-product/dbswitch-product-sybase/src/main/resources/META-INF/services/dbswitch.providers @@ -0,0 +1 @@ +com.gitee.dbswitch.product.sybase.SybaseFactoryProvider \ No newline at end of file diff --git a/dbswitch-product/dbswitch-product-repository/pom.xml b/dbswitch-product/dbswitch-register-product/pom.xml similarity index 97% rename from dbswitch-product/dbswitch-product-repository/pom.xml rename to dbswitch-product/dbswitch-register-product/pom.xml index 19267166..47349bcf 100644 --- a/dbswitch-product/dbswitch-product-repository/pom.xml +++ b/dbswitch-product/dbswitch-register-product/pom.xml @@ -77,6 +77,7 @@ dbswitch-product-gbase ${project.version} + \ No newline at end of file diff --git a/dbswitch-product/dbswitch-register-product/src/main/java/com/gitee/dbswitch/product/repository/ProductRegisterAutoConfiguration.java b/dbswitch-product/dbswitch-register-product/src/main/java/com/gitee/dbswitch/product/repository/ProductRegisterAutoConfiguration.java new file mode 100644 index 00000000..83ad1865 --- /dev/null +++ b/dbswitch-product/dbswitch-register-product/src/main/java/com/gitee/dbswitch/product/repository/ProductRegisterAutoConfiguration.java @@ -0,0 +1,135 @@ +// Copyright tang. All rights reserved. +// https://gitee.com/inrgihc/dbswitch +// +// Use of this source code is governed by a BSD-style license +// +// Author: tang (inrgihc@126.com) +// Date : 2020/1/2 +// Location: beijing , china +///////////////////////////////////////////////////////////// +package com.gitee.dbswitch.product.repository; + +import com.gitee.dbswitch.annotation.Product; +import com.gitee.dbswitch.common.type.ProductTypeEnum; +import com.gitee.dbswitch.provider.ProductFactoryProvider; +import com.gitee.dbswitch.provider.ProductProviderFactory; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.List; +import java.util.ServiceConfigurationError; +import java.util.Set; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.BeanClassLoaderAware; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.context.annotation.Configuration; + +@Slf4j +@Configuration +@ConditionalOnClass(ProductProviderFactory.class) +public class ProductRegisterAutoConfiguration implements InitializingBean, BeanClassLoaderAware { + + private static final String SPI_FILE = "META-INF/services/dbswitch.providers"; + private static final Set providers = new HashSet<>(); + + private ClassLoader classLoader; + + private int parseLine(BufferedReader reader, int lc, List names) + throws IOException, ServiceConfigurationError { + String ln = reader.readLine(); + if (ln == null) { + return -1; + } + int ci = ln.indexOf('#'); + if (ci >= 0) { + ln = ln.substring(0, ci); + } + ln = ln.trim(); + int n = ln.length(); + if (n != 0) { + if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0)) { + log.error("Illegal configuration-file syntax: {}", ln); + } + int cp = ln.codePointAt(0); + if (!Character.isJavaIdentifierStart(cp)) { + log.error("Illegal provider-class name: {}", ln); + } + for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) { + cp = ln.codePointAt(i); + if (!Character.isJavaIdentifierPart(cp) && (cp != '.')) { + log.error("Illegal provider-class name: {}", ln); + } + } + if (!providers.contains(ln) && !names.contains(ln)) { + names.add(ln); + } + } + return lc + 1; + } + + private List parse(URL url) throws ServiceConfigurationError { + InputStream in = null; + BufferedReader reader = null; + ArrayList names = new ArrayList<>(); + try { + in = url.openStream(); + reader = new BufferedReader(new InputStreamReader(in, "utf-8")); + int lc = 1; + while ((lc = parseLine(reader, lc, names)) >= 0) { + } + } catch (IOException x) { + log.error("Error reading configuration file", x); + } finally { + try { + if (reader != null) { + reader.close(); + } + if (in != null) { + in.close(); + } + } catch (IOException y) { + log.error("Error closing configuration file", y); + } + } + return names; + } + + @Override + public void setBeanClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } + + @Override + public void afterPropertiesSet() throws Exception { + log.info("Register database product now ..."); + ClassLoader loader = (null != classLoader) + ? classLoader + : ProductProviderFactory.class.getClassLoader(); + Enumeration resources = loader.getResources(SPI_FILE); + while (resources.hasMoreElements()) { + URL url = resources.nextElement(); + providers.addAll(parse(url)); + } + int totalCount = 0; + for (String className : providers) { + Class aClass = classLoader.loadClass(className); + if (ProductFactoryProvider.class.isAssignableFrom(aClass)) { + if (aClass.isAnnotationPresent(Product.class)) { + Product annotation = aClass.getAnnotation(Product.class); + ProductTypeEnum productType = annotation.value(); + if (null != productType) { + ProductProviderFactory.register(productType, className); + ++totalCount; + } + } + } + } + log.info("Finish to register total {} database product !", totalCount); + } +} \ No newline at end of file diff --git a/dbswitch-product/dbswitch-product-repository/src/main/resources/META-INF/spring.factories b/dbswitch-product/dbswitch-register-product/src/main/resources/META-INF/spring.factories similarity index 100% rename from dbswitch-product/dbswitch-product-repository/src/main/resources/META-INF/spring.factories rename to dbswitch-product/dbswitch-register-product/src/main/resources/META-INF/spring.factories diff --git a/dbswitch-product/pom.xml b/dbswitch-product/pom.xml index 29979626..388a6ae7 100644 --- a/dbswitch-product/pom.xml +++ b/dbswitch-product/pom.xml @@ -25,7 +25,7 @@ dbswitch-product-mariadb dbswitch-product-hive dbswitch-product-gbase - dbswitch-product-repository + dbswitch-register-product \ No newline at end of file