You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ji...@apache.org on 2019/02/22 11:47:37 UTC

[incubator-iotdb] branch fix_sonar_bug updated: refactor WinClient

This is an automated email from the ASF dual-hosted git repository.

jiangtian pushed a commit to branch fix_sonar_bug
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git


The following commit(s) were added to refs/heads/fix_sonar_bug by this push:
     new 4fb9c46  refactor WinClient
4fb9c46 is described below

commit 4fb9c469af839cf56f0feb09814c4718a5eaaf25
Author: 江天 <jt...@163.com>
AuthorDate: Fri Feb 22 19:46:42 2019 +0800

    refactor WinClient
---
 .../org/apache/iotdb/cli/client/WinClient.java     | 149 +++++++++++++--------
 1 file changed, 91 insertions(+), 58 deletions(-)

diff --git a/iotdb-cli/src/main/java/org/apache/iotdb/cli/client/WinClient.java b/iotdb-cli/src/main/java/org/apache/iotdb/cli/client/WinClient.java
index 4e9e2e5..815f3ff 100644
--- a/iotdb-cli/src/main/java/org/apache/iotdb/cli/client/WinClient.java
+++ b/iotdb-cli/src/main/java/org/apache/iotdb/cli/client/WinClient.java
@@ -31,9 +31,12 @@ import org.apache.commons.cli.ParseException;
 import org.apache.iotdb.cli.exception.ArgsErrorException;
 import org.apache.iotdb.jdbc.Config;
 import org.apache.iotdb.jdbc.IoTDBConnection;
