指令执行逻辑增强

master v1.0-SNAPSHOT
dai_48k 2025-04-07 13:43:35 +08:00
parent ee0367ae68
commit d9b2ef2e39
10 changed files with 210 additions and 26 deletions

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.yaml:snakeyaml:1.30">
<CLASSES>
<root url="jar://$PROJECT_DIR$/../../../basic/Maven/maven_repository/org/yaml/snakeyaml/1.30/snakeyaml-1.30.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$PROJECT_DIR$/../../../basic/Maven/maven_repository/org/yaml/snakeyaml/1.30/snakeyaml-1.30-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$PROJECT_DIR$/../../../basic/Maven/maven_repository/org/yaml/snakeyaml/1.30/snakeyaml-1.30-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -7,6 +7,7 @@ import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
@ -30,6 +31,16 @@ public abstract class ProcessAppTemplate implements MiniApp, ActionListener {
*/
public abstract String getCmdStr();
/**
*
*
* null
* @return
*/
public String getRunPath() {
return null;
}
@Override
public JPanel getPanel() {
JPanel panel = new JPanel();
@ -96,7 +107,17 @@ public abstract class ProcessAppTemplate implements MiniApp, ActionListener {
return;
}
try {
process = Runtime.getRuntime().exec(getCmdStr());
File file = null;
if (getRunPath() != null && !getRunPath().equals("")) {
file = new File(getRunPath());
if (!file.exists()) {
failWrite("未找到启动路径:" + getRunPath());
return;
}
}
process = Runtime.getRuntime().exec(getCmdStr(), null, file);
successWrite("启动成功!");
heartbeatCheck();
String tmp;

View File

@ -13,6 +13,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="BKPG_PROTOCAL" />
<orderEntry type="module" module-name="BKPG_PROTOCAL" scope="PROVIDED" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.30" level="project" />
</component>
</module>

View File

@ -20,6 +20,13 @@
<groupId>top.dreamcenter.bkpg</groupId>
<artifactId>BKPG_PROTOCAL</artifactId>
<version>${revision}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.30</version>
</dependency>
</dependencies>
@ -27,10 +34,23 @@
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<outputDirectory>${project.parent.basedir}/ext</outputDirectory>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

View File

@ -1,12 +1,13 @@
package top.dreamcenter.bkpg.group;
import top.dreamcenter.bkpg.group.bean.ProcessConfigBean;
import top.dreamcenter.bkpg.group.util.YamlUtil;
import top.dreamcenter.bkpg.protocal.MiniApp;
import top.dreamcenter.bkpg.protocal.MiniAppGroup;
import top.dreamcenter.bkpg.protocal.template.ProcessAppTemplate;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
import java.util.List;
@ -14,36 +15,65 @@ public class ProcessGroupApp implements MiniAppGroup {
@Override
public List<MiniApp> getMiniApps() {
ProcessConfigBean processConfig = null;
List<MiniApp> list = new LinkedList<>();
try {
processConfig = YamlUtil.getProcessConfig("ext/ProgressGroup.yml");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("ext/ProgressGroup.txt")))) {
String line;
while ((line = reader.readLine()) != null) {
String[] split = line.split(": ");
String appName = split[0];
String appCmd = split[1];
// 从配置装载命令
for (ProcessConfigBean.ProcessBean bean : processConfig.getProcessBeanList()){
MiniApp miniApp = new ProcessAppTemplate() {
@Override
public String getCmdStr() {
return appCmd;
return bean.getCmd();
}
@Override
public String getName() {
return appName;
return bean.getName();
}
@Override
public String getRunPath() {
return bean.getPath();
}
};
list.add(miniApp);
}
} catch (Exception e){
System.err.println(e.getMessage());
return list;
} catch (Exception e) {
// 异常则显示异常面板
MiniApp miniApp = new MiniApp() {
@Override
public String getName() {
return "ProcessGroupApp";
}
@Override
public JPanel getPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JTextArea textArea = new JTextArea();
textArea.setText(e.getMessage());
textArea.setForeground(Color.RED);
System.out.println(e.getMessage());
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setRows(15);
panel.add(scrollPane);
return panel;
}
@Override
public boolean protect() {
return false;
}
};
list.add(miniApp);
}
return list;

View File

@ -0,0 +1,58 @@
package top.dreamcenter.bkpg.group.bean;
import java.util.List;
public class ProcessConfigBean {
private List<ProcessBean> processBeanList;
public List<ProcessBean> getProcessBeanList() {
return processBeanList;
}
public void setProcessBeanList(List<ProcessBean> processBeanList) {
this.processBeanList = processBeanList;
}
public static class ProcessBean{
/**
*
*/
private String name;
/**
*
*/
private String cmd;
/**
*
*/
private String path;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCmd() {
return cmd;
}
public void setCmd(String cmd) {
this.cmd = cmd;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
}

View File

@ -0,0 +1,22 @@
package top.dreamcenter.bkpg.group.util;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import top.dreamcenter.bkpg.group.bean.ProcessConfigBean;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class YamlUtil {
public static ProcessConfigBean getProcessConfig(String path) throws IOException {
Yaml yaml = new Yaml(new Constructor(ProcessConfigBean.class));
try (InputStream in = new FileInputStream(path)) {
return yaml.load(in);
}
}
}

View File

@ -50,6 +50,7 @@ MiniAppGroup
ProcessAppTemplate: MiniApp
- String getName(); // 获取应用名称
- String getCmdStr(); // 需要执行的命令
- String getRunPath(); // 默认实现返回null表示指令执行目录环境
- 其余基类函数已默认实现
@ -87,13 +88,25 @@ protect() 是保护函数如果返回true则表示在BKPG进行RELOAD时
用于执行命令行指令需要实现两个接口一个是getName作用同上另外一个是该抽象类
独有的getCmdStr需要设置命令行的完整执行指令。当点击面板的执行按钮时
会启动一个线程来执行该命令并且每5s会检测一次命令心跳如果心跳失去连接
则会释放资源。可以作为一些应用或者服务的启动端。
则会释放资源。
如果说有的指令需要在特定目录下执行可以覆盖getRunPath来指定运行环境。
可以作为一些应用或者服务的启动端。
为了更加高效的添加命令(常用),
官方内置了一个命令AppGroup: **PLUGIN_BKPG_GROUP_PROCESS**
通过配置ext/ProgressGroup.txt来添加指令应用
指令名称和指令用英文冒号和一个空格隔开【: 】会自动创建多组ProcessAppTemplate。
通过配置ext/ProgressGroup.yml来添加指令应用
会自动创建多组ProcessAppTemplate。格式参考
```yml
processBeanList:
- name: PING百度
cmd: ping www.baidu.com
- name: PING主站
cmd: ping www.dreamcenter.top
- name: nginx
cmd: D:\basic\nginx-1.24.0\nginx
path: D:\basic\nginx-1.24.0
```

View File

@ -1,2 +0,0 @@
PING百度: ping www.baidu.com
PING主站: ping www.dreamcenter.top

View File

@ -0,0 +1,8 @@
processBeanList:
- name: PING百度
cmd: ping www.baidu.com
- name: PING主站
cmd: ping www.dreamcenter.top
- name: nginx
cmd: D:\basic\nginx-1.24.0\nginx
path: D:\basic\nginx-1.24.0