Skip to content

Commit 8adf7b0

Browse files
chore: Apply code style checks with Spotless and linting with SonarQube
- Ran code formatting and styling checks using Spotless to ensure consistent code style. - Performed code analysis and linting with SonarQube to identify and address potential issues. - Fixed minor issues flagged during the linting process to enhance code quality.
1 parent 7ee2fa9 commit 8adf7b0

File tree

4 files changed

+71
-68
lines changed

4 files changed

+71
-68
lines changed

JShellAPI/src/main/java/org/togetherjava/jshellapi/dto/ContainerState.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import java.io.BufferedReader;
44
import java.io.BufferedWriter;
5-
import java.io.InputStream;
6-
import java.io.OutputStream;
75

8-
public record ContainerState(boolean isCached, String containerId, BufferedReader containerOutput, BufferedWriter containerInput) {
6+
public record ContainerState(boolean isCached, String containerId, BufferedReader containerOutput,
7+
BufferedWriter containerInput) {
98
}

JShellAPI/src/main/java/org/togetherjava/jshellapi/service/DockerService.java

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ public class DockerService implements DisposableBean {
3333
private final DockerClient client;
3434
private final Config config;
3535
private final ExecutorService executor = Executors.newSingleThreadExecutor();
36-
private final ConcurrentHashMap<StartupScriptId, String> cachedContainers = new ConcurrentHashMap<>();
36+
private final ConcurrentHashMap<StartupScriptId, String> cachedContainers =
37+
new ConcurrentHashMap<>();
3738
private final StartupScriptsService startupScriptsService;
3839

39-
public DockerService(Config config, StartupScriptsService startupScriptsService) throws InterruptedException, IOException {
40+
public DockerService(Config config, StartupScriptsService startupScriptsService)
41+
throws InterruptedException {
4042
this.startupScriptsService = startupScriptsService;
4143
DefaultDockerClientConfig clientConfig =
4244
DefaultDockerClientConfig.createDefaultConfigBuilder().build();
@@ -77,11 +79,11 @@ private void cleanupLeftovers(UUID currentId) {
7779
*/
7880
private boolean isImagePresentLocally() {
7981
return client.listImagesCmd()
80-
.withFilter("reference", List.of(IMAGE_NAME))
81-
.exec()
82-
.stream()
83-
.flatMap(it -> Arrays.stream(it.getRepoTags()))
84-
.anyMatch(it -> it.endsWith(":" + IMAGE_TAG));
82+
.withFilter("reference", List.of(IMAGE_NAME))
83+
.exec()
84+
.stream()
85+
.flatMap(it -> Arrays.stream(it.getRepoTags()))
86+
.anyMatch(it -> it.endsWith(":" + IMAGE_TAG));
8587
}
8688

8789
/**
@@ -90,9 +92,9 @@ private boolean isImagePresentLocally() {
9092
private void pullImage() throws InterruptedException {
9193
if (!isImagePresentLocally()) {
9294
client.pullImageCmd(IMAGE_NAME)
93-
.withTag(IMAGE_TAG)
94-
.exec(new PullImageResultCallback())
95-
.awaitCompletion(5, TimeUnit.MINUTES);
95+
.withTag(IMAGE_TAG)
96+
.exec(new PullImageResultCallback())
97+
.awaitCompletion(5, TimeUnit.MINUTES);
9698
}
9799
}
98100

@@ -104,28 +106,28 @@ private void pullImage() throws InterruptedException {
104106
*/
105107
public String createContainer(String name) {
106108
HostConfig hostConfig = HostConfig.newHostConfig()
107-
.withAutoRemove(true)
108-
.withInit(true)
109-
.withCapDrop(Capability.ALL)
110-
.withNetworkMode("none")
111-
.withPidsLimit(2000L)
112-
.withReadonlyRootfs(true)
113-
.withMemory((long) config.dockerMaxRamMegaBytes() * 1024 * 1024)
114-
.withCpuCount((long) Math.ceil(config.dockerCPUsUsage()))
115-
.withCpusetCpus(config.dockerCPUSetCPUs());
109+
.withAutoRemove(true)
110+
.withInit(true)
111+
.withCapDrop(Capability.ALL)
112+
.withNetworkMode("none")
113+
.withPidsLimit(2000L)
114+
.withReadonlyRootfs(true)
115+
.withMemory((long) config.dockerMaxRamMegaBytes() * 1024 * 1024)
116+
.withCpuCount((long) Math.ceil(config.dockerCPUsUsage()))
117+
.withCpusetCpus(config.dockerCPUSetCPUs());
116118

117119
return client.createContainerCmd(IMAGE_NAME + ":" + IMAGE_TAG)
118-
.withHostConfig(hostConfig)
119-
.withStdinOpen(true)
120-
.withAttachStdin(true)
121-
.withAttachStderr(true)
122-
.withAttachStdout(true)
123-
.withEnv("evalTimeoutSeconds=" + config.evalTimeoutSeconds(),
124-
"sysOutCharLimit=" + config.sysOutCharLimit())
125-
.withLabels(Map.of(WORKER_LABEL, WORKER_UNIQUE_ID.toString()))
126-
.withName(name)
127-
.exec()
128-
.getId();
120+
.withHostConfig(hostConfig)
121+
.withStdinOpen(true)
122+
.withAttachStdin(true)
123+
.withAttachStderr(true)
124+
.withAttachStdout(true)
125+
.withEnv("evalTimeoutSeconds=" + config.evalTimeoutSeconds(),
126+
"sysOutCharLimit=" + config.sysOutCharLimit())
127+
.withLabels(Map.of(WORKER_LABEL, WORKER_UNIQUE_ID.toString()))
128+
.withName(name)
129+
.exec()
130+
.getId();
129131
}
130132

131133
/**
@@ -135,14 +137,15 @@ public String createContainer(String name) {
135137
* @param startupScriptId Script to initialize the container with.
136138
* @return The ContainerState of the newly created container.
137139
*/
138-
public ContainerState initializeContainer(String name, StartupScriptId startupScriptId) throws IOException {
140+
public ContainerState initializeContainer(String name, StartupScriptId startupScriptId)
141+
throws IOException {
139142
if (cachedContainers.isEmpty() || !cachedContainers.containsKey(startupScriptId)) {
140143
String containerId = createContainer(name);
141144
return setupContainerWithScript(containerId, true, startupScriptId);
142145
}
143146
String containerId = cachedContainers.get(startupScriptId);
144147
executor.submit(() -> initializeCachedContainer(startupScriptId));
145-
// Rename container with new name.
148+
146149
client.renameContainerCmd(containerId).withName(name).exec();
147150
return setupContainerWithScript(containerId, false, startupScriptId);
148151
}
@@ -158,7 +161,8 @@ private void initializeCachedContainer(StartupScriptId startupScriptId) {
158161
startContainer(id);
159162

160163
try (PipedInputStream containerInput = new PipedInputStream();
161-
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new PipedOutputStream(containerInput)))) {
164+
BufferedWriter writer = new BufferedWriter(
165+
new OutputStreamWriter(new PipedOutputStream(containerInput)))) {
162166
attachToContainer(id, containerInput);
163167

164168
writer.write(Utils.sanitizeStartupScript(startupScriptsService.get(startupScriptId)));
@@ -180,12 +184,14 @@ private void initializeCachedContainer(StartupScriptId startupScriptId) {
180184
* @return ContainerState of the spawned container.
181185
* @throws IOException if an I/O error occurs
182186
*/
183-
private ContainerState setupContainerWithScript(String containerId, boolean isCached, StartupScriptId startupScriptId) throws IOException {
187+
private ContainerState setupContainerWithScript(String containerId, boolean isCached,
188+
StartupScriptId startupScriptId) throws IOException {
184189
if (!isCached) {
185190
startContainer(containerId);
186191
}
187192
PipedInputStream containerInput = new PipedInputStream();
188-
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new PipedOutputStream(containerInput)));
193+
BufferedWriter writer =
194+
new BufferedWriter(new OutputStreamWriter(new PipedOutputStream(containerInput)));
189195

190196
InputStream containerOutput = attachToContainer(containerId, containerInput);
191197
BufferedReader reader = new BufferedReader(new InputStreamReader(containerOutput));
@@ -201,6 +207,7 @@ private ContainerState setupContainerWithScript(String containerId, boolean isCa
201207

202208
/**
203209
* Creates a new container
210+
*
204211
* @param containerId the ID of the container to start
205212
*/
206213
public void startContainer(String containerId) {
@@ -214,35 +221,38 @@ public void startContainer(String containerId) {
214221
* Logs any output from stderr and returns an InputStream to read stdout.
215222
*
216223
* @param containerId the ID of the running container to attach to
217-
* @param containerInput the input stream (containerInput) to send to the container
224+
* @param containerInput the input stream (containerInput) to send to the container
218225
* @return InputStream to read the container's stdout
219226
* @throws IOException if an I/O error occurs
220227
*/
221-
public InputStream attachToContainer(String containerId, InputStream containerInput) throws IOException {
228+
public InputStream attachToContainer(String containerId, InputStream containerInput)
229+
throws IOException {
222230
PipedInputStream pipeIn = new PipedInputStream();
223231
PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);
224232

225233
client.attachContainerCmd(containerId)
226-
.withLogs(true)
227-
.withFollowStream(true)
228-
.withStdOut(true)
229-
.withStdErr(true)
230-
.withStdIn(containerInput)
231-
.exec(new ResultCallback.Adapter<>() {
232-
@Override
233-
public void onNext(Frame object) {
234-
try {
235-
String payloadString = new String(object.getPayload(), StandardCharsets.UTF_8);
236-
if (object.getStreamType() == StreamType.STDOUT) {
237-
pipeOut.write(object.getPayload()); // Write stdout data to pipeOut
238-
} else {
239-
LOGGER.warn("Received STDERR from container {}: {}", containerId, payloadString);
240-
}
241-
} catch (IOException e) {
242-
throw new UncheckedIOException(e);
234+
.withLogs(true)
235+
.withFollowStream(true)
236+
.withStdOut(true)
237+
.withStdErr(true)
238+
.withStdIn(containerInput)
239+
.exec(new ResultCallback.Adapter<>() {
240+
@Override
241+
public void onNext(Frame object) {
242+
try {
243+
String payloadString =
244+
new String(object.getPayload(), StandardCharsets.UTF_8);
245+
if (object.getStreamType() == StreamType.STDOUT) {
246+
pipeOut.write(object.getPayload()); // Write stdout data to pipeOut
247+
} else {
248+
LOGGER.warn("Received STDERR from container {}: {}", containerId,
249+
payloadString);
243250
}
251+
} catch (IOException e) {
252+
throw new UncheckedIOException(e);
244253
}
245-
});
254+
}
255+
});
246256

247257
return pipeIn;
248258
}

JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellService.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
5-
import org.springframework.lang.Nullable;
65

76
import org.togetherjava.jshellapi.Config;
87
import org.togetherjava.jshellapi.dto.*;
98
import org.togetherjava.jshellapi.exceptions.DockerException;
109

1110
import java.io.*;
12-
import java.time.Duration;
1311
import java.time.Instant;
1412
import java.util.ArrayList;
1513
import java.util.List;
@@ -31,12 +29,8 @@ public class JShellService {
3129
private final DockerService dockerService;
3230
private final int startupScriptSize;
3331

34-
public JShellService(
35-
DockerService dockerService,
36-
JShellSessionService sessionService,
37-
SessionInfo sessionInfo,
38-
Config config
39-
) throws DockerException {
32+
public JShellService(DockerService dockerService, JShellSessionService sessionService,
33+
SessionInfo sessionInfo, Config config) throws DockerException {
4034
this.dockerService = dockerService;
4135
this.sessionService = sessionService;
4236
this.id = sessionInfo.id();
@@ -52,7 +46,8 @@ public JShellService(
5246
}
5347

5448
try {
55-
ContainerState containerState = dockerService.initializeContainer(containerName(), sessionInfo.startupScriptId());
49+
ContainerState containerState = dockerService.initializeContainer(containerName(),
50+
sessionInfo.startupScriptId());
5651
this.writer = containerState.containerInput();
5752
this.reader = containerState.containerOutput();
5853
checkContainerOK();

JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellSessionService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ private synchronized JShellService createSession(SessionInfo sessionInfo)
9090
LOGGER.info("Creating session : {}.", sessionInfo);
9191
JShellService service = new JShellService(dockerService, this, sessionInfo, config);
9292

93-
9493
jshellSessions.put(sessionInfo.id(), service);
9594
return service;
9695
}

0 commit comments

Comments
 (0)