changeset 1099:07dca767d55d

Localization of messages for CommandException and children reviewed-by: omajid review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-May/006502.html
author Jon VanAlten <vanaltj@gmail.com>
date Thu, 25 Apr 2013 17:27:42 -0600
parents 2471ea5c679c
children c469675494d7
files client/cli/src/main/java/com/redhat/thermostat/client/cli/HostVMArguments.java client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ConnectCommand.java client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/LocaleResources.java client/cli/src/main/resources/com/redhat/thermostat/client/cli/strings.properties common/core/src/main/java/com/redhat/thermostat/common/cli/Command.java common/core/src/main/java/com/redhat/thermostat/common/cli/CommandException.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandLineArgumentsParser.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/LocaleResources.java launcher/src/main/resources/com/redhat/thermostat/launcher/internal/strings.properties vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/FindObjectsCommand.java vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/HeapNotFoundException.java vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ObjectNotFoundException.java vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/SaveHeapDumpToFileCommand.java vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ShowHeapHistogramCommand.java vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/locale/LocaleResources.java vm-heap-analysis/command/src/main/resources/com/redhat/thermostat/vm/heap/analysis/command/locale/strings.properties vm-heap-analysis/command/src/test/java/com/redhat/thermostat/vm/heap/analysis/command/internal/FindObjectsCommandTest.java vm-heap-analysis/command/src/test/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ShowHeapHistogramCommandTest.java
diffstat 19 files changed, 86 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/client/cli/src/main/java/com/redhat/thermostat/client/cli/HostVMArguments.java	Thu May 16 11:40:43 2013 -0600
+++ b/client/cli/src/main/java/com/redhat/thermostat/client/cli/HostVMArguments.java	Thu Apr 25 17:27:42 2013 -0600
@@ -36,8 +36,10 @@
 
 package com.redhat.thermostat.client.cli;
 
+import com.redhat.thermostat.client.cli.internal.LocaleResources;
 import com.redhat.thermostat.common.cli.Arguments;
 import com.redhat.thermostat.common.cli.CommandException;
+import com.redhat.thermostat.common.locale.Translate;
 import com.redhat.thermostat.storage.core.HostRef;
 import com.redhat.thermostat.storage.core.VmRef;
 
@@ -46,6 +48,8 @@
     static final String HOST_ID_ARGUMENT = "hostId";
     static final String VM_ID_ARGUMENT = "vmId";
 
+    private static final Translate<LocaleResources> tr = LocaleResources.createLocalizer();
+
     private HostRef host;
     private VmRef vm;
 
@@ -57,7 +61,7 @@
         String hostId = args.getArgument(HOST_ID_ARGUMENT);
         String vmId = args.getArgument(VM_ID_ARGUMENT);
         if (hostRequired && hostId == null) {
-            throw new CommandException("a " + HOST_ID_ARGUMENT + " is required");
+            throw new CommandException(tr.localize(LocaleResources.HOSTID_REQUIRED_MESSAGE));
         } else if (hostId == null) {
             host = null;
         } else {
@@ -78,7 +82,7 @@
         try {
             return Integer.parseInt(vmId);
         } catch (NumberFormatException ex) {
-            throw new CommandException("Invalid VM ID: " + vmId, ex);
+            throw new CommandException(tr.localize(LocaleResources.INVALID_VMID_MESSAGE, vmId), ex);
         }
     }
 
