Эх сурвалжийг харах

DataUpgrade 规范版本号格式

deason 3 сар өмнө
parent
commit
96f5909392

+ 3 - 1
data-upgrade/src/main/java/com/qmth/boot/data/upgrade/config/DataUpgradeListener.java

@@ -174,7 +174,9 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
             }
 
             String implVersion = annotation.value();
-            // 校验规则:只允许x.x.x格式的版本号 todo
+            if (!VersionComparator.isValidVersion(implVersion)) {
+                throw new RuntimeException("实现类声明的版本号格式不正确!" + clazz.getSimpleName() + " @DataUpgradeVersion:" + implVersion);
+            }
 
             if (vc.compare(implVersion, currentVersion) > 0 && vc.compare(implVersion, appVersion) <= 0) {
                 // log.info("{} {}", clazz.getSimpleName(), implVersion);

+ 1 - 1
data-upgrade/src/main/java/com/qmth/boot/data/upgrade/utils/ClassHelper.java

@@ -27,7 +27,7 @@ public class ClassHelper {
                     instances.add((T) constructor.newInstance());
                 }
             } catch (Exception e) {
-                log.warn("{} class newInstance fail. {}", clazz.getCanonicalName(), e.getMessage());
+                log.warn("{} class newInstance fail. {}", clazz.getSimpleName(), e.getMessage());
             }
         }
 

+ 3 - 4
data-upgrade/src/main/java/com/qmth/boot/data/upgrade/utils/ResourceFileHelper.java

@@ -13,15 +13,14 @@ public class ResourceFileHelper {
     private static final Logger log = LoggerFactory.getLogger(ResourceFileHelper.class);
 
     public static String readContent(String resourcePath) {
-        try {
-            InputStream is = ResourceFileHelper.class.getClassLoader().getResourceAsStream(resourcePath);
+        try (InputStream is = ResourceFileHelper.class.getClassLoader().getResourceAsStream(resourcePath);) {
             if (is == null) {
                 throw new RuntimeException(resourcePath + " file is not exist!");
             }
 
             return ResourceFileHelper.readContent(is);
         } catch (Exception e) {
-            throw new RuntimeException(e);
+            throw new RuntimeException(e.getMessage(), e);
         }
     }
 
@@ -36,7 +35,7 @@ public class ResourceFileHelper {
             }
             return result.toString();
         } catch (Exception e) {
-            throw new RuntimeException(e);
+            throw new RuntimeException(e.getMessage(), e);
         }
     }
 

+ 32 - 2
data-upgrade/src/main/java/com/qmth/boot/data/upgrade/utils/VersionComparator.java

@@ -1,11 +1,20 @@
 package com.qmth.boot.data.upgrade.utils;
 
+import org.apache.commons.lang3.StringUtils;
+
 import java.util.Comparator;
 
 public class VersionComparator implements Comparator<String> {
 
     @Override
     public int compare(String v1, String v2) {
+        if (!VersionComparator.isValidVersion(v1)) {
+            throw new IllegalArgumentException("版本号" + v1 + "格式不正确,格式如:数字.数字.数字!");
+        }
+        if (!VersionComparator.isValidVersion(v2)) {
+            throw new IllegalArgumentException("版本号" + v2 + "格式不正确,格式如:数字.数字.数字!");
+        }
+
         // 分割版本号为数字段
         String[] parts1 = v1.split("\\.");
         String[] parts2 = v2.split("\\.");
@@ -15,14 +24,35 @@ public class VersionComparator implements Comparator<String> {
 
         for (int i = 0; i < maxLength; i++) {
             // 处理长度不一致的情况,缺失段视为0
-            int num1 = (i < parts1.length) ? Integer.parseInt(parts1[i]) : 0;
-            int num2 = (i < parts2.length) ? Integer.parseInt(parts2[i]) : 0;
+            int num1 = (i < parts1.length) ? parseNumber(parts1[i]) : 0;
+            int num2 = (i < parts2.length) ? parseNumber(parts2[i]) : 0;
 
             if (num1 != num2) {
                 return Integer.compare(num1, num2);
             }
         }
+
         return 0;
     }
 
+    public static int parseNumber(String str) {
+        try {
+            return Integer.parseInt(str);
+        } catch (NumberFormatException e) {
+            throw new IllegalArgumentException("字符串" + str + "转换为数字失败!");
+        }
+    }
+
+    /**
+     * 校验版本号,格式如:数字.数字.数字
+     */
+    public static boolean isValidVersion(String version) {
+        if (StringUtils.isBlank(version)) {
+            return false;
+        }
+        // String regex = "\\d+\\.\\d+\\.\\d+";
+        String regex = "\\d+(\\.\\d+)*$";
+        return version.matches(regex);
+    }
+
 }