diff --git a/gclc.system/src/main/java/net/bigeon/gclc/system/ExecSystemCommand.java b/gclc.system/src/main/java/net/bigeon/gclc/system/ExecSystemCommand.java index 79aa835..88dc150 100644 --- a/gclc.system/src/main/java/net/bigeon/gclc/system/ExecSystemCommand.java +++ b/gclc.system/src/main/java/net/bigeon/gclc/system/ExecSystemCommand.java @@ -56,6 +56,8 @@ import net.bigeon.gclc.manager.ConsoleOutput; * @author Emmanuel Bigeon */ public class ExecSystemCommand extends Command { + /** Error message for the manager closed. */ + private static final String MANAGER_WAS_CLOSED = "manager was closed"; /** The command default name */ private static final String COMMAND_DEFAULT_NAME = "exec"; //$NON-NLS-1$ /** The end of line separator */ @@ -64,12 +66,14 @@ public class ExecSystemCommand extends Command { private static final Logger LOGGER = Logger .getLogger(ExecSystemCommand.class.getName()); - /***/ + /** Create the command with the name 'exec'. */ public ExecSystemCommand() { super(COMMAND_DEFAULT_NAME); } - /** @param name the name of the command (the input from the manager that should + /** Create the command with a custom name. + * + * @param name the name of the command (the input from the manager that should * trigger this command) */ public ExecSystemCommand(final String name) { super(name); @@ -92,7 +96,6 @@ public class ExecSystemCommand extends Command { final InputStream is = proc.getInputStream(); final Thread th = new Thread(new Runnable() { - @SuppressWarnings("synthetic-access") @Override public void run() { try { @@ -110,18 +113,12 @@ public class ExecSystemCommand extends Command { final OutputStream os = proc.getOutputStream(); try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os))) { while (th.isAlive()) { - String user; - try { - user = in.prompt(); - } catch (final IOException e) { - throw new CommandRunException(CommandRunExceptionType.INTERACTION, - "manager was closed", e); //$NON-NLS-1$ - } + String user = in.prompt(); writer.write(user + EOL); } } catch (final IOException e1) { throw new CommandRunException(CommandRunExceptionType.INTERACTION, - "manager was closed", e1); //$NON-NLS-1$ + MANAGER_WAS_CLOSED, e1); // $NON-NLS-1$ } } @@ -133,18 +130,28 @@ public class ExecSystemCommand extends Command { int c; try { while ((c = is.read()) != -1) { - try { - out.print(Character.valueOf((char) c).toString()); - } catch (final IOException e) { - throw new CommandRunException(CommandRunExceptionType.INTERACTION, - "manager was closed", e); //$NON-NLS-1$ - } + printCharacter(out, (char) c); } } catch (final IOException e) { LOGGER.log(Level.INFO, "input stream reading failed", e); //$NON-NLS-1$ } } + /** Print a character on the console. + * + * @param out the console + * @param c the character + * @throws CommandRunException if the console failed to print the character. */ + private void printCharacter(final ConsoleOutput out, char c) + throws CommandRunException { + try { + out.print(Character.toString(c)); + } catch (final IOException e) { + throw new CommandRunException(CommandRunExceptionType.INTERACTION, + MANAGER_WAS_CLOSED, e); // $NON-NLS-1$ + } + } + /* (non-Javadoc) * @see fr.bigeon.gclc.command.ICommand#tip() */ @Override diff --git a/gclc.system/src/main/java/net/bigeon/gclc/system/package-info.java b/gclc.system/src/main/java/net/bigeon/gclc/system/package-info.java new file mode 100644 index 0000000..3af1652 --- /dev/null +++ b/gclc.system/src/main/java/net/bigeon/gclc/system/package-info.java @@ -0,0 +1,8 @@ +/** This package contains classes for command execution on the system. + *
+ * The class {@link net.bigeon.gclc.system.ExecSystemCommand} is, in the current + * version (0.0.1), a source of vulnerability and should probably be used only + * in private projects where the user is aware of the dangers. + * + * @author Emmanuel Bigeon */ +package net.bigeon.gclc.system;