You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sqoop.apache.org by bl...@apache.org on 2012/11/14 19:22:22 UTC

[2/4] git commit: SQOOP-694 Introduce client "verbose" mode (Jarek Jarcec Cecho)

SQOOP-694 Introduce client "verbose" mode
(Jarek Jarcec Cecho)


Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/18b5d70d
Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/18b5d70d
Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/18b5d70d

Branch: refs/heads/sqoop2
Commit: 18b5d70d31397b2b72856c88a73eb7689c3b6bbe
Parents: 211c678
Author: Bilung Lee <bl...@apache.org>
Authored: Tue Nov 13 20:49:41 2012 -0800
Committer: Bilung Lee <bl...@apache.org>
Committed: Tue Nov 13 20:49:41 2012 -0800

----------------------------------------------------------------------
 .../org/apache/sqoop/client/core/Environment.java  |   12 ++
 .../org/apache/sqoop/client/shell/SetCommand.java  |   14 ++-
 .../sqoop/client/shell/SetOptionFunction.java      |   83 +++++++++++++++
 .../sqoop/client/utils/ThrowableDisplayer.java     |   33 +++---
 4 files changed, 122 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/18b5d70d/client/src/main/java/org/apache/sqoop/client/core/Environment.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/core/Environment.java b/client/src/main/java/org/apache/sqoop/client/core/Environment.java
index aa8c989..190af37 100644
--- a/client/src/main/java/org/apache/sqoop/client/core/Environment.java
+++ b/client/src/main/java/org/apache/sqoop/client/core/Environment.java
@@ -30,9 +30,12 @@ public final class Environment
   private static String serverPort;
   private static String serverWebapp;
 
+  private static boolean verbose;
+
   private static final String HOST_DEFAULT = "vm-sqoop2";
   private static final String PORT_DEFAULT = "8080";
   private static final String WEBAPP_DEFAULT = "sqoop";
+  private static final boolean VERBOSE_DEFAULT = false;
 
   private static ResourceBundle resourceBundle;
 
@@ -40,6 +43,7 @@ public final class Environment
     serverHost = HOST_DEFAULT;
     serverPort = PORT_DEFAULT;
     serverWebapp = WEBAPP_DEFAULT;
+    verbose = VERBOSE_DEFAULT;
 
     resourceBundle =
       ResourceBundle.getBundle(Constants.RESOURCE_NAME, Locale.getDefault());
@@ -76,4 +80,12 @@ public final class Environment
   public static ResourceBundle getResourceBundle() {
     return resourceBundle;
   }
+
+  public static void setVerbose(boolean newValue) {
+    verbose = newValue;
+  }
+
+  public static boolean isVerboose() {
+    return verbose;
+  }
 }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/18b5d70d/client/src/main/java/org/apache/sqoop/client/shell/SetCommand.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/shell/SetCommand.java b/client/src/main/java/org/apache/sqoop/client/shell/SetCommand.java
index 377c827..20c8090 100644
--- a/client/src/main/java/org/apache/sqoop/client/shell/SetCommand.java
+++ b/client/src/main/java/org/apache/sqoop/client/shell/SetCommand.java
@@ -26,10 +26,11 @@ import org.codehaus.groovy.tools.shell.Shell;
 public class SetCommand extends SqoopCommand
 {
   private SetServerFunction serverFunction;
+  private SetOptionFunction optionFunction;
 
   protected SetCommand(Shell shell) {
     super(shell, "set", "\\st",
-        new String[] {"server", "connector"},
+        new String[] {"server", "option"},
         "Set", "info");
   }
 
@@ -49,12 +50,15 @@ public class SetCommand extends SqoopCommand
       }
       return serverFunction.execute(args);
 
-    } else if (func.equals("client")) {
-      return null;
+    } else if (func.equals("option")) {
+      if (optionFunction == null) {
+        optionFunction = new SetOptionFunction(io);
+      }
+      return optionFunction.execute(args);
 
     } else {
       String msg = "Usage: set " + getUsage();
-      throw new SqoopException(ClientError.CLIENT_0002, msg);    
+      throw new SqoopException(ClientError.CLIENT_0002, msg);
     }
   }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/sqoop/blob/18b5d70d/client/src/main/java/org/apache/sqoop/client/shell/SetOptionFunction.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/shell/SetOptionFunction.java b/client/src/main/java/org/apache/sqoop/client/shell/SetOptionFunction.java
