Browse Source

增加修改注册表代码

master
qggwy 3 years ago
parent
commit
afffb718f9
  1. 4
      src/main/java/com/insigma/service/Database.java
  2. 14
      src/main/java/com/insigma/service/Middleware.java
  3. 2
      src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java
  4. 60
      src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java
  5. 154
      src/main/java/com/insigma/utils/WinCommandUtil.java
  6. 2
      src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java
  7. 2
      src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java

4
src/main/java/com/insigma/service/Database.java

@ -34,7 +34,7 @@ public interface Database {
return false; return false;
} }
/*** /***
* 清除 应用 缓存 * 清除 数据库 缓存
* @param sql 需要执行脚本集合 * @param sql 需要执行脚本集合
* @param tabColObjs 索引对象列表 * @param tabColObjs 索引对象列表
* @return * @return
@ -45,7 +45,7 @@ public interface Database {
} }
/** /**
* 实现注册服务 * 实现数据库注册服务
* @return * @return
*/ */
default boolean registrationDbService(){ default boolean registrationDbService(){

14
src/main/java/com/insigma/service/Middleware.java

@ -1,9 +1,5 @@
package com.insigma.service; package com.insigma.service;
import com.insigma.entry.IndexObj;
import java.util.List;
/** /**
* (Middleware 接口) * (Middleware 接口)
* *
@ -13,7 +9,7 @@ import java.util.List;
public interface Middleware { public interface Middleware {
/*** /***
* 清除 应用 缓存 * 清除 中间件 缓存
* @return * @return
*/ */
default boolean cleanMwCache() throws Exception { default boolean cleanMwCache() throws Exception {
@ -21,17 +17,17 @@ public interface Middleware {
return false; return false;
} }
/*** /***
* 设置 应用 大小 * 设置 中间件 大小
* @param size 设置大小值 * @param size 设置大小值
* @return * @return
*/ */
default boolean setMwSize(int size) throws Exception { default boolean setMwSize(int permSize, int maxPermSize, int xms, int xmx) throws Exception {
System.out.printf("设置应用参数:%d,但是未检测到实现应用调用此方法!", size); System.out.printf("设置应用参数:%d,%d,%d,%d,但是未检测到实现应用调用此方法!", permSize, maxPermSize, xms, xmx);
return false; return false;
} }
/** /**
* 实现注册服务 * 实现中间件注册服务
* @return * @return
*/ */
default boolean registrationMwService(){ default boolean registrationMwService(){

2
src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java

@ -56,7 +56,7 @@ public class LinuxTongWebKingBase extends Computer implements Database, Middlewa
} }
@Override @Override
public boolean setMwSize(int size) throws Exception { public boolean setMwSize(int permSize, int maxPermSize, int xms, int xmx) throws Exception {
return true; return true;
} }

60
src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java

@ -102,7 +102,22 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
@Override @Override
public boolean registrationDbService() { 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; return true;
} }
@ -136,14 +151,16 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
} }
@Override @Override
public boolean setMwSize(int size) throws Exception { public boolean setMwSize(int permSize, int maxPermSize, int xms, int xmx) throws Exception {
log.info("停止 中间件 服务..."); log.info("停止 中间件 服务...");
WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.MW, AppCfg.CODE); WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.MW, AppCfg.CODE);
if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){ if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){
ws.stopService(); ws.stopService();
} }
// TODO if(WinCommandUtil.RegisterUtil.registerJava(AppCfg.HZB, permSize, maxPermSize, xms, xmx)){
return false;
}
log.info("启动 中间件 服务..."); log.info("启动 中间件 服务...");
ws.startService(); ws.startService();
@ -152,10 +169,39 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
@Override @Override
public boolean registrationMwService() { public boolean registrationMwService() {
if(AppCfg.is32Bit){ if(WinCommandUtil.RegisterUtil.registerWindows(
runShell(AppCfg.HZB + "/tomcat8/webapps/qggwy/WEB-INF/classes/static/32bit.bat"); String.format("SYSTEM\\ControlSet001\\Services\\%s", AppCfg.DB),
}else{ AppCfg.MW,
runShell(AppCfg.HZB + "/tomcat8/webapps/qggwy/WEB-INF/classes/static/64bit.bat"); "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; return true;
} }

154
src/main/java/com/insigma/utils/WinCommandUtil.java

@ -1,12 +1,14 @@
package com.insigma.utils; package com.insigma.utils;
import lombok.NonNull; import com.insigma.config.AppCfg;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import com.sun.jna.platform.win32.Advapi32Util; import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.WinReg; import com.sun.jna.platform.win32.WinReg;
import java.io.*; import java.io.*;
import java.util.Objects;
/** /**
* CommandImpUtil Windows 下执行命令 <br/> * CommandImpUtil Windows 下执行命令 <br/>
@ -21,10 +23,11 @@ public class WinCommandUtil implements Runnable {
private final String CMD = "cmd"; private final String CMD = "cmd";
private final String EXE = "cmd /c "; private final String EXE = "cmd /c ";
public WinCommandUtil(String command) { public WinCommandUtil(String command) {
if(StringUtils.startsWithIgnoreCase(command, CMD)){ if (StringUtils.startsWithIgnoreCase(command, CMD)) {
this.command = command; this.command = command;
}else{ } else {
this.command = EXE + command; this.command = EXE + command;
} }
} }
@ -34,7 +37,7 @@ public class WinCommandUtil implements Runnable {
Process process; Process process;
int exitVal = 0; int exitVal = 0;
try { try {
log.debug("准备执行命令:{}",command); log.debug("准备执行命令:{}", command);
process = Runtime.getRuntime().exec(command); process = Runtime.getRuntime().exec(command);
// Runtime.exec()创建的子进程公用父进程的流,不同平台上,父进程的stream buffer可能被打满导致子进程阻塞,从而永远无法返回。 // Runtime.exec()创建的子进程公用父进程的流,不同平台上,父进程的stream buffer可能被打满导致子进程阻塞,从而永远无法返回。
//针对这种情况,我们只需要将子进程的stream重定向出来即可。 //针对这种情况,我们只需要将子进程的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 <br/> * %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit <br/>
*/ */
public static class RegisterUtil { 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 * 向指定目录下的keyPath中, 写入key-value
* HKEY不同时, 可能会出现 "拒绝访问" 的问题, 此时需要手动打开注册表, 修改对应的keyPath父级目录(此处为SOFTWARE)的全部读写权限 * HKEY不同时, 可能会出现 "拒绝访问" 的问题, 此时需要手动打开注册表, 修改对应的keyPath父级目录(此处为SOFTWARE)的全部读写权限
* @param path *
* @param path 操作地址
* @param key 操作值 * @param key 操作值
* @param value 设置值 * @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); log.info("操作:{} , {} , {}", path, key, value);
// 判断KeyPath是否存在 if (registryKeyExists(path)) {
boolean isOkay = Advapi32Util.registryKeyExists(WinReg.HKEY_LOCAL_MACHINE, path); return true;
// 若不存在
if (!isOkay) {
try {
// 创建keyPath(即, 目录)
Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, path);
} catch (Exception e) {
log.error("操作发生异常:{}", e.getMessage());
// 遇到异常, 写入失败
return false;
}
} }
try { try {
@ -120,12 +213,26 @@ public class WinCommandUtil implements Runnable {
return true; 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对 * 删除注册表中的keyPath及旗下key-value对
* @param path 操作路径 "SOFTWARE\\你要添加的keyPath" * @param path 操作路径 "SOFTWARE\\你要添加的keyPath"
* @return * @return 是否操作成功
*/ */
public static boolean deleteRegistryKey(@NonNull String path) { public static boolean deleteRegistryKey(String path) {
log.info("假定操作:{} ", path); log.info("假定操作:{} ", path);
try { try {
// Delete a key // Delete a key
@ -142,11 +249,12 @@ public class WinCommandUtil implements Runnable {
/** /**
* 从注册表中读取key对应的value值 * 从注册表中读取key对应的value值
*
* @param path 操作路径 "SOFTWARE\\你要添加的keyPath" * @param path 操作路径 "SOFTWARE\\你要添加的keyPath"
* @param key 操作值 * @param key 操作值
* @return * @return 是否操作成功
*/ */
public static String readKeyFromRegistry(@NonNull String path, @NonNull String key) { public static String readKeyFromRegistry(String path, String key) {
String value = null; String value = null;
try { try {
// 根据 key(value所在目录) 返回注册表中类型为 REG_SZ 的 value 对应的值 // 根据 key(value所在目录) 返回注册表中类型为 REG_SZ 的 value 对应的值

2
src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java

@ -58,7 +58,7 @@ public class LinuxTongWebKingBaseTest {
@Test @Test
public void setMwSize() throws Exception { public void setMwSize() throws Exception {
assertTrue(linuxTongWebKingBase.setMwSize(500)); assertTrue(linuxTongWebKingBase.setMwSize(128,256,512,512));
log.info("测试完成!"); log.info("测试完成!");
} }

2
src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java

@ -57,7 +57,7 @@ public class WindowsTomcatMysqlTest {
@Test @Test
public void setMwSize() throws Exception { public void setMwSize() throws Exception {
assertTrue(windowsTomcatMysql.setMwSize(500)); assertTrue(windowsTomcatMysql.setMwSize(128,256,512,512));
log.info("测试完成!"); log.info("测试完成!");
} }

Loading…
Cancel
Save