You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@accumulo.apache.org by ec...@apache.org on 2013/06/06 15:22:55 UTC

svn commit: r1490273 - in /accumulo/branches/ACCUMULO-118: ./ assemble/ core/ core/src/main/java/org/apache/accumulo/core/client/mock/ core/src/main/java/org/apache/accumulo/core/util/shell/ examples/ fate/src/main/java/org/apache/accumulo/fate/ fate/s...

Author: ecn
Date: Thu Jun  6 13:22:54 2013
New Revision: 1490273

URL: http://svn.apache.org/r1490273
Log:
ACCUMULO-118 merge trunk into sandbox

Added:
    accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptionsJC.java
      - copied unchanged from r1490272, accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptionsJC.java
Modified:
    accumulo/branches/ACCUMULO-118/   (props changed)
    accumulo/branches/ACCUMULO-118/assemble/   (props changed)
    accumulo/branches/ACCUMULO-118/core/   (props changed)
    accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/client/mock/MockShell.java
    accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java
    accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptions.java
    accumulo/branches/ACCUMULO-118/examples/   (props changed)
    accumulo/branches/ACCUMULO-118/fate/src/main/java/org/apache/accumulo/fate/ZooStore.java   (props changed)
    accumulo/branches/ACCUMULO-118/fate/src/main/java/org/apache/accumulo/fate/zookeeper/ZooSession.java   (props changed)
    accumulo/branches/ACCUMULO-118/pom.xml   (props changed)
    accumulo/branches/ACCUMULO-118/proxy/README   (props changed)
    accumulo/branches/ACCUMULO-118/server/   (props changed)
    accumulo/branches/ACCUMULO-118/src/   (props changed)

Propchange: accumulo/branches/ACCUMULO-118/
------------------------------------------------------------------------------
  Merged /accumulo/trunk:r1489983-1490272

Propchange: accumulo/branches/ACCUMULO-118/assemble/
------------------------------------------------------------------------------
  Merged /accumulo/trunk/assemble:r1489983-1490272

Propchange: accumulo/branches/ACCUMULO-118/core/
------------------------------------------------------------------------------
  Merged /accumulo/trunk/core:r1489983-1490272

Modified: accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/client/mock/MockShell.java
URL: http://svn.apache.org/viewvc/accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/client/mock/MockShell.java?rev=1490273&r1=1490272&r2=1490273&view=diff
==============================================================================
--- accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/client/mock/MockShell.java (original)
+++ accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/client/mock/MockShell.java Thu Jun  6 13:22:54 2013
@@ -17,7 +17,6 @@
 package org.apache.accumulo.core.client.mock;
 
 import java.io.ByteArrayInputStream;
-import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -25,7 +24,7 @@ import java.io.OutputStream;
 import jline.console.ConsoleReader;
 
 import org.apache.accumulo.core.util.shell.Shell;
-import org.apache.commons.cli.CommandLine;
+import org.apache.accumulo.core.util.shell.ShellOptionsJC;
 
 /**
  * An Accumulo Shell implementation that allows a developer to attach an InputStream and Writer to the Shell for testing purposes.
@@ -63,7 +62,7 @@ public class MockShell extends Shell {
   }
   
   @Override
-  protected void setInstance(CommandLine cl) {
+  protected void setInstance(ShellOptionsJC options) {
     // We always want a MockInstance for this test
     instance = new MockInstance();
   }
@@ -77,7 +76,7 @@ public class MockShell extends Shell {
       printInfo();
     
     if (execFile != null) {
-      java.util.Scanner scanner = new java.util.Scanner(new File(execFile));
+      java.util.Scanner scanner = new java.util.Scanner(execFile);
       try {
         while (scanner.hasNextLine() && !hasExited()) {
           execCommand(scanner.nextLine(), true, isVerbose());

Modified: accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java
URL: http://svn.apache.org/viewvc/accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java?rev=1490273&r1=1490272&r2=1490273&view=diff
==============================================================================
--- accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java (original)
+++ accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java Thu Jun  6 13:22:54 2013
@@ -47,7 +47,6 @@ import org.apache.accumulo.core.client.T
 import org.apache.accumulo.core.client.ZooKeeperInstance;
 import org.apache.accumulo.core.client.mock.MockInstance;
 import org.apache.accumulo.core.client.security.tokens.AuthenticationToken;
-import org.apache.accumulo.core.client.security.tokens.AuthenticationToken.Properties;
 import org.apache.accumulo.core.client.security.tokens.PasswordToken;
 import org.apache.accumulo.core.conf.AccumuloConfiguration;
 import org.apache.accumulo.core.conf.Property;
@@ -145,7 +144,6 @@ import org.apache.accumulo.fate.zookeepe
 import org.apache.commons.cli.BasicParser;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.HelpFormatter;
-import org.apache.commons.cli.MissingArgumentException;
 import org.apache.commons.cli.MissingOptionException;
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
@@ -154,6 +152,9 @@ import org.apache.hadoop.fs.Path;
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 
+import com.beust.jcommander.JCommander;
+import com.beust.jcommander.ParameterException;
+
 /**
  * A convenient console interface to perform basic accumulo functions Includes auto-complete, help, and quoted strings with escape sequences
  */
