|
1 |
| -package net.lecousin.framework.util; |
2 |
| - |
3 |
| -import java.io.InputStream; |
4 |
| -import java.nio.charset.StandardCharsets; |
5 |
| -import java.util.concurrent.ThreadFactory; |
6 |
| - |
7 |
| -import net.lecousin.framework.application.Application; |
8 |
| -import net.lecousin.framework.application.LCCore; |
9 |
| -import net.lecousin.framework.event.Listener; |
10 |
| -import net.lecousin.framework.mutable.Mutable; |
11 |
| - |
12 |
| -/** |
13 |
| - * Utility methods with Process. |
14 |
| - */ |
15 |
| -public class ProcessUtil { |
16 |
| - |
17 |
| - /** |
18 |
| - * Create a thread that wait for the given process to end, and call the given listener. |
19 |
| - */ |
20 |
| - public static void onProcessExited(Process process, Listener<Integer> exitValueListener) { |
21 |
| - Application app = LCCore.getApplication(); |
22 |
| - Mutable<Thread> mt = new Mutable<>(null); |
23 |
| - Thread t = app.getThreadFactory().newThread(() -> { |
24 |
| - try { exitValueListener.fire(Integer.valueOf(process.waitFor())); } |
25 |
| - catch (InterruptedException e) { /* ignore and quit */ } |
26 |
| - app.interrupted(mt.get()); |
27 |
| - }); |
28 |
| - mt.set(t); |
29 |
| - t.setName("Waiting for process to exit"); |
30 |
| - t.start(); |
31 |
| - app.toInterruptOnShutdown(t); |
32 |
| - } |
33 |
| - |
34 |
| - /** Launch 2 threads to consume both output and error streams, and call the listeners for each line read. */ |
35 |
| - public static void consumeProcessConsole(Process process, Listener<String> outputListener, Listener<String> errorListener) { |
36 |
| - Application app = LCCore.getApplication(); |
37 |
| - ThreadFactory factory = app.getThreadFactory(); |
38 |
| - Thread t; |
39 |
| - ConsoleConsumer cc; |
40 |
| - cc = new ConsoleConsumer(process.getInputStream(), outputListener); |
41 |
| - t = factory.newThread(cc); |
42 |
| - t.setName("Process output console consumer"); |
43 |
| - cc.app = app; |
44 |
| - cc.t = t; |
45 |
| - t.start(); |
46 |
| - app.toInterruptOnShutdown(t); |
47 |
| - cc = new ConsoleConsumer(process.getErrorStream(), errorListener); |
48 |
| - t = factory.newThread(cc); |
49 |
| - t.setName("Process error console consumer"); |
50 |
| - cc.app = app; |
51 |
| - cc.t = t; |
52 |
| - t.start(); |
53 |
| - app.toInterruptOnShutdown(t); |
54 |
| - } |
55 |
| - |
56 |
| - /** Thread that read from the given stream and call the listener for each line. */ |
57 |
| - public static class ConsoleConsumer implements Runnable { |
58 |
| - |
59 |
| - /** Constructor. */ |
60 |
| - public ConsoleConsumer(InputStream input, Listener<String> listener) { |
61 |
| - this.input = input; |
62 |
| - this.listener = listener; |
63 |
| - } |
64 |
| - |
65 |
| - private InputStream input; |
66 |
| - private Listener<String> listener; |
67 |
| - private Application app; |
68 |
| - private Thread t; |
69 |
| - |
70 |
| - @Override |
71 |
| - public void run() { |
72 |
| - StringBuilder line = new StringBuilder(); |
73 |
| - byte[] buffer = new byte[1024]; |
74 |
| - do { |
75 |
| - int nb; |
76 |
| - try { nb = input.read(buffer); } |
77 |
| - catch (Exception e) { break; } |
78 |
| - if (nb < 0) break; |
79 |
| - if (nb == 0) continue; |
80 |
| - String s = new String(buffer, 0, nb, StandardCharsets.ISO_8859_1); |
81 |
| - while (s.length() > 0) { |
82 |
| - int i = s.indexOf('\r'); |
83 |
| - int j = s.indexOf('\n'); |
84 |
| - if (i < 0 && j < 0) { |
85 |
| - line.append(s); |
86 |
| - break; |
87 |
| - } else if (i < 0) { |
88 |
| - line.append(s.substring(0,j)); |
89 |
| - listener.fire(line.toString()); |
90 |
| - line = new StringBuilder(); |
91 |
| - s = s.substring(j + 1); |
92 |
| - } else if (j < 0) { |
93 |
| - line.append(s.substring(0,i)); |
94 |
| - listener.fire(line.toString()); |
95 |
| - line = new StringBuilder(); |
96 |
| - s = s.substring(i + 1); |
97 |
| - } else if (i == j - 1) { |
98 |
| - line.append(s.substring(0,i)); |
99 |
| - listener.fire(line.toString()); |
100 |
| - line = new StringBuilder(); |
101 |
| - s = s.substring(j + 1); |
102 |
| - } else { |
103 |
| - if (j < i) i = j; |
104 |
| - line.append(s.substring(0,i)); |
105 |
| - listener.fire(line.toString()); |
106 |
| - line = new StringBuilder(); |
107 |
| - s = s.substring(i + 1); |
108 |
| - } |
109 |
| - } |
110 |
| - } while (true); |
111 |
| - if (line.length() > 0) |
112 |
| - listener.fire(line.toString()); |
113 |
| - try { input.close(); } catch (Throwable t) { /* ignore */ } |
114 |
| - app.interrupted(t); |
115 |
| - } |
116 |
| - } |
117 |
| - |
118 |
| -} |
| 1 | +package net.lecousin.framework.util; |
| 2 | + |
| 3 | +import java.io.InputStream; |
| 4 | +import java.nio.charset.StandardCharsets; |
| 5 | +import java.util.concurrent.ThreadFactory; |
| 6 | + |
| 7 | +import net.lecousin.framework.application.Application; |
| 8 | +import net.lecousin.framework.application.LCCore; |
| 9 | +import net.lecousin.framework.event.Listener; |
| 10 | +import net.lecousin.framework.mutable.Mutable; |
| 11 | + |
| 12 | +/** |
| 13 | + * Utility methods with Process. |
| 14 | + */ |
| 15 | +public final class ProcessUtil { |
| 16 | + |
| 17 | + private ProcessUtil() { /* no instance */ } |
| 18 | + |
| 19 | + /** |
| 20 | + * Create a thread that wait for the given process to end, and call the given listener. |
| 21 | + */ |
| 22 | + public static void onProcessExited(Process process, Listener<Integer> exitValueListener) { |
| 23 | + Application app = LCCore.getApplication(); |
| 24 | + Mutable<Thread> mt = new Mutable<>(null); |
| 25 | + Thread t = app.getThreadFactory().newThread(() -> { |
| 26 | + try { exitValueListener.fire(Integer.valueOf(process.waitFor())); } |
| 27 | + catch (InterruptedException e) { /* ignore and quit */ } |
| 28 | + app.interrupted(mt.get()); |
| 29 | + }); |
| 30 | + mt.set(t); |
| 31 | + t.setName("Waiting for process to exit"); |
| 32 | + t.start(); |
| 33 | + app.toInterruptOnShutdown(t); |
| 34 | + } |
| 35 | + |
| 36 | + /** Launch 2 threads to consume both output and error streams, and call the listeners for each line read. */ |
| 37 | + public static void consumeProcessConsole(Process process, Listener<String> outputListener, Listener<String> errorListener) { |
| 38 | + Application app = LCCore.getApplication(); |
| 39 | + ThreadFactory factory = app.getThreadFactory(); |
| 40 | + Thread t; |
| 41 | + ConsoleConsumer cc; |
| 42 | + cc = new ConsoleConsumer(process.getInputStream(), outputListener); |
| 43 | + t = factory.newThread(cc); |
| 44 | + t.setName("Process output console consumer"); |
| 45 | + cc.app = app; |
| 46 | + cc.t = t; |
| 47 | + t.start(); |
| 48 | + app.toInterruptOnShutdown(t); |
| 49 | + cc = new ConsoleConsumer(process.getErrorStream(), errorListener); |
| 50 | + t = factory.newThread(cc); |
| 51 | + t.setName("Process error console consumer"); |
| 52 | + cc.app = app; |
| 53 | + cc.t = t; |
| 54 | + t.start(); |
| 55 | + app.toInterruptOnShutdown(t); |
| 56 | + } |
| 57 | + |
| 58 | + /** Thread that read from the given stream and call the listener for each line. */ |
| 59 | + public static class ConsoleConsumer implements Runnable { |
| 60 | + |
| 61 | + /** Constructor. */ |
| 62 | + public ConsoleConsumer(InputStream input, Listener<String> listener) { |
| 63 | + this.input = input; |
| 64 | + this.listener = listener; |
| 65 | + } |
| 66 | + |
| 67 | + private InputStream input; |
| 68 | + private Listener<String> listener; |
| 69 | + private Application app; |
| 70 | + private Thread t; |
| 71 | + |
| 72 | + @Override |
| 73 | + public void run() { |
| 74 | + StringBuilder line = new StringBuilder(); |
| 75 | + byte[] buffer = new byte[1024]; |
| 76 | + do { |
| 77 | + int nb; |
| 78 | + try { nb = input.read(buffer); } |
| 79 | + catch (Exception e) { break; } |
| 80 | + if (nb < 0) break; |
| 81 | + if (nb == 0) continue; |
| 82 | + String s = new String(buffer, 0, nb, StandardCharsets.ISO_8859_1); |
| 83 | + while (s.length() > 0) { |
| 84 | + int i = s.indexOf('\r'); |
| 85 | + int j = s.indexOf('\n'); |
| 86 | + if (i < 0 && j < 0) { |
| 87 | + line.append(s); |
| 88 | + break; |
| 89 | + } else if (i < 0) { |
| 90 | + line.append(s.substring(0,j)); |
| 91 | + listener.fire(line.toString()); |
| 92 | + line = new StringBuilder(); |
| 93 | + s = s.substring(j + 1); |
| 94 | + } else if (j < 0) { |
| 95 | + line.append(s.substring(0,i)); |
| 96 | + listener.fire(line.toString()); |
| 97 | + line = new StringBuilder(); |
| 98 | + s = s.substring(i + 1); |
| 99 | + } else if (i == j - 1) { |
| 100 | + line.append(s.substring(0,i)); |
| 101 | + listener.fire(line.toString()); |
| 102 | + line = new StringBuilder(); |
| 103 | + s = s.substring(j + 1); |
| 104 | + } else { |
| 105 | + if (j < i) i = j; |
| 106 | + line.append(s.substring(0,i)); |
| 107 | + listener.fire(line.toString()); |
| 108 | + line = new StringBuilder(); |
| 109 | + s = s.substring(i + 1); |
| 110 | + } |
| 111 | + } |
| 112 | + } while (true); |
| 113 | + if (line.length() > 0) |
| 114 | + listener.fire(line.toString()); |
| 115 | + try { input.close(); } catch (Throwable t) { /* ignore */ } |
| 116 | + app.interrupted(t); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments