| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.tools; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | |
| 20 | /** |
| 21 | * Exception thrown by <code>ConsoleTools.run()</code> if the command fails. |
| 22 | * |
| 23 | * @see net.sf.jomic.tools.ConsoleTools#run(String[], File, int) |
| 24 | * @author Thomas Aglassinger |
| 25 | */ |
| 26 | public class ConsoleIOException extends IOException |
| 27 | { |
| 28 | private int actualExitCode; |
| 29 | private String[] arguments; |
| 30 | private int expectedExitCode; |
| 31 | private /*@ nullable @*/ String lastErrorMessage; |
| 32 | private LocaleTools localeTools; |
| 33 | |
| 34 | //@ requires newArguments.length > 0; |
| 35 | //@ requires \nonnullelements(newArguments); |
| 36 | public ConsoleIOException( |
| 37 | String[] newArguments, int newExpectedExitCode, int newActualExitCode, |
| 38 | /*@ nullable @*/ String newLastErrorMessage) { |
| 39 | localeTools = LocaleTools.instance(); |
| 40 | |
| 41 | arguments = (String[]) newArguments.clone(); |
| 42 | actualExitCode = newActualExitCode; |
| 43 | expectedExitCode = newExpectedExitCode; |
| 44 | lastErrorMessage = newLastErrorMessage; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get the actual exit code the command failed with. |
| 49 | */ |
| 50 | public /*@ pure @*/ int getActualExitCode() { |
| 51 | return actualExitCode; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get command and arguments that failed. Use <code>arguments[0]</code> to refer to the |
| 56 | * command. |
| 57 | */ |
| 58 | //@ ensures \nonnullelements(\result); |
| 59 | public /*@ pure @*/ String[] getArguments() { |
| 60 | return arguments; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get the expected exit code that would have indicated a successfull execution. |
| 65 | */ |
| 66 | public /*@ pure @*/ int getExpectedExitCode() { |
| 67 | return expectedExitCode; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get the full command with all arguments. |
| 72 | */ |
| 73 | public /*@ pure @*/ String getFullCommand() { |
| 74 | String result = ""; |
| 75 | |
| 76 | for (int i = 0; i < getArguments().length; i += 1) { |
| 77 | if (i > 0) { |
| 78 | result += " "; |
| 79 | } |
| 80 | result += getArguments()[i]; |
| 81 | } |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get the last error message written by the command to <code>System.err()</code>. |
| 87 | */ |
| 88 | public /*@ nullable pure @*/ String getLastErrorMessage() { |
| 89 | return lastErrorMessage; |
| 90 | } |
| 91 | |
| 92 | public /*@ pure @*/ String getMessage() { |
| 93 | String result; |
| 94 | String lastError = getLastErrorMessage(); |
| 95 | |
| 96 | if (lastError == null) { |
| 97 | lastError = ""; |
| 98 | } |
| 99 | Object[] options = new Object[]{getFullCommand(), new Integer(expectedExitCode), |
| 100 | new Integer(actualExitCode), lastError}; |
| 101 | |
| 102 | result = localeTools.getMessage("errors.cannotRunConsoleCommand", options); |
| 103 | return result; |
| 104 | } |
| 105 | } |