@@ -167,7 +168,6 @@ public class Shell extends ShellOptions 
   public static final String HISTORY_DIR_NAME = ".accumulo";
   public static final String HISTORY_FILE_NAME = "shell_history.txt";
   private static final String SHELL_DESCRIPTION = "Shell - Apache Accumulo Interactive Shell";
-  private static final String DEFAULT_AUTH_TIMEOUT = "60"; // in minutes
   
   protected int exitCode = 0;
   private String tableName;
@@ -190,7 +190,7 @@ public class Shell extends ShellOptions 
   private boolean exit = false;
   
   // file to execute commands from
-  protected String execFile = null;
+  protected File execFile = null;
   // single command to execute from the command line
   protected String execCommand = null;
   protected boolean verbose = true;
@@ -219,99 +219,97 @@ public class Shell extends ShellOptions 
   
   // Not for client use
   public boolean config(String... args) {
+    ShellOptionsJC options = new ShellOptionsJC();
+    JCommander jc = new JCommander();
     
-    CommandLine cl;
+    jc.setProgramName("accumulo shell");
+    jc.addObject(options);
     try {
-      cl = new BasicParser().parse(opts, args);
-      if (cl.getArgs().length > 0)
-        throw new ParseException("Unrecognized arguments: " + cl.getArgList());
-      
-      if (cl.hasOption(helpOpt.getOpt())) {
-        configError = true;
-        printHelp("shell", SHELL_DESCRIPTION, opts);
-        return true;
-      }
-      
-      setDebugging(cl.hasOption(debugOption.getLongOpt()));
-      authTimeout = Integer.parseInt(cl.getOptionValue(authTimeoutOpt.getLongOpt(), DEFAULT_AUTH_TIMEOUT)) * 60 * 1000;
-      disableAuthTimeout = cl.hasOption(disableAuthTimeoutOpt.getLongOpt());
-      
-      if (cl.hasOption(zooKeeperInstance.getOpt()) && cl.getOptionValues(zooKeeperInstance.getOpt()).length != 2)
-        throw new MissingArgumentException(zooKeeperInstance);
-      
-    } catch (Exception e) {
+      jc.parse(args);
+    } catch (ParameterException e) {
       configError = true;
-      printException(e);
-      printHelp("shell", SHELL_DESCRIPTION, opts);
+    }
+    
+    if (options.isHelpEnabled()) {
+      configError = true;
+    }
+    
+    if (!configError && options.getUnrecognizedOptions() != null) {
+      configError = true;
+      logError("Unrecognized Options: " + options.getUnrecognizedOptions().toString());
+    }
+    
+    if (configError) {
+      jc.usage();
       return true;
     }
     
+    setDebugging(options.isDebugEnabled());
+    authTimeout = options.getAuthTimeout() * 60 * 1000; // convert minutes to milliseconds
+    disableAuthTimeout = options.isAuthTimeoutDisabled();
+    
     // get the options that were parsed
-    String sysUser = System.getProperty("user.name");
-    if (sysUser == null)
-      sysUser = "root";
-    String user = cl.getOptionValue(usernameOption.getOpt(), sysUser);
-    
-    String passw = cl.getOptionValue(passwOption.getOpt(), null);
-    tabCompletion = !cl.hasOption(tabCompleteOption.getLongOpt());
-    String[] loginOptions = cl.getOptionValues(loginOption.getOpt());
+    String user = options.getUsername();
+    String password = options.getPassword();
+    
+    tabCompletion = !options.isTabCompletionDisabled();
     
     // Use a fake (Mock), ZK, or HdfsZK Accumulo instance
-    setInstance(cl);
+    setInstance(options);
+    
+    // AuthenticationToken options
+    token = options.getAuthenticationToken();
+    Map<String,String> loginOptions = options.getTokenProperties();
     
     // process default parameters if unspecified
     try {
-      if (loginOptions != null && !cl.hasOption(tokenOption.getOpt()))
-        throw new IllegalArgumentException("Must supply '-" + tokenOption.getOpt() + "' option with '-" + loginOption.getOpt() + "' option");
+      boolean hasToken = (token != null);
+      boolean hasTokenOptions = loginOptions != null && !loginOptions.isEmpty();
       
-      if (loginOptions == null && cl.hasOption(tokenOption.getOpt()))
-        throw new IllegalArgumentException("Must supply '-" + loginOption.getOpt() + "' option with '-" + tokenOption.getOpt() + "' option");
-      
-      if (passw != null && cl.hasOption(tokenOption.getOpt()))
-        throw new IllegalArgumentException("Can not supply '-" + passwOption.getOpt() + "' option with '-" + tokenOption.getOpt() + "' option");
+      // Need either both a token and options, or neither, but not just one.
+      if (hasToken != hasTokenOptions) {
+        throw new ParameterException("Must supply either both or neither of '--tokenClass' and '--tokenProperty'");
+      }
       
-      if (user == null)
-        throw new MissingArgumentException(usernameOption);
+      if (hasToken && password != null) {
+        throw new ParameterException("Can not supply '--pass' option with '--tokenClass' option");
+      }
       
-      if (loginOptions != null && cl.hasOption(tokenOption.getOpt())) {
-        Properties props = new Properties();
-        for (String loginOption : loginOptions)
-          for (String lo : loginOption.split(",")) {
-            String[] split = lo.split("=");
-            props.put(split[0], split[1]);
-          }
+      if (hasToken && hasTokenOptions) {
+        // Fully qualified name so we don't shadow java.util.Properties
+        org.apache.accumulo.core.client.security.tokens.AuthenticationToken.Properties props;
+        // and line wrap it because the package name is so long
+        props = new org.apache.accumulo.core.client.security.tokens.AuthenticationToken.Properties();
         
-        this.token = Class.forName(cl.getOptionValue(tokenOption.getOpt())).asSubclass(AuthenticationToken.class).newInstance();
-        this.token.init(props);
+        props.putAllStrings(loginOptions);
+        token.init(props);
       }
       
-      if (!cl.hasOption(fakeOption.getLongOpt())) {
-        DistributedTrace.enable(instance, new ZooReader(instance.getZooKeepers(), instance.getZooKeepersSessionTimeOut()), "shell", InetAddress.getLocalHost()
-            .getHostName());
+      if (!options.isFake()) {
+        ZooReader zr = new ZooReader(instance.getZooKeepers(), instance.getZooKeepersSessionTimeOut());
+        DistributedTrace.enable(instance, zr, "shell", InetAddress.getLocalHost().getHostName());
       }
       
       Runtime.getRuntime().addShutdownHook(new Thread() {
         @Override
-        public void start() {
+        public void run() {
           reader.getTerminal().setEchoEnabled(true);
         }
       });
       
-      if (passw != null) {
-        this.token = new PasswordToken(passw);
-      }
-      
-      if (this.token == null) {
-        passw = readMaskedLine("Password: ", '*');
-        if (passw != null)
-          this.token = new PasswordToken(passw);
+      if (!hasToken) {
+        if (password == null) {
+          password = reader.readLine("Password: ", '*');
+        }
+        
+        if (password == null) {
+          // User cancel, e.g. Ctrl-D pressed
+          throw new ParameterException("No password or token option supplied");
+        } else {
+          this.token = new PasswordToken(password);
+        }
       }
       
-      if (this.token == null) {
-        reader.println();
-        throw new MissingArgumentException("No password or token option supplied");
-      } // user canceled
-      
       this.setTableName("");
       this.principal = user;
       connector = instance.getConnector(this.principal, token);
@@ -322,14 +320,15 @@ public class Shell extends ShellOptions 
     }
     
     // decide whether to execute commands from a file and quit
-    if (cl.hasOption(execfileOption.getOpt())) {
-      execFile = cl.getOptionValue(execfileOption.getOpt());
+    if (options.getExecFile() != null) {
+      execFile = options.getExecFile();
       verbose = false;
-    } else if (cl.hasOption(execfileVerboseOption.getOpt())) {
-      execFile = cl.getOptionValue(execfileVerboseOption.getOpt());
+    } else if (options.getExecFileVerbose() != null) {
+      execFile = options.getExecFileVerbose();
+      verbose = true;
     }
-    if (cl.hasOption(execCommandOpt.getOpt())) {
-      execCommand = cl.getOptionValue(execCommandOpt.getOpt());
+    execCommand = options.getExecCommand();
+    if (execCommand != null) {
       verbose = false;
     }
     
@@ -378,18 +377,18 @@ public class Shell extends ShellOptions 
     return configError;
   }
   
-  protected void setInstance(CommandLine cl) {
+  protected void setInstance(ShellOptionsJC options) {
     // should only be one instance option set
     instance = null;
-    if (cl.hasOption(fakeOption.getLongOpt())) {
+    if (options.isFake()) {
       instance = new MockInstance("fake");
-    } else if (cl.hasOption(hdfsZooInstance.getOpt())) {
+    } else if (options.isHdfsZooInstance()) {
       @SuppressWarnings("deprecation")
       AccumuloConfiguration deprecatedSiteConfiguration = AccumuloConfiguration.getSiteConfiguration();
       instance = getDefaultInstance(deprecatedSiteConfiguration);
-    } else if (cl.hasOption(zooKeeperInstance.getOpt())) {
-      String[] zkOpts = cl.getOptionValues(zooKeeperInstance.getOpt());
-      instance = new ZooKeeperInstance(zkOpts[0], zkOpts[1]);
+    } else if (options.getZooKeeperInstance().size() > 0) {
+      List<String> zkOpts = options.getZooKeeperInstance();
+      instance = new ZooKeeperInstance(zkOpts.get(0), zkOpts.get(1));
     } else {
       @SuppressWarnings("deprecation")
       AccumuloConfiguration deprecatedSiteConfiguration = AccumuloConfiguration.getSiteConfiguration();
@@ -459,7 +458,7 @@ public class Shell extends ShellOptions 
     ShellCompletor userCompletor = null;
     
     if (execFile != null) {
-      java.util.Scanner scanner = new java.util.Scanner(new File(execFile));
+      java.util.Scanner scanner = new java.util.Scanner(execFile);
       try {
         while (scanner.hasNextLine() && !hasExited()) {
           execCommand(scanner.nextLine(), true, isVerbose());
@@ -843,7 +842,7 @@ public class Shell extends ShellOptions 
       writer.close();
     }
   };
-
+  
   public final void printLines(Iterator<String> lines, boolean paginate) throws IOException {
     printLines(lines, paginate, null);
   }
@@ -1074,7 +1073,7 @@ public class Shell extends ShellOptions 
   public boolean hasExited() {
     return exit;
   }
-
+  
   public boolean isTabCompletion() {
     return tabCompletion;
   }

Modified: accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptions.java
URL: http://svn.apache.org/viewvc/accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptions.java?rev=1490273&r1=1490272&r2=1490273&view=diff
==============================================================================
--- accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptions.java (original)
+++ accumulo/branches/ACCUMULO-118/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptions.java Thu Jun  6 13:22:54 2013
@@ -17,87 +17,16 @@
 package org.apache.accumulo.core.util.shell;
 
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.OptionGroup;
-import org.apache.commons.cli.Options;
 
 /**
  * Abstract class to encompass the Options available on the Accumulo Shell
  */
 public abstract class ShellOptions {
-  protected static final String DEFAULT_AUTH_TIMEOUT = "60"; // in minutes
-  
   // Global options flags
   public static final String userOption = "u";
   public static final String tableOption = "t";
   public static final String helpOption = "?";
   public static final String helpLongOption = "help";
   
-  final Options opts = new Options();
-  
-  final Option usernameOption = new Option("u", "user", true, "username (defaults to your OS user)");
-  final Option passwOption = new Option("p", "password", true, "password (prompt for password if this option is missing)");
-  final Option tokenOption = new Option("tc", "tokenClass", true, "token type to create, use the -l to pass options");
-  final Option loginOption = new Option("l", "tokenProperty", true,
-      "login properties in the format key=value. Reuse -l for each property and/or comma seperate (prompt for properties if this option is missing");
-  
-  final Option tabCompleteOption = new Option(null, "disable-tab-completion", false, "disables tab completion (for less overhead when scripting)");
-  final Option debugOption = new Option(null, "debug", false, "enables client debugging");
-  final Option fakeOption = new Option(null, "fake", false, "fake a connection to accumulo");
   final Option helpOpt = new Option(helpOption, helpLongOption, false, "display this help");
-  final Option execCommandOpt = new Option("e", "execute-command", true, "executes a command, and then exits");
-  final OptionGroup execFileGroup = new OptionGroup();
-  final Option execfileOption = new Option("f", "execute-file", true, "executes commands from a file at startup");
-  final Option execfileVerboseOption = new Option("fv", "execute-file-verbose", true, "executes commands from a file at startup, with commands shown");
-  final OptionGroup instanceOptions = new OptionGroup();
-  final Option hdfsZooInstance = new Option("h", "hdfsZooInstance", false, "use hdfs zoo instance");
-  final Option zooKeeperInstance = new Option("z", "zooKeeperInstance", true, "use a zookeeper instance with the given instance name and list of zoo hosts");
-  final OptionGroup authTimeoutOptions = new OptionGroup();
-  final Option authTimeoutOpt = new Option(null, "auth-timeout", true, "minutes the shell can be idle without re-entering a password (default "
-      + DEFAULT_AUTH_TIMEOUT + " min)");
-  final Option disableAuthTimeoutOpt = new Option(null, "disable-auth-timeout", false, "disables requiring the user to re-type a password after being idle");
-
-  public ShellOptions() {
-    usernameOption.setArgName("user");
-    opts.addOption(usernameOption);
-    
-    passwOption.setArgName("pass");
-    opts.addOption(passwOption);
-    opts.addOption(loginOption);
-    opts.addOption(tokenOption);
-    
-    opts.addOption(tabCompleteOption);
-    
-    opts.addOption(debugOption);
-    
-    opts.addOption(fakeOption);
-    
-    opts.addOption(helpOpt);
-    
-    opts.addOption(execCommandOpt);
-    
-    
-    execfileOption.setArgName("file");
-    execFileGroup.addOption(execfileOption);
-    
-    execfileVerboseOption.setArgName("file");
-    execFileGroup.addOption(execfileVerboseOption);
-    
-    opts.addOptionGroup(execFileGroup);
-    
-    
-    instanceOptions.addOption(hdfsZooInstance);
-    
-    zooKeeperInstance.setArgName("name hosts");
-    zooKeeperInstance.setArgs(2);
-    instanceOptions.addOption(zooKeeperInstance);
-    
-    opts.addOptionGroup(instanceOptions);
-    
-    authTimeoutOpt.setArgName("minutes");
-    authTimeoutOptions.addOption(authTimeoutOpt);
-    
-    authTimeoutOptions.addOption(disableAuthTimeoutOpt);
-    
-    opts.addOptionGroup(authTimeoutOptions);
-  }
 }

Propchange: accumulo/branches/ACCUMULO-118/examples/
------------------------------------------------------------------------------
  Merged /accumulo/trunk/examples:r1489983-1490272

Propchange: accumulo/branches/ACCUMULO-118/fate/src/main/java/org/apache/accumulo/fate/ZooStore.java
------------------------------------------------------------------------------
  Merged /accumulo/trunk/fate/src/main/java/org/apache/accumulo/fate/ZooStore.java:r1489983-1490272

Propchange: accumulo/branches/ACCUMULO-118/fate/src/main/java/org/apache/accumulo/fate/zookeeper/ZooSession.java
------------------------------------------------------------------------------
  Merged /accumulo/trunk/fate/src/main/java/org/apache/accumulo/fate/zookeeper/ZooSession.java:r1489983-1490272

Propchange: accumulo/branches/ACCUMULO-118/pom.xml
------------------------------------------------------------------------------
  Merged /accumulo/trunk/pom.xml:r1489983-1490272

Propchange: accumulo/branches/ACCUMULO-118/proxy/README
------------------------------------------------------------------------------
  Merged /accumulo/trunk/proxy/README:r1489983-1490272

Propchange: accumulo/branches/ACCUMULO-118/server/
------------------------------------------------------------------------------
  Merged /accumulo/trunk/server:r1489983-1490272

Propchange: accumulo/branches/ACCUMULO-118/src/
------------------------------------------------------------------------------
  Merged /accumulo/trunk/src:r1489983-1490272