+import org.apache.thrift.TException;
 
 public class WinClient extends AbstractClient {
 
+  private static CommandLine commandLine;
+
   /**
    * main function.
    *
@@ -44,12 +47,12 @@ public class WinClient extends AbstractClient {
     Options options = createOptions();
     HelpFormatter hf = new HelpFormatter();
     hf.setWidth(MAX_HELP_CONSOLE_WIDTH);
-    CommandLine commandLine = null;
-    CommandLineParser parser = new DefaultParser();
-    String[] newArgs = null;
+    commandLine = null;
+
+    String[] newArgs;
 
     if (args == null || args.length == 0) {
-      System.out.println("Require more params input, please check the following hint.");
+      println("Require more params input, please check the following hint.");
       hf.printHelp(IOTDB_CLI_PREFIX, options, true);
       return;
     }
@@ -58,33 +61,57 @@ public class WinClient extends AbstractClient {
 
     newArgs = removePasswordArgs(args);
 
+    boolean continues = parseCommandLine(options, newArgs, hf);
+    if (!continues) {
+      return;
+    }
+
+    serve();
+  }
+
+  private static String readPassword() {
+    Console c = System.console();
+    if (c == null) { // IN ECLIPSE IDE
+      print(IOTDB_CLI_PREFIX + "> please input password: ");
+      Scanner scanner = new Scanner(System.in);
+      return scanner.nextLine();
+    } else { // Outside Eclipse IDE
+      return new String(c.readPassword(IOTDB_CLI_PREFIX + "> please input password: "));
+    }
+  }
+
+  private static boolean parseCommandLine(Options options, String[] newArgs, HelpFormatter hf) {
     try {
+      CommandLineParser parser = new DefaultParser();
       commandLine = parser.parse(options, newArgs);
       if (commandLine.hasOption(HELP_ARGS)) {
         hf.printHelp(IOTDB_CLI_PREFIX, options, true);
-        return;
+        return false;
       }
       if (commandLine.hasOption(ISO8601_ARGS)) {
         setTimeFormat("long");
       }
       if (commandLine.hasOption(MAX_PRINT_ROW_COUNT_ARGS)) {
-        try {
-          maxPrintRowCount = Integer.valueOf(commandLine.getOptionValue(MAX_PRINT_ROW_COUNT_ARGS));
-          if (maxPrintRowCount < 0) {
-            maxPrintRowCount = Integer.MAX_VALUE;
-          }
-        } catch (NumberFormatException e) {
-          System.out.println(
-              IOTDB_CLI_PREFIX + "> error format of max print row count, it should be number");
-          return;
+        maxPrintRowCount = Integer.valueOf(commandLine.getOptionValue(MAX_PRINT_ROW_COUNT_ARGS));
+        if (maxPrintRowCount < 0) {
+          maxPrintRowCount = Integer.MAX_VALUE;
         }
       }
     } catch (ParseException e) {
-      System.out.println("Require more params input, please check the following hint.");
+      println("Require more params input, please check the following hint.");
       hf.printHelp(IOTDB_CLI_PREFIX, options, true);
-      return;
+      handleException(e);
+      return false;
+    } catch (NumberFormatException e) {
+      println(
+          IOTDB_CLI_PREFIX + "> error format of max print row count, it should be number");
+      handleException(e);
+      return false;
     }
+    return true;
+  }
 
+  private static void serve() {
     try (Scanner scanner = new Scanner(System.in)) {
       host = checkRequiredArg(HOST_ARGS, HOST_NAME, commandLine, false, host);
       port = checkRequiredArg(PORT_ARGS, PORT_NAME, commandLine, false, port);
@@ -93,53 +120,59 @@ public class WinClient extends AbstractClient {
       if (password == null) {
         password = readPassword();
       }
-      try (IoTDBConnection connection = (IoTDBConnection) DriverManager
-          .getConnection(Config.IOTDB_URL_PREFIX + host + ":" + port + "/", username, password)) {
-        properties = connection.getServerProperties();
-        AGGREGRATE_TIME_LIST.addAll(properties.getSupportedTimeAggregationOperations());
-        displayLogo(properties.getVersion());
-        System.out.println(IOTDB_CLI_PREFIX + "> login successfully");
-        while (true) {
-          System.out.print(IOTDB_CLI_PREFIX + "> ");
-          String s = scanner.nextLine();
-          if (s != null) {
-            String[] cmds = s.trim().split(";");
-            for (int i = 0; i < cmds.length; i++) {
-              String cmd = cmds[i];
-              if (cmd != null && !"".equals(cmd.trim())) {
-                OperationResult result = handleInputCmd(cmd, connection);
-                switch (result) {
-                  case RETURN_OPER:
-                    return;
-                  case CONTINUE_OPER:
-                    continue;
-                  default:
-                    break;
-                }
-              }
-            }
-          }
-        }
-      } catch (SQLException e) {
-        System.out.println(String
-            .format("%s> %s Host is %s, port is %s.", IOTDB_CLI_PREFIX, e.getMessage(), host,
-                port));
-      }
+
+      receiveCommands(scanner);
     } catch (ArgsErrorException e) {
-      System.out.println(IOTDB_CLI_PREFIX + "> input params error because" + e.getMessage());
+      println(IOTDB_CLI_PREFIX + "> input params error because" + e.getMessage());
+      handleException(e);
     } catch (Exception e) {
-      System.out.println(IOTDB_CLI_PREFIX + "> exit client with error " + e.getMessage());
+      println(IOTDB_CLI_PREFIX + "> exit client with error " + e.getMessage());
+      handleException(e);
     }
   }
 
-  private static String readPassword() {
-    Console c = System.console();
-    if (c == null) { // IN ECLIPSE IDE
-      System.out.print(IOTDB_CLI_PREFIX + "> please input password: ");
-      Scanner scanner = new Scanner(System.in);
-      return scanner.nextLine();
-    } else { // Outside Eclipse IDE
-      return new String(c.readPassword(IOTDB_CLI_PREFIX + "> please input password: "));
+  private static void receiveCommands(Scanner scanner) throws TException {
+    try (IoTDBConnection connection = (IoTDBConnection) DriverManager
+        .getConnection(Config.IOTDB_URL_PREFIX + host + ":" + port + "/", username, password)) {
+      properties = connection.getServerProperties();
+      AGGREGRATE_TIME_LIST.addAll(properties.getSupportedTimeAggregationOperations());
+      displayLogo(properties.getVersion());
+      println(IOTDB_CLI_PREFIX + "> login successfully");
+      while (true) {
+        print(IOTDB_CLI_PREFIX + "> ");
+        String s = scanner.nextLine();
+        boolean continues = processCommand(s, connection);
+        if (!continues) {
+          break;
+        }
+      }
+    } catch (SQLException e) {
+      println(String
+          .format("%s> %s Host is %s, port is %s.", IOTDB_CLI_PREFIX, e.getMessage(), host,
+              port));
+      handleException(e);
+    }
+  }
+
+  private static boolean processCommand(String s, IoTDBConnection connection) {
+    if (s == null) {
+      return true;
+    }
+    String[] cmds = s.trim().split(";");
+    for (int i = 0; i < cmds.length; i++) {
+      String cmd = cmds[i];
+      if (cmd != null && !"".equals(cmd.trim())) {
+        OperationResult result = handleInputCmd(cmd, connection);
+        switch (result) {
+          case RETURN_OPER:
+            return false;
+          case CONTINUE_OPER:
+            continue;
+          default:
+            break;
+        }
+      }
     }
+    return true;
   }
 }