--- a/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ConnectCommand.java	Thu May 16 11:40:43 2013 -0600
+++ b/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ConnectCommand.java	Thu Apr 25 17:27:42 2013 -0600
@@ -118,7 +118,7 @@
                 username = getUserPass.getUserName(dbUrl);
                 password = new String(getUserPass.getPassword(dbUrl));
             } catch (IOException e) {
-                throw new CommandException("Could not get username or password from user.", e);
+                throw new CommandException(translator.localize(LocaleResources.COMMAND_CONNECT_USER_PROMPT_ERROR), e);
             }
         }
         try {
--- a/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/LocaleResources.java	Thu May 16 11:40:43 2013 -0600
+++ b/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/LocaleResources.java	Thu Apr 25 17:27:42 2013 -0600
@@ -52,6 +52,7 @@
     COMMAND_CONNECT_INVALID_STORAGE,
     COMMAND_CONNECT_NO_KEYRING,
     COMMAND_CONNECT_ERROR,
+    COMMAND_CONNECT_USER_PROMPT_ERROR,
 
     COMMAND_DISCONNECT_NOT_CONNECTED,
     COMMAND_DISCONNECT_ERROR,
@@ -75,6 +76,9 @@
     VM_STOP_TIME_RUNNING,
     VM_STATUS_ALIVE,
     VM_STATUS_DEAD,
+
+    HOSTID_REQUIRED_MESSAGE,
+    INVALID_VMID_MESSAGE,
     ;
 
     static final String RESOURCE_BUNDLE = "com.redhat.thermostat.client.cli.strings";
--- a/client/cli/src/main/resources/com/redhat/thermostat/client/cli/strings.properties	Thu May 16 11:40:43 2013 -0600
+++ b/client/cli/src/main/resources/com/redhat/thermostat/client/cli/strings.properties	Thu Apr 25 17:27:42 2013 -0600
@@ -10,6 +10,7 @@
 COMMAND_CONNECT_INVALID_STORAGE = Unrecognized storage URL {0}
 COMMAND_CONNECT_NO_KEYRING = Unable to retrieve keyring
 COMMAND_CONNECT_ERROR = Error: {0}
+COMMAND_CONNECT_USER_PROMPT_ERROR = Error while prompting for username and password.
 
 COMMAND_DISCONNECT_NOT_CONNECTED = Not connected to storage. You may use the connect command for establishing connections.
 COMMAND_DISCONNECT_ERROR = Failed to disconnect from database.
@@ -33,3 +34,6 @@
 VM_STOP_TIME_RUNNING = <Running>
 VM_STATUS_ALIVE = RUNNING
 VM_STATUS_DEAD = EXITED
+
+HOSTID_REQUIRED_MESSAGE = a hostId is required
+INVALID_VMID_MESSAGE = Invalid VM ID: {0}
--- a/common/core/src/main/java/com/redhat/thermostat/common/cli/Command.java	Thu May 16 11:40:43 2013 -0600
+++ b/common/core/src/main/java/com/redhat/thermostat/common/cli/Command.java	Thu Apr 25 17:27:42 2013 -0600
@@ -62,7 +62,9 @@
     public static final String NAME = "COMMAND_NAME";
 
     /**
-     * Execute the command
+     * Execute the command.  A CommandException may be thrown to indicate a failure
+     * condition; if so the {@link com.redhat.thermostat.launcher.Launcher} will
+     * present the exception message to the user.
      */
     public void run(CommandContext ctx) throws CommandException;
 
--- a/common/core/src/main/java/com/redhat/thermostat/common/cli/CommandException.java	Thu May 16 11:40:43 2013 -0600
+++ b/common/core/src/main/java/com/redhat/thermostat/common/cli/CommandException.java	Thu Apr 25 17:27:42 2013 -0600
@@ -36,6 +36,10 @@
 
 package com.redhat.thermostat.common.cli;
 
+/**
+ * An exception to be thrown by a {@link Command} upon implementation-defined
+ * failure conditions.
+ */
 public class CommandException extends Exception {
 
     private static final long serialVersionUID = 3730368617641245016L;
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandLineArgumentsParser.java	Thu May 16 11:40:43 2013 -0600
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandLineArgumentsParser.java	Thu Apr 25 17:27:42 2013 -0600
@@ -49,9 +49,12 @@
 import org.apache.commons.cli.ParseException;
 
 import com.redhat.thermostat.common.cli.Arguments;
+import com.redhat.thermostat.common.locale.Translate;
 
 public class CommandLineArgumentsParser {
 
+    private static Translate<LocaleResources> tr = LocaleResources.createLocalizer();
+
     private Options options = new Options();
 
     @SuppressWarnings("unchecked")
@@ -80,9 +83,9 @@
         List<String> missingOptions = mae.getMissingOptions();
         StringBuilder msg = new StringBuilder();
         if (missingOptions.size() == 1) {
-            msg.append("Missing required option: ");
+            msg.append(tr.localize(LocaleResources.MISSING_OPTION));
         } else {
-            msg.append("Missing required options: ");
+            msg.append(tr.localize(LocaleResources.MISSING_OPTIONS));
         }
         for (Iterator<String> i = missingOptions.iterator(); i.hasNext();) {
             String missingOption = i.next();
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Thu May 16 11:40:43 2013 -0600
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Thu Apr 25 17:27:42 2013 -0600
@@ -42,6 +42,7 @@
 import java.util.Collection;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import org.apache.commons.cli.Options;
 import org.osgi.framework.BundleContext;
@@ -81,6 +82,7 @@
 public class LauncherImpl implements Launcher {
 
     private static final Translate<LocaleResources> t = LocaleResources.createLocalizer();
+    private Logger logger;
 
     private final AtomicInteger usageCount = new AtomicInteger(0);
     private final BundleContext context;
@@ -111,6 +113,7 @@
         this.commandInfoSource = commandInfoSource;
 
         loggingInitializer.initialize();
+        logger = LoggingUtils.getLogger(LauncherImpl.class);
     }
 
     @Override
@@ -331,7 +334,7 @@
                         username = getUserPass.getUserName(dbUrl);
                         password = new String(getUserPass.getPassword(dbUrl));
                     } catch (IOException ex) {
-                        throw new CommandException("Could not get username or password from user.", ex);
+                        throw new CommandException(t.localize(LocaleResources.LAUNCHER_USER_AUTH_PROMPT_ERROR), ex);
                     }
                 }
                 try {
@@ -340,11 +343,12 @@
                     // This registers the DbService if all goes well
                     service.connect();
                 } catch (StorageException ex) {
-                    throw new CommandException("Unsupported storage URL: " + dbUrl);
+                    throw new CommandException(t.localize(LocaleResources.LAUNCHER_MALFORMED_URL, dbUrl));
                 } catch (ConnectionException ex) {
                     String error = ex.getMessage();
                     String message = ( error == null ? "" : " Error: " + error );
-                    throw new CommandException("Could not connect to: " + dbUrl + message, ex);
+                    logger.log(Level.SEVERE, "Could not connect to: " + dbUrl + message, ex);
+                    throw new CommandException(t.localize(LocaleResources.LAUNCHER_CONNECTION_ERROR, dbUrl), ex);
                 }
             }
         }
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LocaleResources.java	Thu May 16 11:40:43 2013 -0600
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LocaleResources.java	Thu Apr 25 17:27:42 2013 -0600
@@ -51,10 +51,17 @@
 
     OPTION_DB_URL_DESC,
     OPTION_LOG_LEVEL_DESC,
