Skip to content

Commit

Permalink
1. h2数据库文件优化, 取消h2server启动
Browse files Browse the repository at this point in the history
2. 项目结构优化, pom版本统一管理
3. core的beanutils依赖升级为commons-beanutils2版本, 修复之前版本的安全风险.
4. 此版本打包部署需要替换之前所有依赖
  • Loading branch information
qaiu committed Jun 6, 2024
1 parent aaae7ab commit 7663320
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 127 deletions.
3 changes: 1 addition & 2 deletions core-database/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>netdisk-fast-download</artifactId>
<groupId>cn.qaiu</groupId>
<version>0.1.7</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -23,7 +23,6 @@
<dependency>
<groupId>cn.qaiu</groupId>
<artifactId>core</artifactId>
<version>1.0.8</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
Expand Down
22 changes: 14 additions & 8 deletions core-database/src/main/java/cn/qaiu/db/ddl/CreateTable.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.qaiu.db.ddl;

import cn.qaiu.db.pool.JDBCType;
import cn.qaiu.vx.core.util.ReflectionUtil;
import io.vertx.codegen.format.CamelCase;
import io.vertx.codegen.format.Case;
Expand Down Expand Up @@ -65,7 +66,9 @@ private static Case getCase(Class<?> clz) {
}
}

