geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r699599 - /geronimo/gshell/trunk/gshell-commands/gshell-remote/src/main/java/org/apache/geronimo/gshell/commands/remote/RshAction.java
Date Sat, 27 Sep 2008 08:03:06 GMT
Author: jdillon
Date: Sat Sep 27 01:03:06 2008
New Revision: 699599

URL: http://svn.apache.org/viewvc?rev=699599&view=rev
Log:
Validate that prompted username & password are not null and not empty strings using new
PromptReader.Validator muck
Moved prompting up before any remote client is created, incase validation of prompted data
fails

Modified:
    geronimo/gshell/trunk/gshell-commands/gshell-remote/src/main/java/org/apache/geronimo/gshell/commands/remote/RshAction.java

Modified: geronimo/gshell/trunk/gshell-commands/gshell-remote/src/main/java/org/apache/geronimo/gshell/commands/remote/RshAction.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-remote/src/main/java/org/apache/geronimo/gshell/commands/remote/RshAction.java?rev=699599&r1=699598&r2=699599&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-remote/src/main/java/org/apache/geronimo/gshell/commands/remote/RshAction.java
(original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-remote/src/main/java/org/apache/geronimo/gshell/commands/remote/RshAction.java
Sat Sep 27 01:03:06 2008
@@ -70,37 +70,74 @@
         this.container = container;
     }
 
-    public Object execute(final CommandContext context) throws Exception {
-        assert context != null;
-        IO io = context.getIo();
-        MessageSource messages = context.getCommand().getMessages();
+    /**
+     * Helper to validate that prompted username or password is not null or empty.
+     */
+    private class UsernamePasswordValidator
+        implements PromptReader.Validator
+    {
+        private String type;
 
-        io.info(messages.format("info.connecting", remote));
+        private int count = 0;
 
-        RshClient client = container.getBean(RshClient.class);
+        private int max = 3;
 
-        log.debug("Created client: {}", client);
-        
-        client.connect(remote, local);
+        public UsernamePasswordValidator(final String type) {
+            assert type != null;
 
-        io.info(messages.getMessage("info.connected"));
+            this.type = type;
+        }
+
+        public boolean isValid(final String value) {
+            count++;
+
+            if (value != null && value.trim().length() > 0) {
+                return true;
+            }
+
+            if (count >= max) {
+                throw new RuntimeException("Too many attempts; failed to prompt user for
" + type + " after " + max + " tries");
+            }
+
+            return false;
+        }
+    }
+
+    public Object execute(final CommandContext context) throws Exception {
+        assert context != null;
+        IO io = context.getIo();
+        MessageSource messages = context.getCommand().getMessages();
 
         // If the username/password was not configured via cli, then prompt the user for
the values
         if (username == null || password == null) {
             PromptReader prompter = new PromptReader(io);
+            String text;
 
             if (username == null) {
-                username = prompter.readLine(messages.getMessage("prompt.username") + ":
");
+                text = messages.getMessage("prompt.username");
+                username = prompter.readLine(text + ": ", new UsernamePasswordValidator(text));
             }
+            assert username != null;
+            assert username.length() != 0;
 
             if (password == null) {
-                password = prompter.readPassword(messages.getMessage("prompt.password") +
": ");
+                text = messages.getMessage("prompt.password");
+                password = prompter.readLine(text + ": ", new UsernamePasswordValidator(text));
             }
-
-            //
-            // TODO: Handle null inputs... Maybe add this support to PromptReader, then after
n tries throw an exception?
-            //
+            assert password != null;
+            assert password.length() != 0;
         }
+        
+        io.info(messages.format("info.connecting", remote));
+
+        // Create the client from prototype
+        RshClient client = container.getBean(RshClient.class);
+
+        log.debug("Created client: {}", client);
+        
+        client.connect(remote, local);
+
+        io.info(messages.getMessage("info.connected"));
 
         client.login(username, password);
 



Mime
View raw message