-    
+
+    MISSING_OPTION,
+    MISSING_OPTIONS,
+
+    LAUNCHER_USER_AUTH_PROMPT_ERROR,
+    LAUNCHER_MALFORMED_URL,
+    LAUNCHER_CONNECTION_ERROR,
+
     VALIDATION_WARNING,
     VALIDATION_ERROR,
-    VALIDATION_FATAL_ERROR
+    VALIDATION_FATAL_ERROR,
     ;
 
     static final String RESOURCE_BUNDLE = "com.redhat.thermostat.launcher.internal.strings";
--- a/launcher/src/main/resources/com/redhat/thermostat/launcher/internal/strings.properties	Thu May 16 11:40:43 2013 -0600
+++ b/launcher/src/main/resources/com/redhat/thermostat/launcher/internal/strings.properties	Thu Apr 25 17:27:42 2013 -0600
@@ -11,7 +11,14 @@
 OPTION_LOG_LEVEL_DESC = sets the log level for this invocation. Possible values \
  for <logLevel> in decreasing severity are: SEVERE, WARNING, INFO, CONFIG, FINE, \
  FINER, FINEST and OFF
- 
+
+MISSING_OPTION = Missing required option: 
+MISSING_OPTIONS = Missing required options: 
+
+LAUNCHER_USER_AUTH_PROMPT_ERROR = Error while prompting for username and password.
+LAUNCHER_MALFORMED_URL = Unsupported storage URL: {0}
+LAUNCHER_CONNECTION_ERROR = Could not connect to: {0}
+
 VALIDATION_WARNING = Warning in file {0}:{1}.{2}\n
 VALIDATION_ERROR = Error in file {0}:{1}.{2}\n
