Add new interpreter registration mechanism

This commit is contained in:
fvaleri 2016-06-30 12:24:34 +02:00
parent c069074680
commit 7c42733ac0
2 changed files with 6 additions and 16 deletions

View file

@ -47,29 +47,18 @@ import org.slf4j.LoggerFactory;
*/
public class ShellInterpreter extends Interpreter {
private static final Logger LOGGER = LoggerFactory.getLogger(ShellInterpreter.class);
private static final String SHELL_COMMAND_TIMEOUT = "shell.command.timeout.millisecs";
private static final String DEFAULT_COMMAND_TIMEOUT = "600000";
private static final String TIMEOUT_PROPERTY = "shell.command.timeout.millisecs";
private final boolean isWindows = System.getProperty("os.name").startsWith("Windows");
private final String shell = isWindows ? "cmd /c" : "bash -c";
private Map<String, DefaultExecutor> executors;
static {
Interpreter.register(
"sh", "sh",
ShellInterpreter.class.getName(),
new InterpreterPropertyBuilder()
.add(SHELL_COMMAND_TIMEOUT, DEFAULT_COMMAND_TIMEOUT,
"Shell command time out in millisecs. Default = " + DEFAULT_COMMAND_TIMEOUT).build()
);
}
public ShellInterpreter(Properties property) {
super(property);
}
@Override
public void open() {
LOGGER.info("Command timeout is set to: {}", DEFAULT_COMMAND_TIMEOUT);
LOGGER.info("Command timeout property: {}", TIMEOUT_PROPERTY);
executors = new HashMap<String, DefaultExecutor>();
}
@ -95,7 +84,7 @@ public class ShellInterpreter extends Interpreter {
try {
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(outStream, errStream));
executor.setWatchdog(new ExecuteWatchdog(Long.valueOf(getProperty(SHELL_COMMAND_TIMEOUT))));
executor.setWatchdog(new ExecuteWatchdog(Long.valueOf(getProperty(TIMEOUT_PROPERTY))));
executors.put(contextInterpreter.getParagraphId(), executor);
int exitVal = executor.execute(cmdLine);
LOGGER.info("Paragraph " + contextInterpreter.getParagraphId()

View file

@ -34,7 +34,9 @@ public class ShellInterpreterTest {
@Before
public void setUp() throws Exception {
shell = new ShellInterpreter(new Properties());
Properties p = new Properties();
p.setProperty("shell.command.timeout.millisecs", "60000");
shell = new ShellInterpreter(p);
}
@After
@ -51,7 +53,6 @@ public class ShellInterpreterTest {
} else {
result = shell.interpret("ls", context);
}
// System.out.println(result.message());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
}