Skip to content

Commit 7d21142

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 59b73bc commit 7d21142

File tree

4 files changed

+50
-48
lines changed

4 files changed

+50
-48
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: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ 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

3940
private final String jshellWrapperBaseImageName;
@@ -109,15 +110,15 @@ private void pullImage() throws InterruptedException {
109110
*/
110111
public String createContainer(String name) {
111112
HostConfig hostConfig = HostConfig.newHostConfig()
112-
.withAutoRemove(true)
113-
.withInit(true)
114-
.withCapDrop(Capability.ALL)
115-
.withNetworkMode("none")
116-
.withPidsLimit(2000L)
117-
.withReadonlyRootfs(true)
118-
.withMemory((long) config.dockerMaxRamMegaBytes() * 1024 * 1024)
119-
.withCpuCount((long) Math.ceil(config.dockerCPUsUsage()))
120-
.withCpusetCpus(config.dockerCPUSetCPUs());
113+
.withAutoRemove(true)
114+
.withInit(true)
115+
.withCapDrop(Capability.ALL)
116+
.withNetworkMode("none")
117+
.withPidsLimit(2000L)
118+
.withReadonlyRootfs(true)
119+
.withMemory((long) config.dockerMaxRamMegaBytes() * 1024 * 1024)
120+
.withCpuCount((long) Math.ceil(config.dockerCPUsUsage()))
121+
.withCpusetCpus(config.dockerCPUSetCPUs());
121122

122123
return client.createContainerCmd(jshellWrapperBaseImageName + Config.JSHELL_WRAPPER_IMAGE_NAME_TAG)
123124
.withHostConfig(hostConfig)
@@ -140,14 +141,15 @@ public String createContainer(String name) {
140141
* @param startupScriptId Script to initialize the container with.
141142
* @return The ContainerState of the newly created container.
142143
*/
143-
public ContainerState initializeContainer(String name, StartupScriptId startupScriptId) throws IOException {
144+
public ContainerState initializeContainer(String name, StartupScriptId startupScriptId)
145+
throws IOException {
144146
if (cachedContainers.isEmpty() || !cachedContainers.containsKey(startupScriptId)) {
145147
String containerId = createContainer(name);
146148
return setupContainerWithScript(containerId, true, startupScriptId);
147149
}
148150
String containerId = cachedContainers.get(startupScriptId);
149151
executor.submit(() -> initializeCachedContainer(startupScriptId));
150-
// Rename container with new name.
152+
151153
client.renameContainerCmd(containerId).withName(name).exec();
152154
return setupContainerWithScript(containerId, false, startupScriptId);
153155
}
@@ -163,7 +165,8 @@ private void initializeCachedContainer(StartupScriptId startupScriptId) {
163165
startContainer(id);
164166

165167
try (PipedInputStream containerInput = new PipedInputStream();
166-
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new PipedOutputStream(containerInput)))) {
168+
BufferedWriter writer = new BufferedWriter(
169+
new OutputStreamWriter(new PipedOutputStream(containerInput)))) {
167170
attachToContainer(id, containerInput);
168171

169172
writer.write(Utils.sanitizeStartupScript(startupScriptsService.get(startupScriptId)));
@@ -185,12 +188,14 @@ private void initializeCachedContainer(StartupScriptId startupScriptId) {
185188
* @return ContainerState of the spawned container.
186189
* @throws IOException if an I/O error occurs
187190
*/
188-
private ContainerState setupContainerWithScript(String containerId, boolean isCached, StartupScriptId startupScriptId) throws IOException {
191+
private ContainerState setupContainerWithScript(String containerId, boolean isCached,
192+
StartupScriptId startupScriptId) throws IOException {
189193
if (!isCached) {
190194
startContainer(containerId);
191195
}
192196
PipedInputStream containerInput = new PipedInputStream();
193-
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new PipedOutputStream(containerInput)));
197+
BufferedWriter writer =
198+
new BufferedWriter(new OutputStreamWriter(new PipedOutputStream(containerInput)));
194199

195200
InputStream containerOutput = attachToContainer(containerId, containerInput);
196201
BufferedReader reader = new BufferedReader(new InputStreamReader(containerOutput));
@@ -206,6 +211,7 @@ private ContainerState setupContainerWithScript(String containerId, boolean isCa
206211

207212
/**
208213
* Creates a new container
214+
*
209215
* @param containerId the ID of the container to start
210216
*/
211217
public void startContainer(String containerId) {
@@ -219,35 +225,38 @@ public void startContainer(String containerId) {
219225
* Logs any output from stderr and returns an InputStream to read stdout.
220226
*
221227
* @param containerId the ID of the running container to attach to
222-
* @param containerInput the input stream (containerInput) to send to the container
228+
* @param containerInput the input stream (containerInput) to send to the container
223229
* @return InputStream to read the container's stdout
224230
* @throws IOException if an I/O error occurs
225231
*/
226-
public InputStream attachToContainer(String containerId, InputStream containerInput) throws IOException {
232+
public InputStream attachToContainer(String containerId, InputStream containerInput)
233+
throws IOException {
227234
PipedInputStream pipeIn = new PipedInputStream();
228235
PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);
229236

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

252261
return pipeIn;
253262
}

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)