From a88cacdd263d17d462763f7b635430bbba8c3c97 Mon Sep 17 00:00:00 2001 From: bwcx_jzy Date: Fri, 23 Oct 2020 14:55:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../java/io/jpom/common/JpomManifest.java | 22 +++++++------- .../jpom/service/dblog/BaseDbLogService.java | 29 +++++++++++++++++-- .../main/java/io/jpom/system/db/DbConfig.java | 13 +++++++++ .../main/java/io/jpom/system/init/InitDb.java | 11 ++++--- modules/server/src/test/java/TestPath.java | 18 ++++++++++++ 6 files changed, 78 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ae79596e..36bd0547c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ 5. 【Server】开始构建时输出代码目录 6. 【Server】编辑构建类型为SVN没有分组bug(感谢@JAVA-落泪归枫) 7. 更新文档Jpom 的JDK要为1.8.0_40+(感谢@JAVA 企鹅) +8. 【Server】数据库初始化时间前置,打印成功日志,未初始化结束数据库相关操作都忽略 ----------------------------------------------------------- diff --git a/modules/common/src/main/java/io/jpom/common/JpomManifest.java b/modules/common/src/main/java/io/jpom/common/JpomManifest.java index ec22a435c..6d922b752 100644 --- a/modules/common/src/main/java/io/jpom/common/JpomManifest.java +++ b/modules/common/src/main/java/io/jpom/common/JpomManifest.java @@ -192,7 +192,9 @@ public class JpomManifest { */ public static File getRunPath() { URL location = ClassUtil.getLocation(JpomApplication.getAppClass()); - return FileUtil.file(location); + String file = location.getFile(); + String before = StrUtil.subBefore(file, "!", false); + return FileUtil.file(before); } /** @@ -221,7 +223,7 @@ public class JpomManifest { * @param clsName 类名 * @return 结果消息 */ - public static JsonMessage checkJpomJar(String path, Class clsName) { + public static JsonMessage checkJpomJar(String path, Class clsName) { String version; File jarFile = new File(path); try (JarFile jarFile1 = new JarFile(jarFile)) { @@ -229,37 +231,37 @@ public class JpomManifest { Attributes attributes = manifest.getMainAttributes(); String mainClass = attributes.getValue(Attributes.Name.MAIN_CLASS); if (mainClass == null) { - return new JsonMessage(405, "清单文件中没有找到对应的MainClass属性"); + return new JsonMessage<>(405, "清单文件中没有找到对应的MainClass属性"); } JarClassLoader jarClassLoader = JarClassLoader.load(jarFile); try { jarClassLoader.loadClass(mainClass); } catch (ClassNotFoundException notFound) { - return new JsonMessage(405, "中没有找到对应的MainClass:" + mainClass); + return new JsonMessage<>(405, "中没有找到对应的MainClass:" + mainClass); } ZipEntry entry = jarFile1.getEntry(StrUtil.format("BOOT-INF/classes/{}.class", StrUtil.replace(clsName.getName(), ".", "/"))); if (entry == null) { - return new JsonMessage(405, "此包不是Jpom【" + JpomApplication.getAppType().name() + "】包"); + return new JsonMessage<>(405, "此包不是Jpom【" + JpomApplication.getAppType().name() + "】包"); } version = attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION); if (StrUtil.isEmpty(version)) { - return new JsonMessage(405, "此包没有版本号"); + return new JsonMessage<>(405, "此包没有版本号"); } String timeStamp = attributes.getValue("Jpom-Timestamp"); if (StrUtil.isEmpty(timeStamp)) { - return new JsonMessage(405, "此包没有版本号"); + return new JsonMessage<>(405, "此包没有版本号"); } timeStamp = parseJpomTime(timeStamp); if (StrUtil.equals(version, JpomManifest.getInstance().getVersion()) && StrUtil.equals(timeStamp, JpomManifest.getInstance().getTimeStamp())) { - return new JsonMessage(405, "新包和正在运行的包一致"); + return new JsonMessage<>(405, "新包和正在运行的包一致"); } } catch (Exception e) { DefaultSystemLog.getLog().error("解析jar", e); - return new JsonMessage(500, " 解析错误:" + e.getMessage()); + return new JsonMessage<>(500, " 解析错误:" + e.getMessage()); } - return new JsonMessage(200, version); + return new JsonMessage<>(200, version); } /** diff --git a/modules/server/src/main/java/io/jpom/service/dblog/BaseDbLogService.java b/modules/server/src/main/java/io/jpom/service/dblog/BaseDbLogService.java index 6a0eb1884..903cb3c0a 100644 --- a/modules/server/src/main/java/io/jpom/service/dblog/BaseDbLogService.java +++ b/modules/server/src/main/java/io/jpom/service/dblog/BaseDbLogService.java @@ -8,6 +8,7 @@ import cn.hutool.db.Entity; import cn.hutool.db.Page; import cn.hutool.db.PageResult; import io.jpom.system.JpomRuntimeException; +import io.jpom.system.db.DbConfig; import java.sql.SQLException; import java.util.ArrayList; @@ -23,8 +24,8 @@ public abstract class BaseDbLogService { /** * 表名 */ - private String tableName; - private Class tClass; + private final String tableName; + private final Class tClass; /** * 主键 */ @@ -53,6 +54,10 @@ public abstract class BaseDbLogService { * @param t 数据 */ public void insert(T t) { + if (!DbConfig.getInstance().isInit()) { + // ignore + return; + } Db db = Db.use(); db.setWrapper((Character) null); try { @@ -82,6 +87,10 @@ public abstract class BaseDbLogService { * @return 影响行数 */ public int update(Entity entity, Entity where) { + if (!DbConfig.getInstance().isInit()) { + // ignore + return 0; + } Db db = Db.use(); db.setWrapper((Character) null); if (where.isEmpty()) { @@ -103,6 +112,10 @@ public abstract class BaseDbLogService { * @return 数据 */ public T getByKey(String keyValue) { + if (!DbConfig.getInstance().isInit()) { + // ignore + return null; + } Entity where = new Entity(tableName); where.set(key, keyValue); Db db = Db.use(); @@ -129,6 +142,10 @@ public abstract class BaseDbLogService { * @return 影响行数 */ public int delByKey(String keyValue) { + if (!DbConfig.getInstance().isInit()) { + // ignore + return 0; + } Entity where = new Entity(tableName); where.set(key, keyValue); return del(where); @@ -141,6 +158,10 @@ public abstract class BaseDbLogService { * @return 影响行数 */ public int del(Entity where) { + if (!DbConfig.getInstance().isInit()) { + // ignore + return 0; + } where.setTableName(tableName); if (where.isEmpty()) { throw new JpomRuntimeException("没有删除条件"); @@ -162,6 +183,10 @@ public abstract class BaseDbLogService { * @return 结果 */ public PageResult listPage(Entity where, Page page) { + if (!DbConfig.getInstance().isInit()) { + // ignore + return new PageResult<>(page.getPageNumber(), page.getPageSize(), 0); + } where.setTableName(getTableName()); PageResult pageResult; Db db = Db.use(); diff --git a/modules/server/src/main/java/io/jpom/system/db/DbConfig.java b/modules/server/src/main/java/io/jpom/system/db/DbConfig.java index 8f50e4a11..027eac42c 100644 --- a/modules/server/src/main/java/io/jpom/system/db/DbConfig.java +++ b/modules/server/src/main/java/io/jpom/system/db/DbConfig.java @@ -31,6 +31,11 @@ public class DbConfig { private static DbConfig dbConfig; + /** + * 是否初始化成功 + */ + private volatile boolean init; + /** * 单利模式 * @@ -43,6 +48,14 @@ public class DbConfig { return dbConfig; } + public void initOk() { + init = true; + } + + public boolean isInit() { + return init; + } + /** * 获取数据库的jdbc 连接 * diff --git a/modules/server/src/main/java/io/jpom/system/init/InitDb.java b/modules/server/src/main/java/io/jpom/system/init/InitDb.java index b307e7ae3..d1d5715db 100644 --- a/modules/server/src/main/java/io/jpom/system/init/InitDb.java +++ b/modules/server/src/main/java/io/jpom/system/init/InitDb.java @@ -14,20 +14,20 @@ import io.jpom.system.db.DbConfig; import java.io.InputStream; - /** * 初始化数据库 * * @author jiangzeyin * @date 2019/4/19 */ -@PreLoadClass +@PreLoadClass(-1) public class InitDb { @PreLoadMethod private static void init() { Setting setting = new Setting(); - setting.set("url", DbConfig.getInstance().getDbUrl()); + DbConfig instance = DbConfig.getInstance(); + setting.set("url", instance.getDbUrl()); setting.set("user", "jpom"); setting.set("pass", "jpom"); // 调试模式显示sql 信息 @@ -36,7 +36,7 @@ public class InitDb { setting.set("sqlLevel", "INFO"); setting.set("showParams", "true"); } - DefaultSystemLog.getLog().info("初始化数据中...."); + DefaultSystemLog.getLog().warn("load h2 db"); try { // 创建连接 DSFactory dsFactory = DSFactory.create(setting); @@ -47,6 +47,9 @@ public class InitDb { } catch (Exception e) { DefaultSystemLog.getLog().error("初始化数据库失败", e); System.exit(0); + return; } + instance.initOk(); + DefaultSystemLog.getLog().warn("h2 db inited"); } } diff --git a/modules/server/src/test/java/TestPath.java b/modules/server/src/test/java/TestPath.java index cdb7f8e56..84376b5c8 100644 --- a/modules/server/src/test/java/TestPath.java +++ b/modules/server/src/test/java/TestPath.java @@ -1,5 +1,11 @@ +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.StrUtil; +import org.junit.Test; import org.springframework.util.AntPathMatcher; +import java.net.MalformedURLException; +import java.net.URL; + /** * @author bwcx_jzy * @date 2019/8/24 @@ -10,4 +16,16 @@ public class TestPath { AntPathMatcher antPathMatcher = new AntPathMatcher(); System.out.println(antPathMatcher.match("/s/**/sss.html", "//s/s/s/sss.html")); } + + @Test + public void test() throws MalformedURLException { + URL url = new URL("jar:file:/home/jpom/server/lib/server-2.4.8.jar!/BOOT-INF/classes!/"); + String file = url.getFile(); + String x = StrUtil.subBefore(file, "!", false); + System.out.println(x); + System.out.println(FileUtil.file(x)); + + System.out.println(StrUtil.subBefore("sssddsf", "!", false)); + + } }