Skip to content

Commit b0bc42c

Browse files
committed
refactor: extract BaseProcessHandler for reusability
1 parent b84c9ef commit b0bc42c

File tree

3 files changed

+102
-84
lines changed

3 files changed

+102
-84
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package net.laprun.sustainability.power.nuprocess;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.InputStream;
5+
import java.nio.ByteBuffer;
6+
import java.util.Arrays;
7+
import java.util.concurrent.CompletableFuture;
8+
import java.util.concurrent.Future;
9+
import java.util.concurrent.TimeUnit;
10+
11+
import com.zaxxer.nuprocess.NuAbstractProcessHandler;
12+
import com.zaxxer.nuprocess.NuProcess;
13+
14+
public class BaseProcessHandler extends NuAbstractProcessHandler {
15+
private String errorMsg;
16+
private NuProcess process;
17+
private final String[] command;
18+
private final GrowableBuffer stdOutBuffer = new GrowableBuffer();
19+
private final CompletableFuture<InputStream> output = new CompletableFuture<>();
20+
21+
public BaseProcessHandler(String... command) {
22+
if (command == null || command.length == 0) {
23+
throw new IllegalArgumentException("No command specified");
24+
}
25+
this.command = initCommand(command);
26+
}
27+
28+
protected String[] initCommand(String... command) {
29+
return command;
30+
}
31+
32+
public String[] command() {
33+
return command;
34+
}
35+
36+
@Override
37+
public void onPreStart(NuProcess nuProcess) {
38+
this.process = nuProcess;
39+
}
40+
41+
@Override
42+
public void onExit(int statusCode) {
43+
if (Integer.MIN_VALUE == statusCode) {
44+
throw new IllegalArgumentException("Unknown command " + Arrays.toString(command));
45+
}
46+
if (statusCode != 0) {
47+
throw new RuntimeException("Couldn't execute command " + Arrays.toString(command)
48+
+ ". Error code: " + statusCode + ", message: " + errorMsg);
49+
}
50+
}
51+
52+
public void stop() {
53+
if (process.isRunning()) {
54+
process.destroy(false);
55+
try {
56+
process.waitFor(5, TimeUnit.SECONDS);
57+
} catch (InterruptedException e) {
58+
throw new RuntimeException(e);
59+
} finally {
60+
process.destroy(true);
61+
}
62+
}
63+
}
64+
65+
@Override
66+
public void onStdout(ByteBuffer buffer, boolean closed) {
67+
if (buffer.hasRemaining()) {
68+
stdOutBuffer.put(buffer);
69+
}
70+
71+
if (closed) {
72+
output.complete(new ByteArrayInputStream(stdOutBuffer.array()));
73+
}
74+
}
75+
76+
@Override
77+
public void onStderr(ByteBuffer buffer, boolean closed) {
78+
if (!closed) {
79+
byte[] bytes = new byte[buffer.remaining()];
80+
buffer.get(bytes);
81+
errorMsg = new String(bytes);
82+
}
83+
super.onStderr(buffer, closed);
84+
}
85+
86+
public Future<InputStream> getInputStream() {
87+
return output;
88+
}
89+
90+
public boolean isRunning() {
91+
return process != null && process.isRunning();
92+
}
93+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.laprun.sustainability.power.sensors.macos.powermetrics;
1+
package net.laprun.sustainability.power.nuprocess;
22

33
import java.nio.ByteBuffer;
44

Lines changed: 8 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,20 @@
11
package net.laprun.sustainability.power.sensors.macos.powermetrics;
22

3-
import java.io.ByteArrayInputStream;
4-
import java.io.InputStream;
5-
import java.nio.ByteBuffer;
6-
import java.util.Arrays;
7-
import java.util.concurrent.CompletableFuture;
8-
import java.util.concurrent.Future;
9-
import java.util.concurrent.TimeUnit;
10-
11-
import com.zaxxer.nuprocess.NuAbstractProcessHandler;
12-
import com.zaxxer.nuprocess.NuProcess;
13-
14-
public class PowermetricsProcessHandler extends NuAbstractProcessHandler {
15-
private String errorMsg;
16-
private NuProcess process;
17-
private final String[] command;
18-
private final GrowableBuffer stdOutBuffer = new GrowableBuffer();
19-
private final CompletableFuture<InputStream> output = new CompletableFuture<>();
3+
import net.laprun.sustainability.power.nuprocess.BaseProcessHandler;
204

5+
public class PowermetricsProcessHandler extends BaseProcessHandler {
216
public PowermetricsProcessHandler(String... command) {
22-
if (command == null || command.length == 0) {
23-
throw new IllegalArgumentException("No powermetrics options specified");
24-
}
7+
super(command);
8+
}
9+
10+
@Override
11+
protected String[] initCommand(String... command) {
2512
final var additionalArgsCardinality = 3;
2613
final var args = new String[command.length + additionalArgsCardinality];
2714
args[0] = "sudo";
2815
args[1] = "powermetrics";
2916
args[2] = "--samplers";
3017
System.arraycopy(command, 0, args, additionalArgsCardinality, command.length);
31-
this.command = args;
32-
}
33-
34-
public String[] command() {
35-
return command;
36-
}
37-
38-
@Override
39-
public void onPreStart(NuProcess nuProcess) {
40-
this.process = nuProcess;
41-
}
42-
43-
@Override
44-
public void onExit(int statusCode) {
45-
if (Integer.MIN_VALUE == statusCode) {
46-
throw new IllegalArgumentException("Unknown command " + Arrays.toString(command));
47-
}
48-
if (statusCode != 0) {
49-
throw new RuntimeException("Couldn't execute command " + Arrays.toString(command)
50-
+ ". Error code: " + statusCode + ", message: " + errorMsg);
51-
}
52-
}
53-
54-
public void stop() {
55-
if (process.isRunning()) {
56-
process.destroy(false);
57-
try {
58-
process.waitFor(5, TimeUnit.SECONDS);
59-
} catch (InterruptedException e) {
60-
throw new RuntimeException(e);
61-
} finally {
62-
process.destroy(true);
63-
}
64-
}
65-
}
66-
67-
@Override
68-
public void onStdout(ByteBuffer buffer, boolean closed) {
69-
if (buffer.hasRemaining()) {
70-
stdOutBuffer.put(buffer);
71-
}
72-
73-
if (closed) {
74-
output.complete(new ByteArrayInputStream(stdOutBuffer.array()));
75-
}
76-
}
77-
78-
@Override
79-
public void onStderr(ByteBuffer buffer, boolean closed) {
80-
if (!closed) {
81-
byte[] bytes = new byte[buffer.remaining()];
82-
buffer.get(bytes);
83-
errorMsg = new String(bytes);
84-
}
85-
super.onStderr(buffer, closed);
86-
}
87-
88-
public Future<InputStream> getInputStream() {
89-
return output;
90-
}
91-
92-
public boolean isRunning() {
93-
return process != null && process.isRunning();
18+
return args;
9419
}
9520
}

0 commit comments

Comments
 (0)