From afffb718f950d881ca4c7187cc289f3f563050a8 Mon Sep 17 00:00:00 2001 From: qggwy Date: Mon, 25 Apr 2022 00:19:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BF=AE=E6=94=B9=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E8=A1=A8=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/insigma/service/Database.java | 4 +- .../java/com/insigma/service/Middleware.java | 14 +- .../service/impl/LinuxTongWebKingBase.java | 2 +- .../service/impl/WindowsTomcatMysql.java | 60 ++++++- .../com/insigma/utils/WinCommandUtil.java | 158 +++++++++++++++--- .../impl/LinuxTongWebKingBaseTest.java | 2 +- .../service/impl/WindowsTomcatMysqlTest.java | 2 +- 7 files changed, 196 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/insigma/service/Database.java b/src/main/java/com/insigma/service/Database.java index 2f5c95c..aadfd91 100644 --- a/src/main/java/com/insigma/service/Database.java +++ b/src/main/java/com/insigma/service/Database.java @@ -34,7 +34,7 @@ public interface Database { return false; } /*** - * 清除 应用 缓存 + * 清除 数据库 缓存 * @param sql 需要执行脚本集合 * @param tabColObjs 索引对象列表 * @return @@ -45,7 +45,7 @@ public interface Database { } /** - * 实现注册服务 + * 实现数据库注册服务 * @return */ default boolean registrationDbService(){ diff --git a/src/main/java/com/insigma/service/Middleware.java b/src/main/java/com/insigma/service/Middleware.java index 0ba83c3..98c5a63 100644 --- a/src/main/java/com/insigma/service/Middleware.java +++ b/src/main/java/com/insigma/service/Middleware.java @@ -1,9 +1,5 @@ package com.insigma.service; -import com.insigma.entry.IndexObj; - -import java.util.List; - /** * (Middleware 接口) * @@ -13,7 +9,7 @@ import java.util.List; public interface Middleware { /*** - * 清除 应用 缓存 + * 清除 中间件 缓存 * @return */ default boolean cleanMwCache() throws Exception { @@ -21,17 +17,17 @@ public interface Middleware { return false; } /*** - * 设置 应用 大小 + * 设置 中间件 大小 * @param size 设置大小值 * @return */ - default boolean setMwSize(int size) throws Exception { - System.out.printf("设置应用参数:%d,但是未检测到实现应用调用此方法!", size); + default boolean setMwSize(int permSize, int maxPermSize, int xms, int xmx) throws Exception { + System.out.printf("设置应用参数:%d,%d,%d,%d,但是未检测到实现应用调用此方法!", permSize, maxPermSize, xms, xmx); return false; } /** - * 实现注册服务 + * 实现中间件注册服务 * @return */ default boolean registrationMwService(){ diff --git a/src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java b/src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java index 3f6b244..1adf8cb 100644 --- a/src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java +++ b/src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java @@ -56,7 +56,7 @@ public class LinuxTongWebKingBase extends Computer implements Database, Middlewa } @Override - public boolean setMwSize(int size) throws Exception { + public boolean setMwSize(int permSize, int maxPermSize, int xms, int xmx) throws Exception { return true; } diff --git a/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java b/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java index 0dea1e9..f7ce2e2 100644 --- a/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java +++ b/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java @@ -102,7 +102,22 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware @Override public boolean registrationDbService() { - // TODO + if(WinCommandUtil.RegisterUtil.registerWindows( + String.format("SYSTEM\\ControlSet001\\Services\\%s", AppCfg.DB), + AppCfg.DB, + "LocalSystem", + AppCfg.DB, + String.format("%s\\mysql\\bin\\mysqld %s", AppCfg.HZB, AppCfg.DB))){ + return false; + } + if(WinCommandUtil.RegisterUtil.registerWindows( + String.format("SYSTEM\\CurrentControlSet\\Services\\%s", AppCfg.DB), + AppCfg.DB, + "LocalSystem", + AppCfg.DB, + String.format("%s\\mysql\\bin\\mysqld --defaults-file=%s\\mysql\\my.ini %s", AppCfg.HZB, AppCfg.HZB, AppCfg.DB))){ + return false; + } return true; } @@ -136,14 +151,16 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware } @Override - public boolean setMwSize(int size) throws Exception { + public boolean setMwSize(int permSize, int maxPermSize, int xms, int xmx) throws Exception { log.info("停止 中间件 服务..."); WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.MW, AppCfg.CODE); if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){ ws.stopService(); } - // TODO + if(WinCommandUtil.RegisterUtil.registerJava(AppCfg.HZB, permSize, maxPermSize, xms, xmx)){ + return false; + } log.info("启动 中间件 服务..."); ws.startService(); @@ -152,10 +169,39 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware @Override public boolean registrationMwService() { - if(AppCfg.is32Bit){ - runShell(AppCfg.HZB + "/tomcat8/webapps/qggwy/WEB-INF/classes/static/32bit.bat"); - }else{ - runShell(AppCfg.HZB + "/tomcat8/webapps/qggwy/WEB-INF/classes/static/64bit.bat"); + if(WinCommandUtil.RegisterUtil.registerWindows( + String.format("SYSTEM\\ControlSet001\\Services\\%s", AppCfg.DB), + AppCfg.MW, + "NT Authority\\LocalService", + AppCfg.MW, + String.format("%s\\tomcat8\\bin\\Tomcat8.exe //RS//%s", AppCfg.HZB, AppCfg.DB))){ + return false; + } + if(WinCommandUtil.RegisterUtil.registerWindows( + String.format("SYSTEM\\ControlSet001\\Services\\%s", AppCfg.DB), + AppCfg.MW, + "NT Authority\\LocalService", + AppCfg.MW, + String.format("%s\\tomcat8\\bin\\Tomcat8.exe //RS//%s", AppCfg.HZB, AppCfg.DB))){ + return false; + } + if(WinCommandUtil.RegisterUtil.registerJava(AppCfg.HZB,128,256,512,512)){ + return false; + } + if(WinCommandUtil.RegisterUtil.registerLog(AppCfg.HZB)){ + return false; + } + if(WinCommandUtil.RegisterUtil.registerState( + String.format("SOFTWARE\\WOW6432Node\\Apache Software Foundation\\Procrun 2.0\\%s\\Parameters\\Start", AppCfg.DB), + "start", + AppCfg.HZB)){ + return false; + } + if(WinCommandUtil.RegisterUtil.registerState( + String.format("SOFTWARE\\WOW6432Node\\Apache Software Foundation\\Procrun 2.0\\%s\\Parameters\\Stop", AppCfg.DB), + "stop", + AppCfg.HZB)){ + return false; } return true; } diff --git a/src/main/java/com/insigma/utils/WinCommandUtil.java b/src/main/java/com/insigma/utils/WinCommandUtil.java index 73573b4..0b36db2 100644 --- a/src/main/java/com/insigma/utils/WinCommandUtil.java +++ b/src/main/java/com/insigma/utils/WinCommandUtil.java @@ -1,12 +1,14 @@ package com.insigma.utils; -import lombok.NonNull; +import com.insigma.config.AppCfg; import lombok.extern.slf4j.Slf4j; import org.springframework.util.StringUtils; import com.sun.jna.platform.win32.Advapi32Util; import com.sun.jna.platform.win32.WinReg; + import java.io.*; +import java.util.Objects; /** * CommandImpUtil Windows 下执行命令
@@ -21,10 +23,11 @@ public class WinCommandUtil implements Runnable { private final String CMD = "cmd"; private final String EXE = "cmd /c "; + public WinCommandUtil(String command) { - if(StringUtils.startsWithIgnoreCase(command, CMD)){ + if (StringUtils.startsWithIgnoreCase(command, CMD)) { this.command = command; - }else{ + } else { this.command = EXE + command; } } @@ -34,7 +37,7 @@ public class WinCommandUtil implements Runnable { Process process; int exitVal = 0; try { - log.debug("准备执行命令:{}",command); + log.debug("准备执行命令:{}", command); process = Runtime.getRuntime().exec(command); // Runtime.exec()创建的子进程公用父进程的流,不同平台上,父进程的stream buffer可能被打满导致子进程阻塞,从而永远无法返回。 //针对这种情况,我们只需要将子进程的stream重定向出来即可。 @@ -82,29 +85,119 @@ public class WinCommandUtil implements Runnable { * %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
*/ public static class RegisterUtil { + /* 注册服务部分 【1】修改注册表 */ + public static boolean registerWindows(String path, + String serviceName, + String objectName, + String displayName, + String imagePath) { + log.info("操作:{},{},{},{}", path, serviceName, objectName, displayName); + if (registryKeyExists(path)) { + return true; + } + try { + Advapi32Util.registrySetIntValue(WinReg.HKEY_LOCAL_MACHINE, path, "Type", 16); + Advapi32Util.registrySetIntValue(WinReg.HKEY_LOCAL_MACHINE, path, "Start", 2); + Advapi32Util.registrySetIntValue(WinReg.HKEY_LOCAL_MACHINE, path, "ErrorControl", 2); + Advapi32Util.registrySetIntValue(WinReg.HKEY_LOCAL_MACHINE, path, "WOW64", 332); + Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "ObjectName", objectName); + Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "DisplayName", displayName); + Advapi32Util.registrySetExpandableStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "ImagePath", imagePath); + if (Objects.equals(AppCfg.MW, serviceName)) { + Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "Description", "Apache Tomcat 8.5.55 Server - https://tomcat.apache.org/"); + Advapi32Util.registrySetStringArray(WinReg.HKEY_LOCAL_MACHINE, path, "Description", new String[]{"Tcpip", "Afd"}); + } + path += "\\Parameters"; + if (registryKeyExists(path)) { + return true; + } + } catch (Exception e) { + log.error("操作发生异常:{}", e.getMessage()); + return false; + } + return true; + } + /* 注册服务部分 【2】修改注册表 Java */ + public static boolean registerJava(String hzbPath, + int permSize, + int maxPermSize, + int xms, + int xmx) { + String path = String.format("SOFTWARE\\WOW6432Node\\Apache Software Foundation\\Procrun 2.0\\%s\\Parameters\\Java", AppCfg.MW); + log.info("操作:{},{},{},{},{},{}", path, hzbPath, permSize, maxPermSize, xms, xmx); + if (registryKeyExists(path)) { + return true; + } + try { + Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "Jvm", String.format("%s\\tomcat8/JDK1.8/jre\\bin\\server\\jvm.dll", hzbPath)); + Advapi32Util.registrySetStringArray(WinReg.HKEY_LOCAL_MACHINE, path, "Options", new String[]{ + String.format("-Dcatalina.home=%s\\tomcat8", hzbPath), + String.format("-Djava.endorsed.dirs=%s\\tomcat8\\endorsed", hzbPath), + String.format("-Djava.io.tmpdir=%s\\tomcat8\\temp", hzbPath), + "-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager", + String.format("-Djava.util.logging.config.file=%s\\tomcat8\\conf\\logging.properties", hzbPath), + "-Dfile.encoding=GBK", + String.format("-XX:PermSize=%dm", permSize), + String.format("-XX:MaxPermSize=%dm", maxPermSize) + }); + Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "Classpath", + String.format("%s\\tomcat8\\bin\\bootstrap.jar;%s\\tomcat8\\bin\\tomcat-juli.jar", hzbPath, hzbPath)); + Advapi32Util.registrySetIntValue(WinReg.HKEY_LOCAL_MACHINE, path, "JvmMs", xms); + Advapi32Util.registrySetIntValue(WinReg.HKEY_LOCAL_MACHINE, path, "JvmMx", xmx); + } catch (Exception e) { + log.error("操作发生异常:{}", e.getMessage()); + return false; + } + return true; + } + /* 注册服务部分 【3】修改注册表 Log */ + public static boolean registerLog(String hzbPath) { + String path = String.format("SOFTWARE\\WOW6432Node\\Apache Software Foundation\\Procrun 2.0\\%s\\Parameters\\Log", AppCfg.MW); + log.info("操作:{},{}", path, hzbPath); + if (registryKeyExists(path)) { + return true; + } + try { + Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "Path", String.format("%s\\tomcat8\\logs", hzbPath)); + Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "StdError", "auto"); + Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "StdOutput", "auto"); + } catch (Exception e) { + log.error("操作发生异常:{}", e.getMessage()); + return false; + } + return true; + } + /* 注册服务部分 【4】修改注册表 State */ + public static boolean registerState(String path, String state, String hzbPath) { + log.info("操作:{},{},{}", path, state, hzbPath); + if (registryKeyExists(path)) { + return true; + } + try { + Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "Class", "org.apache.catalina.startup.Bootstrap"); + Advapi32Util.registrySetStringArray(WinReg.HKEY_LOCAL_MACHINE, path, "Params", new String[]{state}); + Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "Mode", "jvm"); + Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "WorkingPath", String.format("%s\\tomcat8", hzbPath)); + } catch (Exception e) { + log.error("操作发生异常:{}", e.getMessage()); + return false; + } + return true; + } /** * 向指定目录下的keyPath中, 写入key-value * 当 HKEY不同时, 可能会出现 "拒绝访问" 的问题, 此时需要手动打开注册表, 修改对应的keyPath父级目录(此处为SOFTWARE)的全部读写权限 - * @param path - * @param key 操作值 + * + * @param path 操作地址 + * @param key 操作值 * @param value 设置值 + * @return 是否操作成功 */ - public static boolean writeToRegistry(@NonNull String path, @NonNull String key, @NonNull String value, @NonNull String type) { + public static boolean writeToRegistry(String path, String key, String value) { log.info("操作:{} , {} , {}", path, key, value); - // 判断KeyPath是否存在 - boolean isOkay = Advapi32Util.registryKeyExists(WinReg.HKEY_LOCAL_MACHINE, path); - - // 若不存在 - if (!isOkay) { - try { - // 创建keyPath(即, 目录) - Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, path); - } catch (Exception e) { - log.error("操作发生异常:{}", e.getMessage()); - // 遇到异常, 写入失败 - return false; - } + if (registryKeyExists(path)) { + return true; } try { @@ -120,12 +213,26 @@ public class WinCommandUtil implements Runnable { return true; } + private static boolean registryKeyExists(String path) { + // 判断KeyPath是否存在 + boolean isOkay = Advapi32Util.registryKeyExists(WinReg.HKEY_LOCAL_MACHINE, path); + if (!isOkay) { + try { + Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, path); + } catch (Exception e) { + log.error("操作发生异常:{}", e.getMessage()); + return true; + } + } + return false; + } + /*** * 删除注册表中的keyPath及旗下key-value对 * @param path 操作路径 "SOFTWARE\\你要添加的keyPath" - * @return + * @return 是否操作成功 */ - public static boolean deleteRegistryKey(@NonNull String path) { + public static boolean deleteRegistryKey(String path) { log.info("假定操作:{} ", path); try { // Delete a key @@ -142,11 +249,12 @@ public class WinCommandUtil implements Runnable { /** * 从注册表中读取key对应的value值 + * * @param path 操作路径 "SOFTWARE\\你要添加的keyPath" - * @param key 操作值 - * @return + * @param key 操作值 + * @return 是否操作成功 */ - public static String readKeyFromRegistry(@NonNull String path, @NonNull String key) { + public static String readKeyFromRegistry(String path, String key) { String value = null; try { // 根据 key(value所在目录) 返回注册表中类型为 REG_SZ 的 value 对应的值 diff --git a/src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java b/src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java index 76818be..87ea2bc 100644 --- a/src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java +++ b/src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java @@ -58,7 +58,7 @@ public class LinuxTongWebKingBaseTest { @Test public void setMwSize() throws Exception { - assertTrue(linuxTongWebKingBase.setMwSize(500)); + assertTrue(linuxTongWebKingBase.setMwSize(128,256,512,512)); log.info("测试完成!"); } diff --git a/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java b/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java index c3d3eaf..68b857c 100644 --- a/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java +++ b/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java @@ -57,7 +57,7 @@ public class WindowsTomcatMysqlTest { @Test public void setMwSize() throws Exception { - assertTrue(windowsTomcatMysql.setMwSize(500)); + assertTrue(windowsTomcatMysql.setMwSize(128,256,512,512)); log.info("测试完成!"); }