|
| 1 | +package net.b07z.sepia.server.core.tools; |
| 2 | + |
| 3 | +import java.nio.charset.Charset; |
| 4 | +import java.nio.charset.StandardCharsets; |
| 5 | +import java.util.List; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | +import java.util.stream.Stream; |
| 8 | + |
| 9 | +/** |
| 10 | + * Class to help with runtime executions. |
| 11 | + * |
| 12 | + * @author Florian Quirin |
| 13 | + * |
| 14 | + */ |
| 15 | +public class RuntimeInterface { |
| 16 | + |
| 17 | + public static final boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows"); |
| 18 | + public static Charset windowsShellCodepage = null; |
| 19 | + |
| 20 | + /** |
| 21 | + * Hold result of a runtime command. |
| 22 | + */ |
| 23 | + public static class RuntimeResult { |
| 24 | + private int statusCode; |
| 25 | + private List<String> output; |
| 26 | + private Exception exception; |
| 27 | + |
| 28 | + /** |
| 29 | + * Create new result with statusCode (kind of exit code) and output string. |
| 30 | + * @param statusCode - 0: all good, 1: general error, 2: tbd, 3: timeout |
| 31 | + * @param output - list of lines returned by execution (in correct encoding hopefully) or null |
| 32 | + * @param exception - exception catched if any (or null) |
| 33 | + */ |
| 34 | + public RuntimeResult(int statusCode, List<String> output, Exception exception){ |
| 35 | + this.statusCode = statusCode; |
| 36 | + this.output = output; |
| 37 | + this.exception = exception; |
| 38 | + } |
| 39 | + /** |
| 40 | + * Return list of lines. |
| 41 | + */ |
| 42 | + public List<String> getOutput(){ |
| 43 | + return output; |
| 44 | + } |
| 45 | + /** |
| 46 | + * Return status code - 0: all good, 1: general error, 2: tbd, 3: timeout |
| 47 | + */ |
| 48 | + public int getStatusCode(){ |
| 49 | + return statusCode; |
| 50 | + } |
| 51 | + public Exception getException(){ |
| 52 | + return exception; |
| 53 | + } |
| 54 | + @Override |
| 55 | + public String toString(){ |
| 56 | + return toString(System.lineSeparator()); |
| 57 | + } |
| 58 | + /** |
| 59 | + * Return output as string. If possible (not null or empty) join lines separated by 'delimiter'. |
| 60 | + * @param delimiter |
| 61 | + * @return |
| 62 | + */ |
| 63 | + public String toString(CharSequence delimiter){ |
| 64 | + if (output == null){ |
| 65 | + return null; |
| 66 | + }else if (output.isEmpty()){ |
| 67 | + return ""; |
| 68 | + }else{ |
| 69 | + return String.join(delimiter, output); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * We need to run the shell codepage command to get proper windows encoding (is there REALLY no BETTER WAY??). |
| 76 | + * @return |
| 77 | + */ |
| 78 | + public static RuntimeResult getWindowsShellCodepage(){ |
| 79 | + RuntimeResult rtr = runCommand(new String[]{"chcp"}, 3000); |
| 80 | + List<String> out = rtr.getOutput(); |
| 81 | + if (rtr.getStatusCode() == 0 && Is.notNullOrEmpty(out)){ |
| 82 | + Debugger.println("RuntimeInterface - Trying to get Windows shell encoding...", 3); |
| 83 | + String joinedOut = rtr.toString().replaceAll("\\n|\\r", " "); |
| 84 | + if (joinedOut.matches(".*\\d+.*")){ |
| 85 | + String encoding = joinedOut.replaceAll(".*?([0-9]+).*", "$1"); |
| 86 | + windowsShellCodepage = Charset.forName("cp" + encoding); |
| 87 | + Debugger.println("RuntimeInterface - Windows shell encoding: " + windowsShellCodepage.toString(), 3); |
| 88 | + }else{ |
| 89 | + return new RuntimeResult(1, null, new RuntimeException("Could not get Windows shell encoding (codepage)!")); |
| 90 | + } |
| 91 | + }else{ |
| 92 | + return new RuntimeResult(1, null, new RuntimeException("Could not get Windows shell encoding (codepage)!")); |
| 93 | + } |
| 94 | + return rtr; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Execute runtime command. Chooses between "cmd.exe" and "sh" shell by OS. |
| 99 | + * @param command - e.g.: 'new String[]{"ping", "-c", "3", "sepia-framework.github.io"}' |
| 100 | + * @return |
| 101 | + */ |
| 102 | + public static RuntimeResult runCommand(String[] command){ |
| 103 | + return runCommand(command, 5000); |
| 104 | + } |
| 105 | + public static RuntimeResult runCommand(String[] command, long customTimeout){ |
| 106 | + Charset encoding = StandardCharsets.UTF_8; |
| 107 | + if (isWindows && windowsShellCodepage == null && !command[0].equals("chcp")){ |
| 108 | + RuntimeResult rtr = getWindowsShellCodepage(); |
| 109 | + if (rtr.getStatusCode() != 0){ |
| 110 | + return new RuntimeResult(1, null, rtr.getException()); |
| 111 | + }else{ |
| 112 | + encoding = windowsShellCodepage; |
| 113 | + } |
| 114 | + }else if (windowsShellCodepage != null){ |
| 115 | + encoding = windowsShellCodepage; |
| 116 | + } |
| 117 | + if (customTimeout > 15000){ |
| 118 | + customTimeout = 15000; |
| 119 | + } |
| 120 | + Process process; |
| 121 | + try{ |
| 122 | + //process = Runtime.getRuntime().exec(command); //old way |
| 123 | + ProcessBuilder builder = new ProcessBuilder(); |
| 124 | + String[] osPart; |
| 125 | + if (isWindows){ |
| 126 | + osPart = new String[]{"cmd.exe", "/c"}; |
| 127 | + }else{ |
| 128 | + osPart = new String[]{"sh", "-c"}; |
| 129 | + } |
| 130 | + builder.command(Stream.of(osPart, command).flatMap(Stream::of).toArray(String[]::new)); //tricky way to concatenate arrays |
| 131 | + //builder.directory(new File(System.getProperty("user.home"))); //set process directory or other options ... |
| 132 | + process = builder.start(); |
| 133 | + |
| 134 | + //wait for finish or timeout |
| 135 | + if(!process.waitFor(customTimeout, TimeUnit.MILLISECONDS)){ |
| 136 | + //timeout - kill the process. |
| 137 | + process.destroy(); // consider using destroyForcibly instead |
| 138 | + return new RuntimeResult(3, null, new RuntimeException("Timeout!")); |
| 139 | + } |
| 140 | + }catch (Exception e){ |
| 141 | + return new RuntimeResult(1, null, e); |
| 142 | + } |
| 143 | + List<String> output = null; |
| 144 | + try{ |
| 145 | + output = FilesAndStreams.getLinesFromStream(process.getInputStream(), encoding); |
| 146 | + return new RuntimeResult(process.exitValue(), output, null); |
| 147 | + }catch(Exception e){ |
| 148 | + return new RuntimeResult(1, output, e); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments