修护tomcat路径问题

This commit is contained in:
bwcx_jzy 2019-06-14 09:45:48 +08:00
parent 6437e08710
commit 306e35f085
5 changed files with 108 additions and 63 deletions

View File

@ -9,6 +9,7 @@ import cn.keepbx.jpom.system.JpomRuntimeException;
/**
* tomcat命令执行工具类
*
* @author LF
*/
public abstract class AbstractTomcatCommander {
@ -35,16 +36,18 @@ public abstract class AbstractTomcatCommander {
/**
* 执行tomcat命令
*
* @param tomcatInfoModel tomcat信息
* @param cmd 执行的命令包括start stop
* @param cmd 执行的命令包括start stop
* @return 返回tomcat启动结果
*/
public abstract String execCmd(TomcatInfoModel tomcatInfoModel, String cmd);
/**
* 检查tomcat状态
*
* @param tomcatInfoModel tomcat信息
* @param cmd 操作命令
* @param cmd 操作命令
* @return 状态结果
*/
protected String getStatus(TomcatInfoModel tomcatInfoModel, String cmd) {
@ -54,11 +57,13 @@ public abstract class AbstractTomcatCommander {
int result = 0;
String url = String.format("http://127.0.0.1:%d/", tomcatInfoModel.getPort());
HttpRequest httpRequest = new HttpRequest(url);
httpRequest.setConnectionTimeout(3000); // 设置超时时间为3秒
// 设置超时时间为3秒
httpRequest.setConnectionTimeout(3000);
try {
httpRequest.execute();
result = 1;
} catch (Exception ignored) {}
} catch (Exception ignored) {
}
i++;
if ("start".equals(cmd) && result == 1) {
@ -71,7 +76,8 @@ public abstract class AbstractTomcatCommander {
}
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {}
} catch (InterruptedException ignored) {
}
}
return strReturn;
}

View File

@ -1,5 +1,7 @@
package cn.keepbx.jpom.common.commander.impl;
import cn.hutool.core.util.StrUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.keepbx.jpom.common.commander.AbstractTomcatCommander;
import cn.keepbx.jpom.model.data.TomcatInfoModel;
@ -9,14 +11,17 @@ import java.util.concurrent.TimeUnit;
/**
* tomcat的linux管理命令
*
* @author LF
*/
public class LinuxTomcatCommander extends AbstractTomcatCommander {
@Override
public String execCmd(TomcatInfoModel tomcatInfoModel, String cmd) {
String tomcatPath = tomcatInfoModel.pathAndCheck();
if (StrUtil.isBlank(tomcatPath)) {
return "tomcat path blank";
}
// 拼接命令
String command = String.format("java -Djava.util.logging.config.file=%sconf/logging.properties " +
"-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager " +
@ -27,9 +32,9 @@ public class LinuxTomcatCommander extends AbstractTomcatCommander {
"-Dcatalina.base=%s " +
"-Dcatalina.home=%s " +
"-Djava.io.tmpdir=%stemp/ " +
"org.apache.catalina.startup.Bootstrap %s", tomcatInfoModel.getPath(), tomcatInfoModel.getPath(),
tomcatInfoModel.getPath(), tomcatInfoModel.getPath(), tomcatInfoModel.getPath(),
tomcatInfoModel.getPath(), tomcatInfoModel.getPath(), cmd);
"org.apache.catalina.startup.Bootstrap %s", tomcatPath, tomcatPath,
tomcatPath, tomcatPath, tomcatPath,
tomcatPath, tomcatPath, cmd);
try {
// 执行命令
Process process = Runtime.getRuntime().exec(command);
@ -39,7 +44,7 @@ public class LinuxTomcatCommander extends AbstractTomcatCommander {
process.waitFor(5, TimeUnit.SECONDS);
process.destroy();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
DefaultSystemLog.ERROR().error("tomcat执行名称失败", e);
}
// 查询操作结果并返回
return getStatus(tomcatInfoModel, cmd);

View File

@ -1,5 +1,7 @@
package cn.keepbx.jpom.common.commander.impl;
import cn.hutool.core.util.StrUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.keepbx.jpom.common.commander.AbstractTomcatCommander;
import cn.keepbx.jpom.model.data.TomcatInfoModel;
@ -9,34 +11,40 @@ import java.util.concurrent.TimeUnit;
/**
* tomcat的Windows管理命令
*
* @author LF
*/
public class WindowsTomcatCommander extends AbstractTomcatCommander {
/**
* windows下执行tomcat命令
*
* @param tomcatInfoModel tomcat信息
* @param cmd 执行的命令包括start stop
* @param cmd 执行的命令包括start stop
* @return 返回tomcat启动结果
*/
@Override
public String execCmd(TomcatInfoModel tomcatInfoModel, String cmd) {
String tomcatPath = tomcatInfoModel.pathAndCheck();
if (StrUtil.isBlank(tomcatPath)) {
return "tomcat path blank";
}
// 拼接命令
String command = String.format("cmd /c java -Djava.util.logging.config.file=\"%sconf/logging.properties\" " +
"-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager " +
"-Djdk.tls.ephemeralDHKeySize=2048" +
"-Djava.protocol.handler.pkgs=org.apache.catalina.webresources " +
"-Dignore.endorsed.dirs=\"%s\" " +
"-classpath \"%sbin/bootstrap.jar;%sbin/tomcat-juli.jar\" " +
"-Dcatalina.base=\"%s\" " +
"-Dcatalina.home=\"%s\" " +
"-Djava.io.tmpdir=\"%stemp/\" " +
"org.apache.catalina.startup.Bootstrap %s", tomcatInfoModel.getPath(), tomcatInfoModel.getPath(),
tomcatInfoModel.getPath(), tomcatInfoModel.getPath(), tomcatInfoModel.getPath(),
tomcatInfoModel.getPath(), tomcatInfoModel.getPath(), cmd);
"-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager " +
"-Djdk.tls.ephemeralDHKeySize=2048" +
"-Djava.protocol.handler.pkgs=org.apache.catalina.webresources " +
"-Dignore.endorsed.dirs=\"%s\" " +
"-classpath \"%sbin/bootstrap.jar;%sbin/tomcat-juli.jar\" " +
"-Dcatalina.base=\"%s\" " +
"-Dcatalina.home=\"%s\" " +
"-Djava.io.tmpdir=\"%stemp/\" " +
"org.apache.catalina.startup.Bootstrap %s", tomcatPath, tomcatPath,
tomcatPath, tomcatPath, tomcatPath,
tomcatPath, tomcatPath, cmd);
try {
// 执行命令
Process process = Runtime.getRuntime().exec(command, null, new File(tomcatInfoModel.getPath()));
Process process = Runtime.getRuntime().exec(command, null, new File(tomcatPath));
process.getInputStream().close();
process.getErrorStream().close();
@ -44,7 +52,7 @@ public class WindowsTomcatCommander extends AbstractTomcatCommander {
process.waitFor(5, TimeUnit.SECONDS);
process.destroy();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
DefaultSystemLog.ERROR().error("tomcat执行名称失败", e);
}
// 查询操作结果并返回

View File

@ -120,41 +120,6 @@ public class TomcatManageController extends BaseAgentController {
return JsonMessage.getString(200, "保存成功");
}
/**
* 判断是否是Tomcat的根路径
*
* @return 返回是否是Tomcat根路径
*/
private boolean isTomcatRoot(String path) {
File file = new File(path);
if (file.exists()) {
if (file.isFile()) {
return false;
} else {
File[] files = file.listFiles();
if (files == null) {
return false;
}
// 判断该目录下是否
for (File child : files) {
if ("bin".equals(child.getName()) && child.isDirectory()) {
File[] binFiles = child.listFiles();
if (binFiles == null) {
return false;
}
for (File binChild : binFiles) {
if ("bootstrap.jar".equals(binChild.getName()) && binChild.isFile()) {
return true;
}
}
}
}
return false;
}
} else {
return false;
}
}
/**
* 修改Tomcat信息
@ -187,7 +152,7 @@ public class TomcatManageController extends BaseAgentController {
private String doInitTomcat(TomcatInfoModel tomcatInfoModel) {
String tomcatPath = tomcatInfoModel.getPath();
// 判断Tomcat路径是否正确
if (!isTomcatRoot(tomcatPath)) {
if (!TomcatInfoModel.isTomcatRoot(tomcatPath)) {
return JsonMessage.getString(405, String.format("没有在路径:%s 下检测到Tomcat", tomcatPath));
}

View File

@ -1,7 +1,16 @@
package cn.keepbx.jpom.model.data;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.keepbx.jpom.model.BaseModel;
import java.io.File;
/**
* tomcat 对象实体
*
* @author lf
*/
public class TomcatInfoModel extends BaseModel {
private String path;
@ -14,7 +23,22 @@ public class TomcatInfoModel extends BaseModel {
private String modifyTime;
public String getPath() {
return path;
if (path == null) {
return null;
}
return FileUtil.normalize(path + "/");
}
public String pathAndCheck() {
String path = getPath();
if (path == null) {
return null;
}
path = FileUtil.normalize(path + "/");
if (isTomcatRoot(path)) {
return path;
}
throw new RuntimeException("tomcat path error:" + path);
}
public void setPath(String path) {
@ -76,4 +100,41 @@ public class TomcatInfoModel extends BaseModel {
public void setModifyTime(String modifyTime) {
this.modifyTime = modifyTime;
}
/**
* 判断是否是Tomcat的根路径
*
* @return 返回是否是Tomcat根路径
*/
public static boolean isTomcatRoot(String path) {
File file = new File(path);
if (file.exists()) {
if (file.isFile()) {
return false;
} else {
File[] files = file.listFiles();
if (files == null) {
return false;
}
// 判断该目录下是否
for (File child : files) {
if ("bin".equals(child.getName()) && child.isDirectory()) {
File[] binFiles = child.listFiles();
if (binFiles == null) {
return false;
}
for (File binChild : binFiles) {
if ("bootstrap.jar".equals(binChild.getName()) && binChild.isFile()) {
return true;
}
}
}
}
return false;
}
} else {
return false;
}
}
}