|
6 | 6 | import java.text.SimpleDateFormat; |
7 | 7 | import java.util.ArrayList; |
8 | 8 | import java.util.Date; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Collections; |
9 | 11 |
|
10 | 12 | public class CommonRails |
11 | 13 | { |
@@ -124,6 +126,70 @@ public static Boolean isSocketClosed(Socket socket) |
124 | 126 | } |
125 | 127 | } |
126 | 128 |
|
| 129 | + // Registry for started Processes so CommonRails can monitor them and print on exit |
| 130 | + protected static final List<Process> REGISTERED_PROCESSES = Collections.synchronizedList(new ArrayList<Process>()); |
| 131 | + |
| 132 | + /** |
| 133 | + * Register a started Process with CommonRails. CommonRails will attach a listener |
| 134 | + * to the process' onExit CompletableFuture (Java 9+) and print when the process exits. |
| 135 | + * If onExit is unavailable/throws, a watcher thread using waitFor is started as a fallback. |
| 136 | + */ |
| 137 | + public static synchronized void registerProcess(ProcessBuilder pb, Process process, Object owner) |
| 138 | + { |
| 139 | + if (process == null) return; |
| 140 | + |
| 141 | + REGISTERED_PROCESSES.add(process); |
| 142 | + |
| 143 | + Object printer = (owner == null) ? CommonRails.class : owner; |
| 144 | + |
| 145 | + try |
| 146 | + { |
| 147 | + CommonRails.printSystemComponent(printer, process.hashCode(), ". CommonRails::registerProcess >> registered process: " + process); |
| 148 | + |
| 149 | + // Attach onExit listener |
| 150 | + process.onExit().thenAccept(p -> { |
| 151 | + try |
| 152 | + { |
| 153 | + CommonRails.printSystemComponent(printer, p.hashCode(), ". CommonRails::processExited >> process closed: " + p + " exit=" + p.exitValue()); |
| 154 | + } |
| 155 | + catch (Throwable t) |
| 156 | + { |
| 157 | + // Best-effort printing |
| 158 | + CommonRails.printSystemComponent(printer, p.hashCode(), ". CommonRails::processExited >> process closed: " + p); |
| 159 | + } |
| 160 | + finally |
| 161 | + { |
| 162 | + REGISTERED_PROCESSES.remove(p); |
| 163 | + } |
| 164 | + }); |
| 165 | + } |
| 166 | + catch (Throwable t) |
| 167 | + { |
| 168 | + // Fallback: spawn a watcher thread that waits for the process |
| 169 | + new Thread(() -> { |
| 170 | + try |
| 171 | + { |
| 172 | + int rv = process.waitFor(); |
| 173 | + |
| 174 | + CommonRails.printSystemComponent(printer, process.hashCode(), ". CommonRails::processExited(watcher) >> process closed: " + process + " exit=" + rv); |
| 175 | + } |
| 176 | + catch (Exception e) |
| 177 | + { |
| 178 | + e.printStackTrace(System.err); |
| 179 | + } |
| 180 | + finally |
| 181 | + { |
| 182 | + REGISTERED_PROCESSES.remove(process); |
| 183 | + } |
| 184 | + }, "CommonRails-ProcessWatcher-" + process.hashCode()).start(); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + public static synchronized List<Process> getRegisteredProcesses() |
| 189 | + { |
| 190 | + return new ArrayList<>(REGISTERED_PROCESSES); |
| 191 | + } |
| 192 | + |
127 | 193 | public static class TelnetCallOnComplete implements Runnable |
128 | 194 | { |
129 | 195 | protected WebExpress web_express; |
|
0 commit comments