-VALIDATION_FATAL_ERROR = Fatal error in file {0}:{1}.{2}\n
+VALIDATION_FATAL_ERROR = Fatal error in file {0}:{1}.{2}\n
\ No newline at end of file
--- a/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/FindObjectsCommand.java	Thu May 16 11:40:43 2013 -0600
+++ b/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/FindObjectsCommand.java	Thu Apr 25 17:27:42 2013 -0600
@@ -93,13 +93,11 @@
         String heapId = ctx.getArguments().getArgument(HEAP_ID_ARG);
         HeapInfo heapInfo = heapDAO.getHeapInfo(heapId);
         if (heapInfo == null) {
-            ctx.getConsole().getOutput().println(translator.localize(LocaleResources.HEAP_ID_NOT_FOUND, heapId));
-            return;
+            throw new HeapNotFoundException(heapId);
         }
         HeapDump heapDump = heapDAO.getHeapDump(heapInfo);
         if (heapDump == null) {
-            ctx.getConsole().getOutput().println(translator.localize(LocaleResources.HEAP_ID_NOT_FOUND, heapId));
-            return;
+            throw new HeapNotFoundException(heapId);
         }
 
         List<String> terms = ctx.getArguments().getNonOptionArguments();
--- a/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/HeapNotFoundException.java	Thu May 16 11:40:43 2013 -0600
+++ b/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/HeapNotFoundException.java	Thu Apr 25 17:27:42 2013 -0600
@@ -37,14 +37,16 @@
 package com.redhat.thermostat.vm.heap.analysis.command.internal;
 
 import com.redhat.thermostat.common.cli.CommandException;
+import com.redhat.thermostat.common.locale.Translate;
+import com.redhat.thermostat.vm.heap.analysis.command.locale.LocaleResources;
 
 @SuppressWarnings("serial")
 public class HeapNotFoundException extends CommandException {
 
-    private static final String MESSAGE_TEMPLATE = "Heap ID not found: ";
+    private static final Translate<LocaleResources> tr = LocaleResources.createLocalizer();
 
     HeapNotFoundException(String heapId) {
-        super(MESSAGE_TEMPLATE + heapId);
+        super(tr.localize(LocaleResources.HEAP_ID_NOT_FOUND, heapId));
     }
 
 }
--- a/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ObjectNotFoundException.java	Thu May 16 11:40:43 2013 -0600
+++ b/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ObjectNotFoundException.java	Thu Apr 25 17:27:42 2013 -0600
@@ -37,14 +37,16 @@
 package com.redhat.thermostat.vm.heap.analysis.command.internal;
 
 import com.redhat.thermostat.common.cli.CommandException;
+import com.redhat.thermostat.common.locale.Translate;
+import com.redhat.thermostat.vm.heap.analysis.command.locale.LocaleResources;
 
 @SuppressWarnings("serial")
 public class ObjectNotFoundException extends CommandException {
 
-    private static final String MESSAGE_TEMPLATE = "Object not found: ";
+    private static final Translate<LocaleResources> tr = LocaleResources.createLocalizer();
 
     ObjectNotFoundException(String objectId) {
-        super(MESSAGE_TEMPLATE + objectId);
+        super(tr.localize(LocaleResources.OBJECT_NOT_FOUND_MESSAGE, objectId));
     }
 
 }
