hou 1 dag geleden
bovenliggende
commit
83d76f6ae5
1 gewijzigde bestanden met toevoegingen van 67 en 0 verwijderingen
  1. 67 0
      src/com/mes/util/ConfigUtil.java

+ 67 - 0
src/com/mes/util/ConfigUtil.java

@@ -0,0 +1,67 @@
+package com.mes.util;
+
+import com.mes.ui.MesClient;
+
+import java.io.*;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.Properties;
+
+public class ConfigUtil {
+
+    /** 打包部署时改此版本号,现场启动日志可核对是否为最新 jar */
+    public static final String BUILD_VERSION = "20260703-op030-fix";
+
+    private static String configSource = "unknown";
+
+    public static String getConfigSource(){
+        return configSource;
+    }
+
+    public static File getAppDir(){
+        try{
+            URL url = MesClient.class.getProtectionDomain().getCodeSource().getLocation();
+            File file = new File(url.toURI());
+            if(file.isFile()){
+                return file.getParentFile();
+            }
+            return file;
+        }catch (Exception e){
+            return new File(System.getProperty("user.dir"));
+        }
+    }
+
+    public static Properties loadProperties() throws IOException {
+        Properties pro = new Properties();
+        File external = new File(getAppDir(), "config/config.properties");
+        if(external.isFile()){
+            try(InputStream is = new FileInputStream(external);
+                Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)){
+                pro.load(reader);
+                configSource = external.getAbsolutePath();
+                return pro;
+            }
+        }
+        InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
+        if(is == null){
+            is = MesClient.class.getClassLoader().getResourceAsStream("config/config.properties");
+        }
+        if(is == null){
+            throw new FileNotFoundException("config/config.properties not found");
+        }
+        try(Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)){
+            pro.load(reader);
+            configSource = "classpath:config/config.properties";
+        }
+        return pro;
+    }
+
+    public static void printStartupInfo(){
+        System.out.println("========== MES Client (OP030) ==========");
+        System.out.println("build=" + BUILD_VERSION);
+        System.out.println("appDir=" + getAppDir().getAbsolutePath());
+        System.out.println("config=" + configSource);
+        System.out.println("tcpModule=Netty连接复用版");
+        System.out.println("========================================");
+    }
+}