felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r882582 - /felix/trunk/karaf/client/src/main/java/org/apache/felix/karaf/client/Main.java
Date Fri, 20 Nov 2009 14:43:19 GMT
Author: gnodet
Date: Fri Nov 20 14:43:18 2009
New Revision: 882582

URL: http://svn.apache.org/viewvc?rev=882582&view=rev
Log:
FELIX-1882: karaf-client should have the option to retry connection establishment

Modified:
    felix/trunk/karaf/client/src/main/java/org/apache/felix/karaf/client/Main.java

Modified: felix/trunk/karaf/client/src/main/java/org/apache/felix/karaf/client/Main.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/client/src/main/java/org/apache/felix/karaf/client/Main.java?rev=882582&r1=882581&r2=882582&view=diff
==============================================================================
--- felix/trunk/karaf/client/src/main/java/org/apache/felix/karaf/client/Main.java (original)
+++ felix/trunk/karaf/client/src/main/java/org/apache/felix/karaf/client/Main.java Fri Nov
20 14:43:18 2009
@@ -26,6 +26,7 @@
 import org.apache.sshd.SshClient;
 import org.apache.sshd.client.channel.ChannelShell;
 import org.apache.sshd.client.future.ConnectFuture;
+import org.apache.sshd.common.RuntimeSshException;
 
 import org.fusesource.jansi.AnsiConsole;
 import org.slf4j.impl.SimpleLogger;
@@ -42,6 +43,8 @@
         String password = "karaf";
         StringBuilder sb = new StringBuilder();
         int level = 1;
+        int retryAttempts = 0;
+        int retryDelay = 2;
 
         for (int i = 0; i < args.length; i++) {
             if (args[i].charAt(0) == '-') {
@@ -55,6 +58,10 @@
                     password = args[++i];
                 } else if (args[i].equals("-v")) {
                     level++;
+                } else if (args[i].equals("-r")) {
+                    retryAttempts = Integer.parseInt(args[++i]);
+                } else if (args[i].equals("-d")) {
+                    retryDelay = Integer.parseInt(args[++i]);
                 } else if (args[i].equals("--help")) {
                     System.out.println("Apache Felix Karaf client");
                     System.out.println("  -a [port]     specify the port to connect to");
@@ -63,6 +70,8 @@
                     System.out.println("  -p [password] specify the password");
                     System.out.println("  --help        shows this help message");
                     System.out.println("  -v            raise verbosity");
+                    System.out.println("  -r [attempts] retry connection establishment (up
to attempts times)");
+                    System.out.println("  -d [delay]    intra-retry delay (defaults to 2
seconds)");
                     System.out.println("  [commands]    commands to run");
                     System.out.println("If no commands are specified, the client will be
put in an interactive mode");
                     System.exit(0);
@@ -83,9 +92,22 @@
         try {
             client = SshClient.setUpDefaultClient();
             client.start();
-            ConnectFuture future = client.connect(host, port);
-            future.await();
-            ClientSession session = future.getSession();
+            int retries = 0;
+            ClientSession session = null;
+            do {
+                ConnectFuture future = client.connect(host, port);
+                future.await();
+                try { 
+                    session = future.getSession();
+                } catch (RuntimeSshException ex) {
+                    if (retries++ < retryAttempts) {
+                        Thread.sleep(retryDelay * 1000);
+                        System.out.println("retrying (attempt " + retries + ") ...");
+                    } else {
+                        throw ex;
+                    }
+                }
+            } while (session == null);
             session.authPassword(user, password);
             ClientChannel channel;
 			if (sb.length() > 0) {



Mime
View raw message