From a2e831cf6ded52916fd6513638f00c908cef45bf Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 15 Mar 2011 09:18:19 +0000 Subject: [PATCH] BATCH-1705: add try/catch around stdin access --- .../launch/support/CommandLineJobRunner.java | 25 +++++++++++++------ .../support/CommandLineJobRunnerTests.java | 17 +++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java index c1890bc5d..e897326c3 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java @@ -16,6 +16,7 @@ package org.springframework.batch.core.launch.support; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; @@ -514,15 +515,23 @@ public class CommandLineJobRunner { List newargs = new ArrayList(Arrays.asList(args)); - if (System.in.available() > 0) { - BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); - String line = " "; - while (StringUtils.hasLength(line)) { - if (!line.startsWith("#") && StringUtils.hasText(line)) { - logger.debug("Stdin arg: "+line); - newargs.add(line); + try { + if (System.in.available() > 0) { + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + String line = " "; + while (StringUtils.hasLength(line)) { + if (!line.startsWith("#") && StringUtils.hasText(line)) { + logger.debug("Stdin arg: " + line); + newargs.add(line); + } + line = reader.readLine(); } - line = reader.readLine(); + } + } + catch (IOException e) { + logger.warn("Could not access stdin (maybe a platform limitation)"); + if (logger.isDebugEnabled()) { + logger.debug("Exception details", e); } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java index b54de0eec..88c6a451a 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java @@ -18,6 +18,7 @@ package org.springframework.batch.core.launch.support; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; @@ -136,6 +137,22 @@ public class CommandLineJobRunnerTests { assertEquals(new JobParameters(), StubJobLauncher.jobParameters); } + @Test + public void testWithInvalidStdin() throws Throwable { + System.setIn(new InputStream() { + public int available() throws IOException { + throw new IOException("Planned"); + } + + public int read() { + return -1; + } + }); + CommandLineJobRunner.main(new String[] { jobPath, jobName }); + assertEquals(0, StubSystemExiter.status); + assertEquals(0, StubJobLauncher.jobParameters.getParameters().size()); + } + @Test public void testWithStdinCommandLine() throws Throwable { System.setIn(new InputStream() {