修护未加载到tools.jar判断

This commit is contained in:
bwcx_jzy 2019-09-05 01:31:51 -05:00
parent a338c4614d
commit 8297866ce4
15 changed files with 89 additions and 54 deletions

View File

@ -10,6 +10,7 @@
### 解决BUG、优化功能
1. 【Server】节点分发需要节点数大于二感谢@Sawyer
2. 修护未加载到tools.jar判断感谢@java-磊)
-----------------------------------------------------------

View File

@ -5,12 +5,12 @@
<parent>
<artifactId>jpom-parent</artifactId>
<groupId>io.jpom</groupId>
<version>2.4.4</version>
<version>2.4.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>agent</artifactId>
<version>2.4.4</version>
<version>2.4.5</version>
<name>Jpom 插件端</name>
<properties>
<start-class>io.jpom.JpomAgentApplication</start-class>

View File

@ -218,7 +218,7 @@ public abstract class AbstractProjectCommander {
try (JarFile jarFile1 = new JarFile(jarFile)) {
Manifest manifest = jarFile1.getManifest();
Attributes attributes = manifest.getMainAttributes();
String mainClass = attributes.getValue("Main-Class");
String mainClass = attributes.getValue(Attributes.Name.MAIN_CLASS);
if (mainClass == null) {
return jarFile.getAbsolutePath() + "中没有找到对应的MainClass属性";
}

View File

@ -5,13 +5,13 @@
<parent>
<artifactId>jpom-parent</artifactId>
<groupId>io.jpom</groupId>
<version>2.4.4</version>
<version>2.4.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>Jpom 公共模块</name>
<artifactId>common</artifactId>
<version>2.4.4</version>
<version>2.4.5</version>
<dependencies>
<!-- 文件编码识别-->

View File

@ -226,7 +226,7 @@ public class JpomManifest {
try (JarFile jarFile1 = new JarFile(jarFile)) {
Manifest manifest = jarFile1.getManifest();
Attributes attributes = manifest.getMainAttributes();
String mainClass = attributes.getValue("Main-Class");
String mainClass = attributes.getValue(Attributes.Name.MAIN_CLASS);
if (mainClass == null) {
return new JsonMessage(405, "清单文件中没有找到对应的MainClass属性");
}

View File

@ -0,0 +1,35 @@
package io.jpom.system.init;
import cn.jiangzeyin.common.DefaultSystemLog;
import io.jpom.JpomApplication;
import io.jpom.common.JpomManifest;
import io.jpom.util.JvmUtil;
import sun.jvmstat.monitor.MonitoredVm;
import sun.jvmstat.monitor.VmIdentifier;
import java.util.List;
/**
* @author bwcx_jzy
* @date 2019/9/5
*/
class CheckDuplicateRun {
static void check() {
try {
Class appClass = JpomApplication.getAppClass();
String pid = String.valueOf(JpomManifest.getInstance().getPid());
List<MonitoredVm> monitoredVms = JvmUtil.listMainClass(appClass.getName());
monitoredVms.forEach(monitoredVm -> {
VmIdentifier vmIdentifier = monitoredVm.getVmIdentifier();
if (pid.equals(vmIdentifier.getUserInfo())) {
return;
}
DefaultSystemLog.LOG().info("Jpom 程序建议一个机器上只运行一个对应的程序:" + JpomApplication.getAppType() + " pid:" + vmIdentifier.getUserInfo());
});
} catch (Exception e) {
DefaultSystemLog.ERROR().error("检查异常", e);
}
}
}

View File

@ -5,19 +5,12 @@ import cn.hutool.core.util.ClassUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.jiangzeyin.common.PreLoadClass;
import cn.jiangzeyin.common.PreLoadMethod;
import io.jpom.JpomApplication;
import io.jpom.common.JpomManifest;
import io.jpom.system.ConfigBean;
import io.jpom.system.ExtConfigBean;
import io.jpom.util.JvmUtil;
import sun.jvmstat.monitor.MonitorException;
import sun.jvmstat.monitor.MonitoredVm;
import sun.jvmstat.monitor.VmIdentifier;
import io.jpom.util.StringUtil;
import java.io.File;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
/**
* 数据目录权限检查
@ -33,14 +26,14 @@ public class CheckPath {
private static final String[] CLASS_NAME = new String[]{"sun.jvmstat.monitor.MonitorException", "com.sun.tools.attach.VirtualMachine"};
@PreLoadMethod(2)
@PreLoadMethod(1)
private static void checkToolsJar() {
try {
for (String item : CLASS_NAME) {
ClassUtil.loadClass(item, false);
}
} catch (Exception e) {
File file = JvmUtil.getToolsJar();
File file = StringUtil.getToolsJar();
if (file.exists() && file.isFile()) {
DefaultSystemLog.ERROR().error("Jpom未能正常加载tools.jar,请检查当前系统环境变量是否配置JAVA_HOME或者检查Jpom管理命令是否正确", e);
} else {
@ -55,21 +48,7 @@ public class CheckPath {
*/
@PreLoadMethod(2)
private static void checkDuplicateRun() {
Class appClass = JpomApplication.getAppClass();
List<MonitoredVm> monitoredVms;
try {
String pid = String.valueOf(JpomManifest.getInstance().getPid());
monitoredVms = JvmUtil.listMainClass(appClass.getName());
monitoredVms.forEach(monitoredVm -> {
VmIdentifier vmIdentifier = monitoredVm.getVmIdentifier();
if (pid.equals(vmIdentifier.getUserInfo())) {
return;
}
DefaultSystemLog.LOG().info("Jpom 程序建议一个机器上只运行一个对应的程序:" + JpomApplication.getAppType());
});
} catch (MonitorException | URISyntaxException ignored) {
}
CheckDuplicateRun.check();
}
@PreLoadMethod(3)

View File

@ -25,6 +25,9 @@ import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
/**
* jvm jmx 工具
@ -285,16 +288,6 @@ public class JvmUtil {
throw new JpomRuntimeException("JDK中" + file.getAbsolutePath() + " 文件不存在");
}
/**
* 获取jdk 中的tools jar文件路径
*
* @return file
*/
public static File getToolsJar() {
File file = new File(SystemUtil.getJavaRuntimeInfo().getHomeDir());
return new File(file.getParentFile(), "lib/tools.jar");
}
/**
* 工具指定的 mainClass 获取对应所有的的 MonitoredVm对象
*
@ -313,11 +306,28 @@ public class JvmUtil {
MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + process));
// 获取类名
String processName = MonitoredVmUtil.mainClass(vm, true);
if (!mainClass.equals(processName)) {
continue;
if (mainClass.equals(processName) || checkFile(processName, mainClass)) {
monitoredVms.add(vm);
}
monitoredVms.add(vm);
}
return monitoredVms;
}
private static boolean checkFile(String fileName, String mainClass) {
try {
File file = FileUtil.file(fileName);
if (!file.exists() || file.isDirectory()) {
return false;
}
try (JarFile jarFile1 = new JarFile(file)) {
Manifest manifest = jarFile1.getManifest();
Attributes attributes = manifest.getMainAttributes();
String jarMainClass = attributes.getValue(Attributes.Name.MAIN_CLASS);
String jarStartClass = attributes.getValue("Start-Class");
return StrUtil.equals(mainClass, jarMainClass) || StrUtil.equals(mainClass, jarStartClass);
}
} catch (Exception e) {
return false;
}
}
}

View File

@ -3,6 +3,7 @@ package io.jpom.util;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import cn.hutool.system.SystemUtil;
import java.io.File;
@ -77,4 +78,14 @@ public class StringUtil {
}
return path;
}
/**
* 获取jdk 中的tools jar文件路径
*
* @return file
*/
public static File getToolsJar() {
File file = new File(SystemUtil.getJavaRuntimeInfo().getHomeDir());
return new File(file.getParentFile(), "lib/tools.jar");
}
}

View File

@ -17,20 +17,20 @@ public class ContiPerTest {
@Rule
public ContiPerfRule i = new ContiPerfRule();
@Test
// @Test
@PerfTest(invocations = 200000000, threads = 16)
public void test1() throws Exception {
IdUtil.fastSimpleUUID();
}
@Test
// @Test
@PerfTest(invocations = 200000000, threads = 16)
public void test2() throws Exception {
UUID.randomUUID().toString();
}
@Test
// @Test
@PerfTest(invocations = 20000, threads = 16)
public void testHttp() {
HttpUtil.createGet("https://baidu.com")

View File

@ -5,13 +5,13 @@
<parent>
<artifactId>jpom-parent</artifactId>
<groupId>io.jpom</groupId>
<version>2.4.4</version>
<version>2.4.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>Jpom 服务端</name>
<artifactId>server</artifactId>
<version>2.4.4</version>
<version>2.4.5</version>
<properties>
<start-class>io.jpom.JpomServerApplication</start-class>
</properties>

View File

@ -16,7 +16,7 @@ import org.springframework.boot.web.servlet.ServletComponentScan;
* @author jiangzeyin
* @date 2017/9/14
*/
@SpringBootApplication(scanBasePackages = {"io.jpom"})
@SpringBootApplication
@ServletComponentScan
@EnableCommonBoot
public class JpomServerApplication implements ApplicationEventLoad {

View File

@ -29,10 +29,9 @@ public class NodeInfoController extends AbstractController {
* @param model 节点对象
* @param type 操作类型
* @return json
* @throws Exception 异常
*/
@RequestMapping(value = ServerOpenApi.UPDATE_NODE_INFO, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String update(NodeModel model, String type) throws Exception {
public String update(NodeModel model, String type) {
if ("add".equalsIgnoreCase(type)) {
return nodeService.addNode(model, getRequest());
} else {

View File

@ -101,7 +101,7 @@ public class NodeService extends BaseOperService<NodeModel> implements BaseDynam
return JsonMessage.getString(200, "操作成功");
}
public String updateNode(NodeModel nodeModel) throws Exception {
public String updateNode(NodeModel nodeModel) {
NodeModel exit = getItem(nodeModel.getId());
if (exit == null) {
return JsonMessage.getString(405, "节点不存在");

View File

@ -21,7 +21,7 @@
一款简而轻的低侵入式在线构建、自动部署、日常运维、项目监控软件
</description>
<inceptionYear>2017</inceptionYear>
<version>2.4.4</version>
<version>2.4.5</version>
<url>https://gitee.com/keepbx/Jpom</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>