From c19f49d38a28f6ae92e97a89b88e61d887e0d7c9 Mon Sep 17 00:00:00 2001 From: youys <1272586223@qq.com> Date: Tue, 11 Jul 2023 16:31:04 +0800 Subject: [PATCH] =?UTF-8?q?java=E5=9C=A8=E7=BA=BF=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../educoder/debugger/DebuggerController.java | 41 ++++++++++---- .../net/educoder/debugger/StreamGobbler.java | 1 - .../java/net/educoder/model/ShellResult.java | 35 ++++++++++++ .../java/net/educoder/util/ShellUtil.java | 56 +++++++++++++++++++ src/main/resources/application.yml | 2 +- 5 files changed, 122 insertions(+), 13 deletions(-) create mode 100644 src/main/java/net/educoder/model/ShellResult.java create mode 100644 src/main/java/net/educoder/util/ShellUtil.java diff --git a/src/main/java/net/educoder/debugger/DebuggerController.java b/src/main/java/net/educoder/debugger/DebuggerController.java index 471dcbe..0f41d1b 100644 --- a/src/main/java/net/educoder/debugger/DebuggerController.java +++ b/src/main/java/net/educoder/debugger/DebuggerController.java @@ -2,8 +2,14 @@ package net.educoder.debugger; import com.google.gson.Gson; import net.educoder.model.DebugResult; +import net.educoder.model.ShellResult; +import net.educoder.util.ShellUtil; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -22,16 +28,13 @@ import java.util.Map; @RestController public class DebuggerController { - // public static final String CLASS_PATHS = "/Users/youyongsheng/Desktop/ZQ/online-debug/evaluation/test-java/part4/src:/Users/youyongsheng/Desktop/ZQ/online-debug/evaluation/test-java/part4/src/precompiled.jar"; -// public static final String MAIN_CLASS = "__Driver__"; - public static final String CLASS_PATHS = "/Users/youyongsheng/Desktop/ZQ/online-debug/evaluation/test-java"; - public static final String MAIN_CLASS = "Debug"; - // private static Debugger debugger = new Debugger(MAIN_CLASS, CLASS_PATHS); private static Debugger debugger = null; private static final Gson gson = new Gson(); private String outputPath; + private final Logger logger = LoggerFactory.getLogger(DebuggerController.class); + @GetMapping("/") public String home() { @@ -44,12 +47,16 @@ public class DebuggerController { * @return * @throws Exception */ - @GetMapping("/start-program") - public String startProgram(@RequestParam String classPath, - @RequestParam String mainClass, - @RequestParam String breakPoints, + @PostMapping("/start-program") + public String startProgram(@RequestParam("classPath") String classPath, + @RequestParam("mainClass") String mainClass, + @RequestParam("breakPoints") String breakPoints, @RequestParam("fileInputPath") String fileInputPath, - @RequestParam("fileOutPath") String fileOutPath) throws Exception { + @RequestParam("fileOutPath") String fileOutPath, + @RequestParam("originExecuteFile") String originExecuteFile) throws Exception { + + // 编译代码 + compileCode(originExecuteFile); initDebuggerInstance(mainClass, classPath); outputPath = fileOutPath; @@ -199,7 +206,7 @@ public class DebuggerController { return gson.toJson(result); } -// result.setExpressions(); + result.setExpressions(new HashMap<>()); result.setFilename(debugger.getLocationFilename()); result.setCurrent_line(debugger.getLocationLineNumber()); @@ -232,4 +239,16 @@ public class DebuggerController { } } } + + /** + * 编译代码 + * + * @param originalEvaluateFile 原始评测文件 + */ + private void compileCode(String originalEvaluateFile) { + String compilePath = "/data/workspace" + File.separator + originalEvaluateFile.substring(0, originalEvaluateFile.lastIndexOf("/") + 1); + String compileCommand = StringUtils.join("javac -g -cp ", compilePath, " ", "/data/workspace", File.separator, originalEvaluateFile); + ShellResult shellResult = ShellUtil.executeAndGetExitStatus(compileCommand); + logger.info("编译代码command:{}, status:{}, out:{}", compileCommand, shellResult.getExitStatus(), shellResult.getOut()); + } } diff --git a/src/main/java/net/educoder/debugger/StreamGobbler.java b/src/main/java/net/educoder/debugger/StreamGobbler.java index 819fd92..434e8f2 100644 --- a/src/main/java/net/educoder/debugger/StreamGobbler.java +++ b/src/main/java/net/educoder/debugger/StreamGobbler.java @@ -26,7 +26,6 @@ public class StreamGobbler extends Thread { int i; while ((i = bufferedReader.read()) != -1) { - System.out.println(filepath+ " write---------"+(char)i); fileWriter.write((char)i); fileWriter.flush(); } diff --git a/src/main/java/net/educoder/model/ShellResult.java b/src/main/java/net/educoder/model/ShellResult.java new file mode 100644 index 0000000..7f4ab36 --- /dev/null +++ b/src/main/java/net/educoder/model/ShellResult.java @@ -0,0 +1,35 @@ +package net.educoder.model; + + +/** + * shell执行结果 + * + * @author 威少 + */ +public class ShellResult { + /** + * 退出码 + */ + private Integer exitStatus; + /** + * 实际输出 + */ + private String out; + + + public Integer getExitStatus() { + return exitStatus; + } + + public void setExitStatus(Integer exitStatus) { + this.exitStatus = exitStatus; + } + + public String getOut() { + return out; + } + + public void setOut(String out) { + this.out = out; + } +} diff --git a/src/main/java/net/educoder/util/ShellUtil.java b/src/main/java/net/educoder/util/ShellUtil.java new file mode 100644 index 0000000..9256c4c --- /dev/null +++ b/src/main/java/net/educoder/util/ShellUtil.java @@ -0,0 +1,56 @@ +package net.educoder.util; + +import net.educoder.model.ShellResult; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + + +public final class ShellUtil { + + private static final Logger logger = LoggerFactory.getLogger(ShellUtil.class); + + /** + * 执行shell命令并获取输出 + */ + public static String execute(String command) { + return executeAndGetExitStatus(command).getOut(); + } + + + /** + * 执行命令并获得输出以及退出码 + */ + public static ShellResult executeAndGetExitStatus(String command) { + ShellResult result = new ShellResult(); + + StringBuilder out = new StringBuilder(); + Integer exitStatus = -1; + + ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", command); + pb.redirectErrorStream(true); + try { + Process process = pb.start(); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + while ((line = reader.readLine()) != null) { + out.append(line); + out.append(System.getProperty("line.separator")); + } + exitStatus = process.waitFor(); + + } catch (Exception e) { + logger.error("执行shell出错, command:{}", command, e); + } + + result.setOut(out.toString().trim()); + result.setExitStatus(exitStatus); + logger.debug("execute shell command: {}, out: {}, status: {}", command, out, exitStatus); + + return result; + } + + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b67e63e..a7afc92 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,2 +1,2 @@ server: - port: 8083 + port: 8080