new file mode 100644
index 0000000..3764306
--- /dev/null
+++ b/client/src/main/java/org/apache/sqoop/client/shell/SetOptionFunction.java
@@ -0,0 +1,83 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sqoop.client.shell;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.sqoop.client.core.Environment;
+import org.codehaus.groovy.tools.shell.IO;
+
+import java.util.List;
+
+/**
+ *
+ */
+public class SetOptionFunction extends SqoopFunction {
+
+  public static final String NAME = "name";
+  public static final String VALUE = "value";
+
+  private IO io;
+
+  @SuppressWarnings("static-access")
+  protected SetOptionFunction(IO io) {
+    this.io = io;
+
+    this.addOption(OptionBuilder.hasArg()
+      .withDescription("Client option name")
+      .withLongOpt(NAME)
+      .create(NAME.charAt(0)));
+    this.addOption(OptionBuilder.hasArg()
+      .withDescription("New option value")
+      .withLongOpt(VALUE)
+      .create(VALUE.charAt(0)));
+  }
+
+  public Object execute(List<String> args) {
+    CommandLine line = parseOptions(this, 1, args);
+    if (!line.hasOption(NAME)) {
+      io.out.println("Required argument --name is missing.");
+      return null;
+    }
+    if (!line.hasOption(VALUE)) {
+      io.out.println("Required argument --value is missing.");
+      return null;
+    }
+
+    handleOptionSetting(line.getOptionValue(NAME), line.getOptionValue(VALUE));
+
+    io.out.println();
+    return null;
+  }
+
+  private void handleOptionSetting(String name, String value) {
+    if(name.equals("verbose")) {
+      boolean newValue = false;
+
+      if(value.equals("1") || value.equals("true")) {
+        newValue = true;
+      }
+
+      Environment.setVerbose(newValue);
+      io.out.println("Verbose option was changed to " + newValue);
+      return;
+    }
+
+    io.out.println("Unknown option " + name + ". Ignoring...");
+  }
+}

http://git-wip-us.apache.org/repos/asf/sqoop/blob/18b5d70d/client/src/main/java/org/apache/sqoop/client/utils/ThrowableDisplayer.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/utils/ThrowableDisplayer.java b/client/src/main/java/org/apache/sqoop/client/utils/ThrowableDisplayer.java
index c8f8223..45c78fb 100644
--- a/client/src/main/java/org/apache/sqoop/client/utils/ThrowableDisplayer.java
+++ b/client/src/main/java/org/apache/sqoop/client/utils/ThrowableDisplayer.java
@@ -19,6 +19,7 @@ package org.apache.sqoop.client.utils;
 
 import groovy.lang.MissingPropertyException;
 import org.apache.sqoop.client.core.ClientError;
+import org.apache.sqoop.client.core.Environment;
 import org.apache.sqoop.common.SqoopException;
 import org.codehaus.groovy.tools.shell.IO;
 
@@ -57,12 +58,12 @@ public class ThrowableDisplayer {
     if(t instanceof SqoopException
       && ((SqoopException)t).getErrorCode() == ClientError.CLIENT_0006) {
       io.out.print("@|red Server has returned exception: |@");
-      printThrowable(io, t.getCause());
+      printThrowable(io, t.getCause(), Environment.isVerboose());
     } else if(t.getClass() == MissingPropertyException.class) {
       io.out.print("@|red Unknown command: |@");
       io.out.println(t.getMessage());
     } else {
-      printThrowable(io, t);
+      printThrowable(io, t, Environment.isVerboose());
     }
   }
 
@@ -72,26 +73,28 @@ public class ThrowableDisplayer {
    * @param io IO object to use for generating output
    * @param t Throwable to display
    */
-  protected static void printThrowable(IO io, Throwable t) {
+  protected static void printThrowable(IO io, Throwable t, boolean verbose) {
     io.out.print("@|red Exception: |@");
     io.out.print(t.getClass().getName());
     io.out.print(" @|red Message: |@");
     io.out.print(t.getMessage());
     io.out.println();
 
-    io.out.println("Stack trace:");
-    for(StackTraceElement e : t.getStackTrace()) {
-      io.out.print("\t @|bold at |@ ");
-      io.out.print(e.getClassName());
-      io.out.print(" (@|bold " + e.getFileName() + ":"
-        + e.getLineNumber() + ") |@ ");
-      io.out.println();
-    }
+    if(verbose) {
+      io.out.println("Stack trace:");
+      for(StackTraceElement e : t.getStackTrace()) {
+        io.out.print("\t @|bold at |@ ");
+        io.out.print(e.getClassName());
+        io.out.print(" (@|bold " + e.getFileName() + ":"
+          + e.getLineNumber() + ") |@ ");
+        io.out.println();
+      }
 
-    Throwable cause = t.getCause();
-    if(cause != null) {
-      io.out.print("Caused by: ");
-      printThrowable(io, cause);
+      Throwable cause = t.getCause();
+      if(cause != null) {
+        io.out.print("Caused by: ");
+        printThrowable(io, cause, verbose);
+      }
     }
   }