From d41493274b533ea9d80fa987e26fac4cbc6eb021 Mon Sep 17 00:00:00 2001 From: Jarred Li Date: Mon, 21 May 2012 17:14:44 +0800 Subject: [PATCH] support run OS commands --- .../roo/shell/SimpleParser.java | 4 +- .../shell/commands/OsCommands.java | 42 ++++++++++++ .../shell/commands/OsOperations.java | 22 ++++++ .../shell/commands/OsOperationsImpl.java | 67 +++++++++++++++++++ 4 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/springframework/shell/commands/OsCommands.java create mode 100644 src/main/java/org/springframework/shell/commands/OsOperations.java create mode 100644 src/main/java/org/springframework/shell/commands/OsOperationsImpl.java diff --git a/src/main/java/org/springframework/roo/shell/SimpleParser.java b/src/main/java/org/springframework/roo/shell/SimpleParser.java index 9fcf6b60..4b70124f 100644 --- a/src/main/java/org/springframework/roo/shell/SimpleParser.java +++ b/src/main/java/org/springframework/roo/shell/SimpleParser.java @@ -1191,8 +1191,8 @@ public class SimpleParser implements Parser { } LOGGER.info(sb.toString()); - LOGGER.warning("** Type 'hint' (without the quotes) and hit ENTER for step-by-step guidance **" - + StringUtils.LINE_SEPARATOR); +// LOGGER.warning("** Type 'hint' (without the quotes) and hit ENTER for step-by-step guidance **" +// + StringUtils.LINE_SEPARATOR); } } diff --git a/src/main/java/org/springframework/shell/commands/OsCommands.java b/src/main/java/org/springframework/shell/commands/OsCommands.java new file mode 100644 index 00000000..33c11836 --- /dev/null +++ b/src/main/java/org/springframework/shell/commands/OsCommands.java @@ -0,0 +1,42 @@ +package org.springframework.shell.commands; + +import java.io.IOException; +import java.util.logging.Logger; + +import org.springframework.roo.shell.CliCommand; +import org.springframework.roo.shell.CliOption; +import org.springframework.roo.shell.CommandMarker; +import org.springframework.roo.support.logging.HandlerUtils; +import org.springframework.stereotype.Component; + +/** + * Command type to allow execution of native OS commands from the Spring Roo + * shell. + * + * @author Stefan Schmidt + * @since 1.2.0 + */ +@Component +public class OsCommands implements CommandMarker { + + private static final Logger LOGGER = HandlerUtils + .getLogger(OsCommands.class); + + private OsOperations osOperations = new OsOperationsImpl(); + + @CliCommand(value = "!", help = "Allows execution of operating system (OS) commands.") + public void command( + @CliOption(key = { "", "command" }, mandatory = false, specifiedDefaultValue = "", unspecifiedDefaultValue = "", help = "The command to execute") final String command) { + + System.out.println("command is:" + command); + if (command != null && command.length() > 0) { + try { + osOperations.executeCommand(command); + } + catch (final IOException e) { + LOGGER.severe("Unable to execute command " + command + " [" + + e.getMessage() + "]"); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/shell/commands/OsOperations.java b/src/main/java/org/springframework/shell/commands/OsOperations.java new file mode 100644 index 00000000..ea664ea6 --- /dev/null +++ b/src/main/java/org/springframework/shell/commands/OsOperations.java @@ -0,0 +1,22 @@ +package org.springframework.shell.commands; + +import java.io.IOException; + +/** + * Operations type to allow execution of native OS commands from the Spring Roo + * shell. + * + * @author Stefan Schmidt + * @since 1.2.0 + */ +public interface OsOperations { + + /** + * Attempts the execution of a commands and delegates the output to the + * standard logger. + * + * @param command the command to execute + * @throws IOException if an error occurs + */ + void executeCommand(String command) throws IOException; +} \ No newline at end of file diff --git a/src/main/java/org/springframework/shell/commands/OsOperationsImpl.java b/src/main/java/org/springframework/shell/commands/OsOperationsImpl.java new file mode 100644 index 00000000..93288e0f --- /dev/null +++ b/src/main/java/org/springframework/shell/commands/OsOperationsImpl.java @@ -0,0 +1,67 @@ +package org.springframework.shell.commands; + +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.logging.Logger; + +import org.apache.commons.io.IOUtils; +import org.springframework.roo.support.logging.HandlerUtils; +import org.springframework.stereotype.Component; + +/** + * Implementation of {@link OsOperations} interface. + * + * @author Stefan Schmidt + * @since 1.2.0 + */ +@Component +public class OsOperationsImpl implements OsOperations { + private static final Logger LOGGER = HandlerUtils.getLogger(OsOperationsImpl.class); + + public void executeCommand(final String command) throws IOException { + final File root = new File("."); + final Process p = Runtime.getRuntime().exec(command, null, root); + Reader input = new InputStreamReader(p.getInputStream()); + Reader errors = new InputStreamReader(p.getErrorStream()); + + for (String line : IOUtils.readLines(input)) { + if (line.startsWith("[ERROR]")) { + LOGGER.severe(line); + } + else if (line.startsWith("[WARNING]")) { + LOGGER.warning(line); + } + else { + LOGGER.info(line); + } + } + + + for (String line : IOUtils.readLines(errors)) { + if (line.startsWith("[ERROR]")) { + LOGGER.severe(line); + } + else if (line.startsWith("[WARNING]")) { + LOGGER.warning(line); + } + else { + LOGGER.info(line); + } + } + + + p.getOutputStream().close(); + + + try { + if (p.waitFor() != 0) { + LOGGER.warning("The command '" + command + "' did not complete successfully"); + } + } catch (final InterruptedException e) { + throw new IllegalStateException(e); + } + } + +} \ No newline at end of file