--- a/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/SaveHeapDumpToFileCommand.java	Thu May 16 11:40:43 2013 -0600
+++ b/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/SaveHeapDumpToFileCommand.java	Thu Apr 25 17:27:42 2013 -0600
@@ -113,7 +113,7 @@
                     ctx.getConsole().getOutput().println(translator.localize(LocaleResources.COMMAND_SAVE_HEAP_DUMP_ERROR_SAVING, e.getMessage()));
                 }
             } else {
-                ctx.getConsole().getOutput().println(translator.localize(LocaleResources.HEAP_ID_NOT_FOUND, heapId));
+                throw new HeapNotFoundException(heapId);
             }
         } catch (IOException e) {
             throw new CommandException(translator.localize(LocaleResources.COMMAND_SAVE_HEAP_DUMP_ERROR_CLOSING_STREAM, e.getMessage()));
--- a/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ShowHeapHistogramCommand.java	Thu May 16 11:40:43 2013 -0600
+++ b/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ShowHeapHistogramCommand.java	Thu Apr 25 17:27:42 2013 -0600
@@ -89,13 +89,12 @@
 
         HeapInfo heapInfo = heapDAO.getHeapInfo(heapId);
         if (heapInfo == null) {
-            ctx.getConsole().getOutput().println(translator.localize(LocaleResources.HEAP_ID_NOT_FOUND, heapId));
-            return;
+            throw new HeapNotFoundException(heapId);
         }
 
         ObjectHistogram histogram = heapDAO.getHistogram(heapInfo);
         if (histogram == null) {
-            ctx.getConsole().getOutput().println(translator.localize(LocaleResources.HEAP_ID_NOT_FOUND, heapId));
+            ctx.getConsole().getOutput().println(translator.localize(LocaleResources.ERROR_READING_HISTOGRAM_MESSAGE, heapId));
             return;
         } else {
             printHeapHistogram(histogram, ctx.getConsole().getOutput());
--- a/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/locale/LocaleResources.java	Thu May 16 11:40:43 2013 -0600
+++ b/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/locale/LocaleResources.java	Thu Apr 25 17:27:42 2013 -0600
@@ -70,6 +70,9 @@
     COMMAND_OBJECT_INFO_REFERENCES,
     COMMAND_OBJECT_INFO_REFERRERS,
 
+    OBJECT_NOT_FOUND_MESSAGE,
+    ERROR_READING_HISTOGRAM_MESSAGE,
+
     COMMAND_SAVE_HEAP_DUMP_SAVED_TO_FILE,
     COMMAND_SAVE_HEAP_DUMP_ERROR_SAVING,
     COMMAND_SAVE_HEAP_DUMP_ERROR_CLOSING_STREAM,
--- a/vm-heap-analysis/command/src/main/resources/com/redhat/thermostat/vm/heap/analysis/command/locale/strings.properties	Thu May 16 11:40:43 2013 -0600
+++ b/vm-heap-analysis/command/src/main/resources/com/redhat/thermostat/vm/heap/analysis/command/locale/strings.properties	Thu Apr 25 17:27:42 2013 -0600
@@ -29,6 +29,10 @@
 COMMAND_OBJECT_INFO_REFERENCES = References:
 COMMAND_OBJECT_INFO_REFERRERS = Referrers:
 
+OBJECT_NOT_FOUND_MESSAGE = Object not found: 
+ERROR_READING_HISTOGRAM_MESSAGE = Unexpected error while reading histogram for heap ID: {0}
+
 COMMAND_SAVE_HEAP_DUMP_SAVED_TO_FILE = Saved heap dump to {0}
 COMMAND_SAVE_HEAP_DUMP_ERROR_SAVING = error saving heap to file: {0}
-COMMAND_SAVE_HEAP_DUMP_ERROR_CLOSING_STREAM = error closing heap stream: {0}
\ No newline at end of file
+COMMAND_SAVE_HEAP_DUMP_ERROR_CLOSING_STREAM = error closing heap stream: {0}
+
--- a/vm-heap-analysis/command/src/test/java/com/redhat/thermostat/vm/heap/analysis/command/internal/FindObjectsCommandTest.java	Thu May 16 11:40:43 2013 -0600
+++ b/vm-heap-analysis/command/src/test/java/com/redhat/thermostat/vm/heap/analysis/command/internal/FindObjectsCommandTest.java	Thu Apr 25 17:27:42 2013 -0600
@@ -38,6 +38,7 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import static org.mockito.Matchers.isA;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -182,9 +183,12 @@
         SimpleArguments args = new SimpleArguments();
         args.addArgument("heapId", INVALID_HEAP_ID);
 
-        cmd.run(factory.createContext(args));
-
-        assertEquals("Heap ID not found: " + INVALID_HEAP_ID + "\n", factory.getOutput());
+        try {
+            cmd.run(factory.createContext(args));
+            fail();
+        } catch (CommandException e) {
+            assertEquals("Heap ID not found: " + INVALID_HEAP_ID, e.getMessage());
+        }
     }
 }
 
--- a/vm-heap-analysis/command/src/test/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ShowHeapHistogramCommandTest.java	Thu May 16 11:40:43 2013 -0600
+++ b/vm-heap-analysis/command/src/test/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ShowHeapHistogramCommandTest.java	Thu Apr 25 17:27:42 2013 -0600
@@ -124,9 +124,12 @@
         SimpleArguments args = new SimpleArguments();
         args.addArgument("heapId", BAD_HEAP_ID);
 
-        command.run(factory.createContext(args));
-
-        assertEquals("Heap ID not found: " + BAD_HEAP_ID + "\n", factory.getOutput());
+        try {
+            command.run(factory.createContext(args));
+            fail();
+        } catch (CommandException e) {
+            assertEquals("Heap ID not found: " + BAD_HEAP_ID, e.getMessage());
+        }
     }
     
     @Test