Debian Codegraph @

src/main/java/com/carrotsearch/procfork/ProcessBuilderLauncher.java

raw symbols SCIP
1 /*
2 * procfork
3 *
4 * Copyright (C) 2019, Dawid Weiss.
5 * All rights reserved.
6 */
7 package com.carrotsearch.procfork;
8
9 import java.io.IOException;
10 import java.nio.file.Files;
11 import java.nio.file.Path;
12 import java.nio.file.Paths;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.LinkedHashMap;
16 import java.util.List;
17 import java.util.Locale;
18 import java.util.Map;
19
20 public class ProcessBuilderLauncher implements Launcher {
21 private Path executable;
22 private Path cwd;
23 private List<String> args = new ArrayList<>();
24 private Map<String, String> envVars = new LinkedHashMap<>();
25 private boolean executeViaShell;
26
27 private Path input;
28
29 @Override
30 public Launcher args(String... values) {
31 for (String v : values) {
32 arg(v);
33 }
34 return this;
35 }
36
37 @Override
38 public Launcher arg(String value) {
39 args.add(value);
40 return this;
41 }
42
43 @Override
44 public Launcher executable(Path executable) {
45 this.executable = executable;
46 return this;
47 }
48
49 @Override
50 public Launcher viaShellLauncher() {
51 this.executeViaShell = true;
52 return this;
53 }
54
55 @Override
56 public Launcher cwd(Path cwd) {
57 this.cwd = cwd;
58 return this;
59 }
60
61 @Override
62 public Launcher envvar(String key, String value) {
63 assert key != null && value != null;
64
65 envVars.put(key, value);
66 return this;
67 }
68
69 @Override
70 public Launcher input(Path input) {
71 this.input = input;
72 return this;
73 }
74
75 @Override
76 public ForkedProcess execute() throws IOException {
77 ProcessBuilder pb = new ProcessBuilder();
78 if (cwd != null) {
79 pb.directory(cwd.toFile());
80 }
81
82 List<String> command;
83 if (executeViaShell) {
84 if (LocalEnvironment.IS_OS_WINDOWS) {
85 command = shellInvokeWindows();
86 } else if (LocalEnvironment.IS_OS_UNIXISH) {
87 command = shellInvokeUnixish();
88 } else {
89 throw new RuntimeException("Unsupported operating system: " + LocalEnvironment.OS_NAME);
90 }
91 } else {
92 command = new ArrayList<>();
93 command.add(executableName());
94 command.addAll(args);
95 }
96
97 pb.command(command);
98 pb.environment().putAll(envVars);
99
100 if (input != null) {
101 pb.redirectInput(ProcessBuilder.Redirect.from(input.toFile()));
102 } else {
103 pb.redirectInput(ProcessBuilder.Redirect.PIPE);
104 }
105
106 // merge stderr and stdout
107 pb.redirectErrorStream(true);
108
109 Path output = Files.createTempFile("process-", ".out");
110 pb.redirectOutput(ProcessBuilder.Redirect.to(output.toFile()));
111
112 try {
113 Process process = pb.start();
114
115 // If there is no input, close the pipe early.
116 if (input == null) {
117 process.getInputStream().close();
118 }
119
120 return new ForkedProcess(process, output);
121 } catch (IOException e) {
122 try {
123 Files.deleteIfExists(output);
124 } catch (IOException e2) {
125 e.addSuppressed(e2);
126 }
127 throw e;
128 }
129 }
130
131 protected List<String> shellInvokeUnixish() {
132 Path cmd = Paths.get("/bin/sh");
133 if (!Files.isRegularFile(cmd)) {
134 throw new RuntimeException("sh interpreter couldn't be found: " + cmd);
135 }
136
137 List<String> command = new ArrayList<>();
138 command.addAll(
139 Arrays.asList(
140 cmd.toString(),
141 "-f", // don't glob.
142 "-c",
143 "'" + executableName() + "'" + " \"$@\"",
144 executable.getFileName().toString()));
145 command.addAll(args);
146
147 return command;
148 }
149
150 protected List<String> shellInvokeWindows() {
151 List<String> command = new ArrayList<>();
152 Path cmd = null;
153 for (Map.Entry<String, String> e : System.getenv().entrySet()) {
154 if (e.getKey().toLowerCase(Locale.ROOT).equals("comspec")) {
155 cmd = Paths.get(e.getValue());
156 break;
157 }
158 }
159 if (cmd == null || !Files.isRegularFile(cmd)) {
160 throw new RuntimeException(
161 "Command line interpreter couldn't be found or is not a file: " + cmd);
162 }
163
164 command.add(cmd.toString());
165
166 /*
167 * cmd. Hopeless.
168 *
169 * mkdir "foo bar"
170 * echo @echo You said: %1 > "foo bar\test.cmd"
171 * cmd /C "foo bar\test.cmd" hello
172 * cmd /C "foo bar\test.cmd" "good morning"
173 */
174
175 // See https://stackoverflow.com/a/356014
176 command.add("/S");
177 command.add("/C");
178 command.add("\"");
179 command.add(executableName());
180
181 if (args.isEmpty()) {
182 command.add("\"");
183 } else {
184 command.addAll(args.subList(0, args.size() - 1));
185 command.add(args.get(args.size() - 1) + "\"");
186 }
187
188 System.out.println(command);
189
190 return command;
191 }
192
193 private String executableName() {
194 return executable.toString();
195 }
196 }