From 0946c7d0e6d1ec97083b7c142d77ab63c7210b6a Mon Sep 17 00:00:00 2001 From: VIVIMAN Date: Thu, 21 Apr 2022 18:58:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=8D=E5=8A=A1=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- run.bat | 12 + .../service/impl/WindowsTomcatMysql.java | 3 + .../com/insigma/utils/WinCommandUtil.java | 5 - .../com/insigma/utils/WinServiceTool.java | 309 ++++++++++++++++++ .../utils => main/resources/static}/32b.bat | 0 .../utils => main/resources/static}/64b.bat | 0 src/main/resources/templates/.keepgit | 0 .../service/impl/WindowsTomcatMysqlTest.java | 8 +- .../com/insigma/utils/WinServiceToolTest.java | 33 ++ 9 files changed, 364 insertions(+), 6 deletions(-) create mode 100644 run.bat create mode 100644 src/main/java/com/insigma/utils/WinServiceTool.java rename src/{test/java/com/insigma/utils => main/resources/static}/32b.bat (100%) rename src/{test/java/com/insigma/utils => main/resources/static}/64b.bat (100%) delete mode 100644 src/main/resources/templates/.keepgit create mode 100644 src/test/java/com/insigma/utils/WinServiceToolTest.java diff --git a/run.bat b/run.bat new file mode 100644 index 0000000..412f3fe --- /dev/null +++ b/run.bat @@ -0,0 +1,12 @@ +call :IsAdmin + +java -jar app.jar + +:IsAdmin +Reg.exe query "HKU\S-1-5-19\Environment" +If Not %ERRORLEVEL% EQU 0 ( + Cls & Echo You must have administrator rights to continue ... + Pause & Exit +) +Cls +goto:eof diff --git a/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java b/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java index 3c8967f..e042ea1 100644 --- a/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java +++ b/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java @@ -117,6 +117,9 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware FileUtil.delAllFile(AppCfg.HZB + "/tomcat8/temp"); FileUtil.delAllFile(AppCfg.HZB + "/tomcat8/work/Catalina/localhost/qggwy"); + log.info("删除 应用 日志文件..."); + FileUtil.delAllFile(AppCfg.HZB + "/tomcat8/logs"); + log.info("启动 中间件 服务..."); runShell("sc start " + AppCfg.MW); return true; diff --git a/src/main/java/com/insigma/utils/WinCommandUtil.java b/src/main/java/com/insigma/utils/WinCommandUtil.java index 7778b48..97a649c 100644 --- a/src/main/java/com/insigma/utils/WinCommandUtil.java +++ b/src/main/java/com/insigma/utils/WinCommandUtil.java @@ -73,9 +73,4 @@ public class WinCommandUtil implements Runnable { } } } - - public static class RegisterUtil { - // TODO 无法绕开系统权限 - - } } \ No newline at end of file diff --git a/src/main/java/com/insigma/utils/WinServiceTool.java b/src/main/java/com/insigma/utils/WinServiceTool.java new file mode 100644 index 0000000..9a64a72 --- /dev/null +++ b/src/main/java/com/insigma/utils/WinServiceTool.java @@ -0,0 +1,309 @@ +package com.insigma.utils; + +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.thymeleaf.util.StringUtils; + +import java.io.*; +import java.nio.charset.Charset; + +/** + * Windows系统工具类 + * + * @author Zaki Chen + */ +@Slf4j +@NoArgsConstructor +public class WinServiceTool { + + /** + * 获取windows服务实例对象 + * + * @param serviceName + * @return + */ + public static WindowsService getService(String serviceName) { + return new WindowsService(serviceName, Charset.defaultCharset()); + } + + /** + * 获取windows服务实例对象 + * + * @param serviceName + * @param charset windows命令执行输出流字符集 + * @return + */ + public static WindowsService getService(String serviceName, Charset charset) { + return new WindowsService(serviceName, charset); + } + + /** + * 获取windows服务实例对象 + * + * @param serviceName + * @param charset windows命令执行输出流字符集 + * @return + */ + + public static WindowsService getService(String serviceName, String charset) { + return new WindowsService(serviceName, Charset.forName(charset)); + } + + /** + * 读取输入流中的文本信息 + * + * @param input 文本输入流 + * @param charset 文本编码 + * @return + */ + private static String convertInputStream2Str(InputStream input, Charset charset) { + final char[] buffer = new char[4096]; + final StringBuilder out = new StringBuilder(); + try { + final Reader in = new InputStreamReader(input, charset); + try { + for (; ; ) { + int rsz = in.read(buffer, 0, buffer.length); + if (rsz < 0) { + break; + } + out.append(buffer, 0, rsz); + } + } finally { + in.close(); + } + } catch (Exception ex) { + log.error("执行发生异常:{}", ex.getMessage()); + } + return out.toString(); + } + + public static class WindowsService { + + /** + * 服务名称 + */ + private String serviceName; + /** + * 服务是否存在 + */ + private boolean isExists; + /** + * 服务运行状态 + */ + private ServiceState runningStatus; + /** + * 服务信息 + */ + private String info; + + /** + * windows状态码服务_运行中 + */ + private final static String SERVICE_RUNNING_CODE = "4"; + + /** + * windows状态码服务_正在停止运行 + */ + private final static String SERVICE_STOPPING_CODE = "3"; + + /** + * windows状态码服务_已停止运行 + */ + private final static String SERVICE_STOPPED_CODE = "1"; + + /** + * windows操作系统cmd指令返回内容的字符集 + */ + private Charset charset; + + /** + * 直接完成状态的初始化 + * + * @param serviceName + */ + public WindowsService(String serviceName, Charset charset) { + this.serviceName = serviceName; + this.charset = charset; + Runtime runtime = Runtime.getRuntime(); + String result; + try { + Process process = runtime.exec("sc query " + serviceName); + InputStream inputStream = process.getInputStream(); + result = convertInputStream2Str(inputStream, Charset.defaultCharset()); + } catch (IOException e) { + e.printStackTrace(); + isExists = false; + runningStatus = ServiceState.UNKNOWN; + info = null; + return; + } + String[] split = result.split("\n"); + if (split.length < 5) { + isExists = false; + runningStatus = ServiceState.UNKNOWN; + info = null; + return; + } + isExists = true; + String state = split[3].split(":")[1].trim(); + String code = state.substring(0, 1); + info = state.substring(3); + if (SERVICE_RUNNING_CODE.equals(code)) { + runningStatus = ServiceState.RUNNING; + } else if (SERVICE_STOPPING_CODE.equals(code)) { + runningStatus = ServiceState.STOPPING; + } else if (SERVICE_STOPPED_CODE.equals(code)) { + runningStatus = ServiceState.STOPPED; + } else { + runningStatus = ServiceState.UNKNOWN; + } + } + + /** + * 重启服务 + * + * @return 执行结果 + */ + public boolean restartService() throws Exception { + if (!isExists) { + return false; + } + boolean isStop = false; + if (getRunningStatus() == ServiceState.RUNNING) { + isStop = stopService(); + } + if (isStop) { + boolean isStart = startService(); + if (isStart) { + return true; + } + } + return false; + } + + /** + * 启动服务 + * + * @return 执行结果 + */ + public boolean startService() throws Exception { + if (!isExists) { + return false; + } + boolean ret = false; + if (getRunningStatus() == ServiceState.STOPPED) { + try { + Process exec = Runtime.getRuntime().exec("net start " + serviceName); + InputStream errorStream = exec.getErrorStream(); + String errMsg = convertInputStream2Str(errorStream, getCharset()); + if (StringUtils.isEmpty(errMsg)) { + throw new Exception("执行启动服务[" + getServiceName() + "]出错:\n" + errMsg); + } + runningStatus = ServiceState.RUNNING; + ret = true; + } catch (IOException e) { + log.error("执行发生异常:{}", e.getMessage()); + } + } + return ret; + } + + /** + * 停止服务 + * + * @return 执行结果 + */ + public boolean stopService() throws Exception { + if (!isExists) { + return false; + } + boolean ret = false; + if (getRunningStatus() == ServiceState.RUNNING) { + try { + Process exec = Runtime.getRuntime().exec("net stop " + serviceName); + InputStream errorStream = exec.getErrorStream(); + String errMsg = convertInputStream2Str(errorStream, getCharset()); + if (StringUtils.isEmpty(errMsg)) { + throw new Exception("执行停止服务[" + getServiceName() + "]出错:\n" + errMsg); + } + runningStatus = ServiceState.STOPPED; + ret = true; + } catch (IOException e) { + e.printStackTrace(); + } + } + return ret; + } + + private enum ServiceState { + /** + * 正在运行 + */ + RUNNING, + /** + * 正在停止 + */ + STOPPING, + /** + * 已停止 + */ + STOPPED, + /** + * 未知(服务不存在) + */ + UNKNOWN + } + + + /** + * 服务名 + * + * @return + */ + public String getServiceName() { + return serviceName; + } + + /** + * 服务是否存在 + * + * @return + */ + public boolean isExists() { + return isExists; + } + + /** + * 服务运行状态 + * + * @return + */ + public ServiceState getRunningStatus() { + return runningStatus; + } + + /** + * 服务信息 + * + * @return + */ + public String getInfo() { + return info; + } + + public Charset getCharset() { + return charset; + } + + @Override + public String toString() { + return "WindowsService{" + + "serviceName='" + serviceName + '\'' + + ", isExists=" + isExists + + ", runningStatus=" + runningStatus + + ", info='" + info + '\'' + + '}'; + } + } +} diff --git a/src/test/java/com/insigma/utils/32b.bat b/src/main/resources/static/32b.bat similarity index 100% rename from src/test/java/com/insigma/utils/32b.bat rename to src/main/resources/static/32b.bat diff --git a/src/test/java/com/insigma/utils/64b.bat b/src/main/resources/static/64b.bat similarity index 100% rename from src/test/java/com/insigma/utils/64b.bat rename to src/main/resources/static/64b.bat diff --git a/src/main/resources/templates/.keepgit b/src/main/resources/templates/.keepgit deleted file mode 100644 index e69de29..0000000 diff --git a/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java b/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java index 4e040c0..aea6ee5 100644 --- a/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java +++ b/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java @@ -68,7 +68,13 @@ public class WindowsTomcatMysqlTest { @Test public void runShell() { - assertTrue(windowsTomcatMysql.runShell("")); + assertTrue(windowsTomcatMysql.runShell("SERVICES.MSC")); + log.info("测试完成!"); + } + + @Test + public void openServer() { + windowsTomcatMysql.openServer(); log.info("测试完成!"); } } \ No newline at end of file diff --git a/src/test/java/com/insigma/utils/WinServiceToolTest.java b/src/test/java/com/insigma/utils/WinServiceToolTest.java new file mode 100644 index 0000000..7ad4bfc --- /dev/null +++ b/src/test/java/com/insigma/utils/WinServiceToolTest.java @@ -0,0 +1,33 @@ +package com.insigma.utils; + +import com.insigma.config.AppCfg; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +@Slf4j +public class WinServiceToolTest { + + static { + DbUtil.url = "jdbc:mysql://127.0.0.1:35017/hy_qggwy"; + DbUtil.usr = "root"; + DbUtil.pwd = "admin"; + AppCfg.DB = "GWY20_Mysql"; + AppCfg.MW = "GWY20_Tomcat"; + AppCfg.HZB = "D:/hzb2021"; + AppCfg.BROWSER = "360se.exe"; + } + + @Test + public void getService() throws Exception { + + WinServiceTool.WindowsService windowsService = WinServiceTool.getService(AppCfg.DB, "GBK"); + log.info("停止代码:{}", windowsService); + assertTrue(windowsService.stopService()); + log.info("启动代码:{}", windowsService); + assertTrue(windowsService.startService()); + log.info("重启代码:{}", windowsService); + assertTrue(windowsService.restartService()); + } +} \ No newline at end of file