|
@ -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 对应的值
|
|
|