public static String getCreateTableSQL(Class<?> clz) {
public static String getCreateTableSQL(Class<?> clz, JDBCType type) {
String quotationMarks = type == JDBCType.H2DB ? "\"" : "`";
String endStr = type == JDBCType.H2DB ? ");" : ")ENGINE=InnoDB DEFAULT CHARSET=utf8;";
// 判断类上是否有次注解
String primaryKey = null; // 主键
String tableName = null; // 表名
Expand Down Expand Up @@ -93,7 +96,7 @@ public static String getCreateTableSQL(Class<?> clz) {
int[] decimalSize = {22, 2};
int varcharSize = 255;
StringBuilder sb = new StringBuilder(50);
sb.append("CREATE TABLE IF NOT EXISTS \"").append(tableName).append("\" ( \r\n ");
sb.append("CREATE TABLE IF NOT EXISTS ").append(quotationMarks).append(tableName).append(quotationMarks).append(" ( \r\n ");
boolean firstId = true;
for (Field f : fields) {
Class<?> paramType = f.getType();
Expand All @@ -114,7 +117,7 @@ public static String getCreateTableSQL(Class<?> clz) {
decimalSize = fieldAnnotation.decimalSize();
varcharSize = fieldAnnotation.varcharSize();
}
sb.append("\"").append(column).append("\"");
sb.append(quotationMarks).append(column).append(quotationMarks);
sb.append(" ").append(sqlType);
// 添加类型长度
if (sqlType.equals("DECIMAL")) {
Expand Down Expand Up @@ -155,17 +158,20 @@ public static String getCreateTableSQL(Class<?> clz) {
//去掉最后一个逗号
int lastIndex = sql.lastIndexOf(",");
sql = sql.substring(0, lastIndex) + sql.substring(lastIndex + 1);
return sql.substring(0, sql.length() - 1) + ");\r\n";
return sql.substring(0, sql.length() - 1) + endStr;
}

public static void createTable(JDBCPool pool) {
public static void createTable(JDBCPool pool, JDBCType type) {
Set<Class<?>> tableClassList = ReflectionUtil.getReflections().getTypesAnnotatedWith(Table.class);
if (tableClassList.isEmpty()) LOGGER.info("Table model class not fount");
tableClassList.forEach(clazz -> {
String createTableSQL = getCreateTableSQL(clazz);
String createTableSQL = getCreateTableSQL(clazz, type);

pool.query(createTableSQL).execute().onSuccess(
rs -> LOGGER.info("\n" + createTableSQL + "create table --> ok")
).onFailure(Throwable::printStackTrace);
rs -> LOGGER.info("table auto generate:\n" + createTableSQL)
).onFailure(e -> {
LOGGER.error(e.getMessage() + " SQL: \n" + createTableSQL);
});
});
}
}
78 changes: 12 additions & 66 deletions core-database/src/main/java/cn/qaiu/db/pool/JDBCPoolInit.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package cn.qaiu.db.pool;

import cn.qaiu.db.ddl.CreateTable;
import cn.qaiu.db.server.H2ServerHolder;
import cn.qaiu.vx.core.util.VertxHolder;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.jdbcclient.JDBCPool;
import org.h2.tools.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

/**
* 初始化JDBC
* <br>Create date 2021/8/10 12:04
Expand All @@ -28,12 +21,14 @@ public class JDBCPoolInit {
JsonObject dbConfig;
Vertx vertx = VertxHolder.getVertxInstance();
String url;
private JDBCType type;

private static JDBCPoolInit instance;

public JDBCPoolInit(Builder builder) {
this.dbConfig = builder.dbConfig;
this.url = builder.url;
this.type = builder.type;
}

public static Builder builder() {
Expand All @@ -47,10 +42,12 @@ public static JDBCPoolInit instance() {
public static class Builder {
private JsonObject dbConfig;
private String url;
private JDBCType type;

public Builder config(JsonObject dbConfig) {
this.dbConfig = dbConfig;
this.url = dbConfig.getString("jdbcUrl");
this.type = JDBCUtil.getJDBCType(dbConfig.getString("driverClassName"));
return this;
}

Expand All @@ -73,67 +70,16 @@ public void initPool() {
return;
}

// 异步启动H2服务
vertx.createSharedWorkerExecutor("h2-server", 1, Long.MAX_VALUE)
.executeBlocking(this::h2serverExecute)
.onSuccess(res->{
LOGGER.info(res);
// 初始化数据库连接
vertx.createSharedWorkerExecutor("sql-pool-init")
.executeBlocking(this::poolInitExecute)
.onSuccess(LOGGER::info)
.onFailure(Throwable::printStackTrace);
// 初始化数据库连接
vertx.createSharedWorkerExecutor("sql-pool-init")
.executeBlocking(() -> {
// 初始化连接池
pool = JDBCPool.pool(vertx, dbConfig);
CreateTable.createTable(pool, type);
return "数据库连接初始化: URL=" + url;
})
.onSuccess(LOGGER::info)
.onFailure(Throwable::printStackTrace);


}

private void poolInitExecute(Promise<String> promise) {
// 初始化连接池
pool = JDBCPool.pool(vertx, dbConfig);
CreateTable.createTable(pool);
promise.complete("init jdbc pool success");

}

private void checkOrCreateDBFile() {
LOGGER.info("init sql start");
String[] path = url.split("\\./");
path[1] = path[1].split(";")[0];
path[1] += ".mv.db";
File file = new File(path[1]);
if (!file.exists()) {
if (!file.getParentFile().exists()) {
if (file.getParentFile().mkdirs()) {
LOGGER.info("mkdirs -> {}", file.getParentFile().getAbsolutePath());
}
}
try {
if (file.createNewFile()) {
LOGGER.info("create file -> {}", file.getAbsolutePath());
}
} catch (IOException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("file create failed");
}
}
}

private void h2serverExecute(Promise<String> promise) {
// 初始化H2db, 创建本地db文件
checkOrCreateDBFile();

try {
String url = dbConfig.getString("jdbcUrl");
String[] portStr = url.split(":");
String port = portStr[portStr.length - 1].split("[/\\\\]")[0];
LOGGER.info("H2server listen port to {}", port);
H2ServerHolder.init(Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", port).start());
promise.complete("Start h2Server success");
} catch (SQLException e) {
throw new RuntimeException("Start h2Server failed: " + e.getMessage());
}
}

/**
Expand Down
23 changes: 6 additions & 17 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,15 @@
<parent>
<artifactId>netdisk-fast-download</artifactId>
<groupId>cn.qaiu</groupId>
<version>0.1.7</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>1.0.8</version>
<artifactId>core</artifactId>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-dependencies</artifactId>
<version>${vertx.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!--logback日志实现-->
<dependency>
Expand Down Expand Up @@ -73,11 +60,13 @@
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.melloware/commons-beanutils2 -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
<groupId>com.melloware</groupId>
<artifactId>commons-beanutils2</artifactId>
<version>${commons-beanutils2.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/cn/qaiu/vx/core/util/CommonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import cn.qaiu.vx.core.annotaions.HandleSortFilter;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils2.ConvertUtils;
import org.apache.commons.beanutils2.Converter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/cn/qaiu/vx/core/util/ParamUtil.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.qaiu.vx.core.util;

import io.vertx.core.MultiMap;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils2.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
11 changes: 6 additions & 5 deletions core/src/main/java/cn/qaiu/vx/core/util/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.reflections.Reflections;
import org.reflections.scanners.*;
import org.reflections.scanners.MemberUsageScanner;
import org.reflections.scanners.MethodParameterNamesScanner;
import org.reflections.scanners.Scanners;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
Expand Down Expand Up @@ -185,10 +186,10 @@ public static Object conversion(CtClass ctClass, String value, String fmt) {
return DateUtils.parseDate(value, fmt);
} catch (ParseException e) {
e.printStackTrace();
throw new ConversionException("无法将格式化日期");
throw new RuntimeException("无法将格式化日期");
}
default:
throw new ConversionException("无法将String类型" + value + "转为[" + name + "]");
throw new RuntimeException("无法将String类型" + value + "转为[" + name + "]");
}
}

Expand All @@ -200,7 +201,7 @@ public static Object conversion(CtClass ctClass, String value, String fmt) {
* @return Array
*/
public static Object conversionArray(CtClass ctClass, String value) {
if (!isBasicTypeArray(ctClass)) throw new ConversionException("无法解析数组");
if (!isBasicTypeArray(ctClass)) throw new RuntimeException("无法解析数组");
String[] strArr = value.split(",");
List<Object> obj = new ArrayList<>();
Arrays.stream(strArr).forEach(v -> obj.add(conversion(ctClass, v, null)));
Expand Down
5 changes: 2 additions & 3 deletions parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>netdisk-fast-download</artifactId>
<groupId>cn.qaiu</groupId>
<version>0.1.7</version>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>parser</artifactId>

<properties>
Expand Down
33 changes: 32 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>cn.qaiu</groupId>
<artifactId>netdisk-fast-download</artifactId>
<packaging>pom</packaging>
<version>0.1.7</version>
<version>${revision}</version>

<modules>
<module>core</module>
Expand All @@ -17,6 +17,7 @@
</modules>

<properties>
<revision>0.1.7</revision>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
Expand All @@ -30,10 +31,40 @@
<lombok.version>1.18.12</lombok.version>
<slf4j.version>2.0.5</slf4j.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<commons-beanutils2.version>2.0.0</commons-beanutils2.version>
<jackson.version>2.14.2</jackson.version>
<logback.version>1.4.12</logback.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-dependencies</artifactId>
<version>${vertx.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>cn.qaiu</groupId>
<artifactId>core</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>cn.qaiu</groupId>
<artifactId>core-database</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>cn.qaiu</groupId>
<artifactId>parser</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
Expand Down
Loading

0 comments on commit 7663320

Please sign in to comment.