BATCH-1705: add try/catch around stdin access

This commit is contained in:
Dave Syer
2011-03-15 09:18:19 +00:00
parent 3e3cfa1dff
commit a2e831cf6d
2 changed files with 34 additions and 8 deletions

View File

@@ -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<String> newargs = new ArrayList<String>(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);
}
}

View File

@@ -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() {