@@ -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 }
0 commit comments