From f01ab4d8d7dfb79c3d42fade563b21d56bfa81f2 Mon Sep 17 00:00:00 2001 From: Jarek Jarcec Cecho Date: Sat, 9 Aug 2014 21:28:46 -0700 Subject: [PATCH] SQOOP-1424: Sqoop2: Simplify SqoopCommand in shell package --- client/pom.xml | 4 + pom.xml | 5 + .../org/apache/sqoop/shell/CloneCommand.java | 42 ++--- .../org/apache/sqoop/shell/CreateCommand.java | 42 ++--- .../org/apache/sqoop/shell/DeleteCommand.java | 43 ++--- .../apache/sqoop/shell/DisableCommand.java | 43 ++--- .../org/apache/sqoop/shell/EnableCommand.java | 43 ++--- .../org/apache/sqoop/shell/SetCommand.java | 46 ++---- .../apache/sqoop/shell/SetOptionFunction.java | 2 +- .../apache/sqoop/shell/SetServerFunction.java | 3 +- .../org/apache/sqoop/shell/ShowCommand.java | 96 ++--------- .../sqoop/shell/ShowConnectionFunction.java | 2 +- .../sqoop/shell/ShowConnectorFunction.java | 2 +- .../sqoop/shell/ShowFrameworkFunction.java | 2 +- .../apache/sqoop/shell/ShowJobFunction.java | 2 +- .../sqoop/shell/ShowOptionFunction.java | 2 +- .../sqoop/shell/ShowServerFunction.java | 2 +- .../sqoop/shell/ShowSubmissionFunction.java | 2 +- .../sqoop/shell/ShowVersionFunction.java | 2 +- .../org/apache/sqoop/shell/SqoopCommand.java | 151 +++++++++--------- .../org/apache/sqoop/shell/StartCommand.java | 38 +---- .../org/apache/sqoop/shell/StatusCommand.java | 36 +---- .../org/apache/sqoop/shell/StopCommand.java | 34 +--- .../org/apache/sqoop/shell/UpdateCommand.java | 42 ++--- .../apache/sqoop/shell/core/Constants.java | 50 +----- .../main/resources/shell-resource.properties | 48 +++--- 26 files changed, 227 insertions(+), 557 deletions(-) diff --git a/client/pom.xml b/client/pom.xml index 975773d5..b2e221ec 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -37,6 +37,10 @@ limitations under the License. junit test + + com.google.guava + guava + org.mockito mockito-all diff --git a/pom.xml b/pom.xml index d7947976..81f8acef 100644 --- a/pom.xml +++ b/pom.xml @@ -320,6 +320,11 @@ limitations under the License. json-simple ${json-simple.version} + + com.google.guava + guava + ${guava.version} + org.apache.sqoop.submission sqoop-submission-mapreduce diff --git a/shell/src/main/java/org/apache/sqoop/shell/CloneCommand.java b/shell/src/main/java/org/apache/sqoop/shell/CloneCommand.java index a7e7e7dc..4cdf0e4e 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/CloneCommand.java +++ b/shell/src/main/java/org/apache/sqoop/shell/CloneCommand.java @@ -17,47 +17,23 @@ */ package org.apache.sqoop.shell; +import com.google.common.collect.ImmutableMap; import org.apache.sqoop.shell.core.Constants; import org.codehaus.groovy.tools.shell.Shell; -import java.util.List; - -import static org.apache.sqoop.shell.ShellEnvironment.*; - /** * Client side cloning of connection and job objects. */ public class CloneCommand extends SqoopCommand { - private CloneConnectionFunction connectionFunction; - private CloneJobFunction jobFunction; - public CloneCommand(Shell shell) { - super(shell, Constants.CMD_CLONE, Constants.CMD_CLONE_SC, - new String[] {Constants.FN_CONNECTION, Constants.FN_JOB}, - Constants.PRE_CLONE, Constants.SUF_INFO); - } - - public Object executeCommand(List args) { - if (args.size() == 0) { - printlnResource(Constants.RES_CLONE_USAGE, getUsage()); - return null; - } - - String func = (String)args.get(0); - if (func.equals(Constants.FN_CONNECTION)) { - if (connectionFunction == null) { - connectionFunction = new CloneConnectionFunction(); - } - return connectionFunction.execute(args); - } else if (func.equals(Constants.FN_JOB)) { - if (jobFunction == null) { - jobFunction = new CloneJobFunction(); - } - return jobFunction.execute(args); - } else { - printlnResource(Constants.RES_FUNCTION_UNKNOWN, func); - return null; - } + super(shell, + Constants.CMD_CLONE, + Constants.CMD_CLONE_SC, + ImmutableMap.of( + Constants.FN_CONNECTION, CloneConnectionFunction.class, + Constants.FN_JOB, CloneJobFunction.class + ) + ); } } diff --git a/shell/src/main/java/org/apache/sqoop/shell/CreateCommand.java b/shell/src/main/java/org/apache/sqoop/shell/CreateCommand.java index 9ad007b4..fce7c86d 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/CreateCommand.java +++ b/shell/src/main/java/org/apache/sqoop/shell/CreateCommand.java @@ -17,47 +17,23 @@ */ package org.apache.sqoop.shell; +import com.google.common.collect.ImmutableMap; import org.apache.sqoop.shell.core.Constants; import org.codehaus.groovy.tools.shell.Shell; -import java.util.List; - -import static org.apache.sqoop.shell.ShellEnvironment.*; - /** * */ public class CreateCommand extends SqoopCommand { - private CreateConnectionFunction connectionFunction; - private CreateJobFunction jobFunction; - public CreateCommand(Shell shell) { - super(shell, Constants.CMD_CREATE, Constants.CMD_CREATE_SC, - new String[] {Constants.FN_CONNECTION, Constants.FN_JOB}, - Constants.PRE_CREATE, Constants.SUF_INFO); - } - - public Object executeCommand(List args) { - if (args.size() == 0) { - printlnResource(Constants.RES_CREATE_USAGE, getUsage()); - return null; - } - - String func = (String)args.get(0); - if (func.equals(Constants.FN_CONNECTION)) { - if (connectionFunction == null) { - connectionFunction = new CreateConnectionFunction(); - } - return connectionFunction.execute(args); - } else if (func.equals(Constants.FN_JOB)) { - if (jobFunction == null) { - jobFunction = new CreateJobFunction(); - } - return jobFunction.execute(args); - } else { - printlnResource(Constants.RES_FUNCTION_UNKNOWN, func); - return null; - } + super(shell, + Constants.CMD_CREATE, + Constants.CMD_CREATE_SC, + ImmutableMap.of( + Constants.FN_CONNECTION, CreateConnectionFunction.class, + Constants.FN_JOB, CreateJobFunction.class + ) + ); } } diff --git a/shell/src/main/java/org/apache/sqoop/shell/DeleteCommand.java b/shell/src/main/java/org/apache/sqoop/shell/DeleteCommand.java index abfcf2ed..107e5e0d 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/DeleteCommand.java +++ b/shell/src/main/java/org/apache/sqoop/shell/DeleteCommand.java @@ -17,48 +17,23 @@ */ package org.apache.sqoop.shell; +import com.google.common.collect.ImmutableMap; import org.apache.sqoop.shell.core.Constants; import org.codehaus.groovy.tools.shell.Shell; -import java.util.List; - -import static org.apache.sqoop.shell.ShellEnvironment.*; /** * */ public class DeleteCommand extends SqoopCommand { - private DeleteConnectionFunction connectionFunction; - private DeleteJobFunction jobFunction; - public DeleteCommand(Shell shell) { - super(shell, Constants.CMD_DELETE, Constants.CMD_DELETE_SC, - new String[] {Constants.FN_CONNECTION, Constants.FN_JOB}, - Constants.PRE_DELETE, Constants.SUF_INFO); - } - - @Override - @SuppressWarnings("unchecked") - public Object executeCommand(List args) { - if (args.size() == 0) { - printlnResource(Constants.RES_DELETE_USAGE, getUsage()); - return null; - } - - String func = (String)args.get(0); - if (func.equals(Constants.FN_CONNECTION)) { - if (connectionFunction == null) { - connectionFunction = new DeleteConnectionFunction(); - } - return connectionFunction.execute(args); - } else if (func.equals(Constants.FN_JOB)) { - if (jobFunction == null) { - jobFunction = new DeleteJobFunction(); - } - return jobFunction.execute(args); - } else { - printlnResource(Constants.RES_FUNCTION_UNKNOWN, func); - return null; - } + super(shell, + Constants.CMD_DELETE, + Constants.CMD_DELETE_SC, + ImmutableMap.of( + Constants.FN_CONNECTION, DeleteConnectionFunction.class, + Constants.FN_JOB, DeleteJobFunction.class + ) + ); } } diff --git a/shell/src/main/java/org/apache/sqoop/shell/DisableCommand.java b/shell/src/main/java/org/apache/sqoop/shell/DisableCommand.java index 5a6d942c..fa3263f7 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/DisableCommand.java +++ b/shell/src/main/java/org/apache/sqoop/shell/DisableCommand.java @@ -17,48 +17,23 @@ */ package org.apache.sqoop.shell; +import com.google.common.collect.ImmutableMap; import org.apache.sqoop.shell.core.Constants; import org.codehaus.groovy.tools.shell.Shell; -import java.util.List; - -import static org.apache.sqoop.shell.ShellEnvironment.*; /** * */ public class DisableCommand extends SqoopCommand { - private DisableConnectionFunction connectionFunction; - private DisableJobFunction jobFunction; - public DisableCommand(Shell shell) { - super(shell, Constants.CMD_DISABLE, Constants.CMD_DISABLE_SC, - new String[] {Constants.FN_CONNECTION, Constants.FN_JOB}, - Constants.PRE_DISABLE, Constants.SUF_INFO); - } - - @Override - @SuppressWarnings("unchecked") - public Object executeCommand(List args) { - if (args.size() == 0) { - printlnResource(Constants.RES_DISABLE_USAGE, getUsage()); - return null; - } - - String func = (String)args.get(0); - if (func.equals(Constants.FN_CONNECTION)) { - if (connectionFunction == null) { - connectionFunction = new DisableConnectionFunction(); - } - return connectionFunction.execute(args); - } else if (func.equals(Constants.FN_JOB)) { - if (jobFunction == null) { - jobFunction = new DisableJobFunction(); - } - return jobFunction.execute(args); - } else { - printlnResource(Constants.RES_FUNCTION_UNKNOWN, func); - return null; - } + super(shell, + Constants.CMD_DISABLE, + Constants.CMD_DISABLE_SC, + ImmutableMap.of( + Constants.FN_CONNECTION, DisableConnectionFunction.class, + Constants.FN_JOB, DisableJobFunction.class + ) + ); } } diff --git a/shell/src/main/java/org/apache/sqoop/shell/EnableCommand.java b/shell/src/main/java/org/apache/sqoop/shell/EnableCommand.java index 3b8c0b1e..b48647bb 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/EnableCommand.java +++ b/shell/src/main/java/org/apache/sqoop/shell/EnableCommand.java @@ -17,48 +17,23 @@ */ package org.apache.sqoop.shell; +import com.google.common.collect.ImmutableMap; import org.apache.sqoop.shell.core.Constants; import org.codehaus.groovy.tools.shell.Shell; -import java.util.List; - -import static org.apache.sqoop.shell.ShellEnvironment.*; /** * */ public class EnableCommand extends SqoopCommand { - private EnableConnectionFunction connectionFunction; - private EnableJobFunction jobFunction; - public EnableCommand(Shell shell) { - super(shell, Constants.CMD_ENABLE, Constants.CMD_ENABLE_SC, - new String[] {Constants.FN_CONNECTION, Constants.FN_JOB}, - Constants.PRE_ENABLE, Constants.SUF_INFO); - } - - @Override - @SuppressWarnings("unchecked") - public Object executeCommand(List args) { - if (args.size() == 0) { - printlnResource(Constants.RES_ENABLE_USAGE, getUsage()); - return null; - } - - String func = (String)args.get(0); - if (func.equals(Constants.FN_CONNECTION)) { - if (connectionFunction == null) { - connectionFunction = new EnableConnectionFunction(); - } - return connectionFunction.execute(args); - } else if (func.equals(Constants.FN_JOB)) { - if (jobFunction == null) { - jobFunction = new EnableJobFunction(); - } - return jobFunction.execute(args); - } else { - printlnResource(Constants.RES_FUNCTION_UNKNOWN, func); - return null; - } + super(shell, + Constants.CMD_ENABLE, + Constants.CMD_ENABLE_SC, + ImmutableMap.of( + Constants.FN_CONNECTION, EnableConnectionFunction.class, + Constants.FN_JOB, EnableJobFunction.class + ) + ); } } diff --git a/shell/src/main/java/org/apache/sqoop/shell/SetCommand.java b/shell/src/main/java/org/apache/sqoop/shell/SetCommand.java index 548def0c..3b8f4c2c 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/SetCommand.java +++ b/shell/src/main/java/org/apache/sqoop/shell/SetCommand.java @@ -17,48 +17,20 @@ */ package org.apache.sqoop.shell; -import java.util.List; - +import com.google.common.collect.ImmutableMap; import org.apache.sqoop.shell.core.Constants; import org.codehaus.groovy.tools.shell.Shell; -import static org.apache.sqoop.shell.ShellEnvironment.*; - public class SetCommand extends SqoopCommand { - private SetServerFunction serverFunction; - private SetOptionFunction optionFunction; - protected SetCommand(Shell shell) { - super(shell, Constants.CMD_SET, Constants.CMD_SET_SC, - new String[] {Constants.FN_SERVER, Constants.FN_OPTION}, - Constants.PRE_SET, Constants.SUF_INFO); - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Override - public Object executeCommand(List args) { - - if (args.size() == 0) { - printlnResource(Constants.RES_SET_USAGE, getUsage()); - return null; - } - String func = (String)args.get(0); - if (func.equals(Constants.FN_SERVER)) { - if (serverFunction == null) { - serverFunction = new SetServerFunction(); - } - return serverFunction.execute(args); - - } else if (func.equals(Constants.FN_OPTION)) { - if (optionFunction == null) { - optionFunction = new SetOptionFunction(); - } - return optionFunction.execute(args); - - } else { - printlnResource(Constants.RES_FUNCTION_UNKNOWN, func); - return null; - } + super(shell, + Constants.CMD_SET, + Constants.CMD_SET_SC, + ImmutableMap.of( + Constants.FN_SERVER, SetServerFunction.class, + Constants.FN_OPTION, SetOptionFunction.class + ) + ); } } diff --git a/shell/src/main/java/org/apache/sqoop/shell/SetOptionFunction.java b/shell/src/main/java/org/apache/sqoop/shell/SetOptionFunction.java index 5a15a542..700f646a 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/SetOptionFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/SetOptionFunction.java @@ -30,7 +30,7 @@ @SuppressWarnings("serial") public class SetOptionFunction extends SqoopFunction { @SuppressWarnings("static-access") - protected SetOptionFunction() { + public SetOptionFunction() { this.addOption(OptionBuilder.hasArg() .withDescription(resourceString(Constants.RES_SET_PROMPT_OPT_NAME)) .withLongOpt(Constants.OPT_NAME) diff --git a/shell/src/main/java/org/apache/sqoop/shell/SetServerFunction.java b/shell/src/main/java/org/apache/sqoop/shell/SetServerFunction.java index af99480c..84df2812 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/SetServerFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/SetServerFunction.java @@ -26,8 +26,9 @@ @SuppressWarnings("serial") public class SetServerFunction extends SqoopFunction { + @SuppressWarnings("static-access") - protected SetServerFunction() { + public SetServerFunction() { this.addOption(OptionBuilder.hasArg().withArgName(Constants.OPT_HOST) .withDescription(resourceString(Constants.RES_SET_HOST_DESCRIPTION)) .withLongOpt(Constants.OPT_HOST) diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowCommand.java b/shell/src/main/java/org/apache/sqoop/shell/ShowCommand.java index 672fa853..ba1d3842 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowCommand.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowCommand.java @@ -17,90 +17,26 @@ */ package org.apache.sqoop.shell; -import java.util.List; - +import com.google.common.collect.ImmutableMap; import org.apache.sqoop.shell.core.Constants; import org.codehaus.groovy.tools.shell.Shell; -import static org.apache.sqoop.shell.ShellEnvironment.*; - -public class ShowCommand extends SqoopCommand -{ - private ShowServerFunction serverFunction; - private ShowVersionFunction versionFunction; - private ShowConnectorFunction connectorFunction; - private ShowJobFunction jobFunction; - private ShowSubmissionFunction submissionFunction; - private ShowFrameworkFunction frameworkFunction; - private ShowConnectionFunction connectionFunction; - private ShowOptionFunction optionFunction; - +public class ShowCommand extends SqoopCommand { protected ShowCommand(Shell shell) { - super(shell, Constants.CMD_SHOW, Constants.CMD_SHOW_SC, - new String[] {Constants.FN_SERVER, Constants.FN_VERSION, - Constants.FN_CONNECTOR, Constants.FN_FRAMEWORK, - Constants.FN_CONNECTION, Constants.FN_JOB, Constants.FN_SUBMISSION, Constants.FN_OPTION }, - Constants.PRE_SHOW, Constants.SUF_INFO); - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Override - public Object executeCommand(List args) { - if (args.size() == 0) { - printlnResource(Constants.RES_SHOW_USAGE, getUsage()); - return null; - } - - String func = (String)args.get(0); - if (func.equals(Constants.FN_SERVER)) { - if (serverFunction == null) { - serverFunction = new ShowServerFunction(); - } - return serverFunction.execute(args); - - } else if (func.equals(Constants.FN_VERSION)) { - if (versionFunction == null) { - versionFunction = new ShowVersionFunction(); - } - return versionFunction.execute(args); - - } else if (func.equals(Constants.FN_CONNECTOR)) { - if (connectorFunction == null) { - connectorFunction = new ShowConnectorFunction(); - } - return connectorFunction.execute(args); - - } else if (func.equals(Constants.FN_FRAMEWORK)) { - if (frameworkFunction == null) { - frameworkFunction = new ShowFrameworkFunction(); - } - return frameworkFunction.execute(args); - - } else if (func.equals(Constants.FN_CONNECTION)) { - if (connectionFunction == null) { - connectionFunction = new ShowConnectionFunction(); - } - return connectionFunction.execute(args); - - } else if (func.equals(Constants.FN_JOB)) { - if (jobFunction == null) { - jobFunction = new ShowJobFunction(); - } - return jobFunction.execute(args); - } else if (func.equals(Constants.FN_SUBMISSION)) { - if (submissionFunction == null) { - submissionFunction = new ShowSubmissionFunction(); - } - return submissionFunction.execute(args); - } else if (func.equals(Constants.FN_OPTION)) { - if (optionFunction == null) { - optionFunction = new ShowOptionFunction(); - } - return optionFunction.execute(args); - } else { - printlnResource(Constants.RES_FUNCTION_UNKNOWN, func); - return null; - } + super(shell, + Constants.CMD_SHOW, + Constants.CMD_SHOW_SC, + new ImmutableMap.Builder>() + .put(Constants.FN_SERVER, ShowServerFunction.class) + .put(Constants.FN_VERSION, ShowVersionFunction.class) + .put(Constants.FN_CONNECTOR, ShowConnectorFunction.class) + .put(Constants.FN_FRAMEWORK, ShowFrameworkFunction.class) + .put(Constants.FN_CONNECTION, ShowConnectionFunction.class) + .put(Constants.FN_JOB, ShowJobFunction.class) + .put(Constants.FN_SUBMISSION, ShowSubmissionFunction.class) + .put(Constants.FN_OPTION, ShowOptionFunction.class) + .build() + ); } } diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowConnectionFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowConnectionFunction.java index dfaa90ef..3e8cc0ac 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowConnectionFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowConnectionFunction.java @@ -37,7 +37,7 @@ @SuppressWarnings("serial") public class ShowConnectionFunction extends SqoopFunction { @SuppressWarnings("static-access") - protected ShowConnectionFunction() { + public ShowConnectionFunction() { this.addOption(OptionBuilder .withDescription(resourceString(Constants.RES_SHOW_PROMPT_DISPLAY_ALL_CONNS)) .withLongOpt(Constants.OPT_ALL) diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowConnectorFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowConnectorFunction.java index 7b9b00ce..bbfbb3f1 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowConnectorFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowConnectorFunction.java @@ -34,7 +34,7 @@ @SuppressWarnings("serial") public class ShowConnectorFunction extends SqoopFunction { @SuppressWarnings("static-access") - protected ShowConnectorFunction() { + public ShowConnectorFunction() { this.addOption(OptionBuilder .withDescription(resourceString(Constants.RES_SHOW_PROMPT_DISPLAY_ALL_CONNECTORS)) .withLongOpt(Constants.OPT_ALL) diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowFrameworkFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowFrameworkFunction.java index 6e430725..0c587b2e 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowFrameworkFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowFrameworkFunction.java @@ -32,7 +32,7 @@ */ @SuppressWarnings("serial") public class ShowFrameworkFunction extends SqoopFunction { - protected ShowFrameworkFunction() { + public ShowFrameworkFunction() { } @Override diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowJobFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowJobFunction.java index 46182114..464f66ee 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowJobFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowJobFunction.java @@ -38,7 +38,7 @@ @SuppressWarnings("serial") public class ShowJobFunction extends SqoopFunction { @SuppressWarnings("static-access") - protected ShowJobFunction() { + public ShowJobFunction() { this.addOption(OptionBuilder .withDescription(resourceString(Constants.RES_SHOW_PROMPT_DISPLAY_ALL_JOBS)) .withLongOpt(Constants.OPT_ALL) diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowOptionFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowOptionFunction.java index 920e6596..4bb0cab3 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowOptionFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowOptionFunction.java @@ -33,7 +33,7 @@ public class ShowOptionFunction extends SqoopFunction { * Construct new object. */ @SuppressWarnings("static-access") - protected ShowOptionFunction() { + public ShowOptionFunction() { this.addOption(OptionBuilder .hasArg().withArgName(Constants.OPT_NAME) .withDescription(resource.getString(Constants.RES_SET_PROMPT_OPT_NAME)) diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowServerFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowServerFunction.java index 23016ee3..67eb6a60 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowServerFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowServerFunction.java @@ -27,7 +27,7 @@ @SuppressWarnings("serial") public class ShowServerFunction extends SqoopFunction { @SuppressWarnings("static-access") - protected ShowServerFunction() { + public ShowServerFunction() { this.addOption(OptionBuilder .withDescription(resourceString(Constants.RES_SHOW_PROMPT_DISPLAY_ALL_SERVERS)) .withLongOpt(Constants.OPT_ALL) diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowSubmissionFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowSubmissionFunction.java index be50cef9..2d00b88e 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowSubmissionFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowSubmissionFunction.java @@ -33,7 +33,7 @@ @SuppressWarnings("serial") public class ShowSubmissionFunction extends SqoopFunction { @SuppressWarnings("static-access") - protected ShowSubmissionFunction() { + public ShowSubmissionFunction() { this.addOption(OptionBuilder .withDescription(resourceString(Constants.RES_SHOW_PROMPT_DISPLAY_ALL_SUBMISSIONS)) .withLongOpt(Constants.OPT_DETAIL) diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowVersionFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowVersionFunction.java index f0919d36..6cb28e59 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/ShowVersionFunction.java +++ b/shell/src/main/java/org/apache/sqoop/shell/ShowVersionFunction.java @@ -35,7 +35,7 @@ public class ShowVersionFunction extends SqoopFunction { @SuppressWarnings("static-access") - protected ShowVersionFunction() { + public ShowVersionFunction() { this.addOption(OptionBuilder .withDescription(resourceString(Constants.RES_SHOW_PROMPT_DISPLAY_ALL_VERSIONS)) .withLongOpt(Constants.OPT_ALL) diff --git a/shell/src/main/java/org/apache/sqoop/shell/SqoopCommand.java b/shell/src/main/java/org/apache/sqoop/shell/SqoopCommand.java index 241d120b..cbd34f50 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/SqoopCommand.java +++ b/shell/src/main/java/org/apache/sqoop/shell/SqoopCommand.java @@ -23,99 +23,75 @@ import java.util.*; +import org.apache.commons.lang.StringUtils; +import org.apache.sqoop.shell.core.Constants; import org.apache.sqoop.common.SqoopException; import org.apache.sqoop.shell.core.ShellError; +import org.apache.sqoop.utils.ClassUtils; import org.codehaus.groovy.tools.shell.ComplexCommandSupport; import org.codehaus.groovy.tools.shell.Shell; -public abstract class SqoopCommand extends ComplexCommandSupport -{ - private String descriptionPrefix; - private String descriptionPostfix; +import static org.apache.sqoop.shell.ShellEnvironment.*; - private String description; - private String usage; - private String help; +/** + * Sqoop shell command. + * + * Every command should define following resource properties: + * + * $command.description + * One sentence describing purpose of the command, displayed on "help" command. + */ +public abstract class SqoopCommand extends ComplexCommandSupport { - @SuppressWarnings("unchecked") - protected SqoopCommand(Shell shell, String name, String shortcut, - String[] funcs, String descriptionPrefix, String descriptionPostfix) { + /** + * Command name + */ + private String name; + + /** + * Function map given by concrete implementation. + * + * Key: Name of the function as is present in the shell + * Value: Class name implementing the function + */ + private final Map> functionNames; + + /** + * Instantiated functions for reuse. Built lazily. + */ + private final Map functionInstances; + + protected SqoopCommand(Shell shell, + String name, + String shortcut, + Map> funcs) { super(shell, name, shortcut); - this.functions = new LinkedList(); - for (String func : funcs) { - this.functions.add(func); - } + this.name = name; + this.functionNames = funcs; + this.functionInstances = new HashMap(); - this.descriptionPrefix = descriptionPrefix; - this.descriptionPostfix = descriptionPostfix; + this.functions = new LinkedList(); + this.functions.addAll(funcs.keySet()); } @Override public String getDescription() { - if (description == null) { - StringBuilder sb = new StringBuilder(); - - if (descriptionPrefix != null) { - sb.append(descriptionPrefix); - sb.append(" "); - } - - @SuppressWarnings("unchecked") - Iterator iterator = functions.iterator(); - int size = functions.size(); - sb.append(iterator.next()); - if (size > 1) { - for (int i = 1; i < (size - 1); i++) { - sb.append(", "); - sb.append(iterator.next()); - } - sb.append(" or "); - sb.append(iterator.next()); - } - - if (descriptionPostfix != null) { - sb.append(" "); - sb.append(descriptionPostfix); - } - - description = sb.toString(); - } - - return description; + return resourceString(name + ".description"); } @Override public String getUsage() { - if (usage == null) { - StringBuilder sb = new StringBuilder(); - - sb.append("["); - - @SuppressWarnings("unchecked") - Iterator iterator = functions.iterator(); - int size = functions.size(); - sb.append(iterator.next()); - for (int i = 1; i < size; i++) { - sb.append("|"); - sb.append(iterator.next()); - } - - sb.append("]"); - - usage = sb.toString(); - } - - return usage; + return new StringBuilder() + .append("[") + .append(StringUtils.join(functionNames.keySet(), "|")) + .append("]") + .toString(); } @Override public String getHelp() { - if (help == null) { - help = getDescription() + "."; - } - - return help; + return getDescription() + "."; } /** @@ -132,7 +108,38 @@ public Object execute(List args) { * @param args list * @return Object */ - public abstract Object executeCommand(List args); + public Object executeCommand(List args) { + if (args.size() == 0) { + printlnResource(Constants.RES_SHARED_USAGE, name, getUsage()); + return null; + } + + String func = (String)args.get(0); + + // Unknown function + if(!functionNames.containsKey(func)) { + printlnResource(Constants.RES_SHARED_UNKNOWN_FUNCTION, func); + return null; + } + + // If we already do have the instance, execute it + if(functionInstances.containsKey(func)) { + return functionInstances.get(func).execute(args); + } + + // Otherwise create new instance + Class klass = functionNames.get(func); + SqoopFunction instance = (SqoopFunction) ClassUtils.instantiate(klass); + if(instance == null) { + // This is pretty much a developer error as it shouldn't happen without changing and testing code + throw new SqoopException(ShellError.SHELL_0000, "Can't instantiate class " + klass); + } + + functionInstances.put(func, instance); + + // And return the function execution + return instance.execute(args); + } @SuppressWarnings({ "rawtypes", "unchecked" }) protected void resolveVariables(List arg) { diff --git a/shell/src/main/java/org/apache/sqoop/shell/StartCommand.java b/shell/src/main/java/org/apache/sqoop/shell/StartCommand.java index 914454f0..7c569807 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/StartCommand.java +++ b/shell/src/main/java/org/apache/sqoop/shell/StartCommand.java @@ -17,41 +17,19 @@ */ package org.apache.sqoop.shell; -import java.util.List; - -import org.apache.log4j.Logger; +import com.google.common.collect.ImmutableMap; import org.apache.sqoop.shell.core.Constants; import org.codehaus.groovy.tools.shell.Shell; -import static org.apache.sqoop.shell.ShellEnvironment.printlnResource; - public class StartCommand extends SqoopCommand { - public static final Logger LOG = Logger.getLogger(StartCommand.class); - - private StartJobFunction startJobFunction; protected StartCommand(Shell shell) { - super(shell, Constants.CMD_START, Constants.CMD_START_SC, - new String[] {Constants.FN_JOB}, Constants.PRE_START, null); - } - - @Override - public Object executeCommand(List args) { - if (args.size() == 0) { - printlnResource(Constants.RES_START_USAGE, getUsage()); - return null; - } - - String func = (String) args.get(0); - if (func.equals(Constants.FN_JOB)) { - if (startJobFunction == null) { - startJobFunction = new StartJobFunction(); - } - return startJobFunction.execute(args); - } else { - printlnResource(Constants.RES_FUNCTION_UNKNOWN, func); - } - - return null; + super(shell, + Constants.CMD_START, + Constants.CMD_START_SC, + new ImmutableMap.Builder>() + .put(Constants.FN_JOB, StartJobFunction.class) + .build() + ); } } diff --git a/shell/src/main/java/org/apache/sqoop/shell/StatusCommand.java b/shell/src/main/java/org/apache/sqoop/shell/StatusCommand.java index ebd45489..3447a87c 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/StatusCommand.java +++ b/shell/src/main/java/org/apache/sqoop/shell/StatusCommand.java @@ -17,39 +17,19 @@ */ package org.apache.sqoop.shell; -import java.util.List; - +import com.google.common.collect.ImmutableMap; import org.apache.sqoop.shell.core.Constants; import org.codehaus.groovy.tools.shell.Shell; -import static org.apache.sqoop.shell.ShellEnvironment.printlnResource; - public class StatusCommand extends SqoopCommand { - private StatusJobFunction statusJobFunction; - protected StatusCommand(Shell shell) { - super(shell, Constants.CMD_STATUS, Constants.CMD_STATUS_SC, - new String[] { Constants.FN_JOB }, Constants.PRE_STATUS, null); - } - - @Override - public Object executeCommand(List args) { - if (args.size() == 0) { - printlnResource(Constants.RES_STATUS_USAGE, getUsage()); - return null; - } - - String func = (String) args.get(0); - if (func.equals(Constants.FN_JOB)) { - if (statusJobFunction == null) { - statusJobFunction = new StatusJobFunction(); - } - return statusJobFunction.execute(args); - } else { - printlnResource(Constants.RES_FUNCTION_UNKNOWN, func); - } - - return null; + super(shell, + Constants.CMD_STATUS, + Constants.CMD_STATUS_SC, + new ImmutableMap.Builder>() + .put(Constants.FN_JOB, StatusJobFunction.class) + .build() + ); } } diff --git a/shell/src/main/java/org/apache/sqoop/shell/StopCommand.java b/shell/src/main/java/org/apache/sqoop/shell/StopCommand.java index 65a454bb..50b2e81b 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/StopCommand.java +++ b/shell/src/main/java/org/apache/sqoop/shell/StopCommand.java @@ -17,37 +17,19 @@ */ package org.apache.sqoop.shell; -import java.util.List; - +import com.google.common.collect.ImmutableMap; import org.apache.sqoop.shell.core.Constants; import org.codehaus.groovy.tools.shell.Shell; -import static org.apache.sqoop.shell.ShellEnvironment.printlnResource; - public class StopCommand extends SqoopCommand { - private StopJobFunction stopJobFunction; - protected StopCommand(Shell shell) { - super(shell, Constants.CMD_STOP, Constants.CMD_STOP_SC, - new String[] { Constants.FN_JOB }, Constants.PRE_STOP, null); - } - @Override - public Object executeCommand(List args) { - if (args.size() == 0) { - printlnResource(Constants.RES_STOP_USAGE, getUsage()); - return null; - } - - String func = (String) args.get(0); - if (func.equals(Constants.FN_JOB)) { - if (stopJobFunction == null) { - stopJobFunction = new StopJobFunction(); - } - return stopJobFunction.execute(args); - } else { - printlnResource(Constants.RES_FUNCTION_UNKNOWN, func); - } - return null; + super(shell, + Constants.CMD_STOP, + Constants.CMD_STOP_SC, + new ImmutableMap.Builder>() + .put(Constants.FN_JOB, StopJobFunction.class) + .build() + ); } } diff --git a/shell/src/main/java/org/apache/sqoop/shell/UpdateCommand.java b/shell/src/main/java/org/apache/sqoop/shell/UpdateCommand.java index 24f31ea2..d291c422 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/UpdateCommand.java +++ b/shell/src/main/java/org/apache/sqoop/shell/UpdateCommand.java @@ -17,47 +17,23 @@ */ package org.apache.sqoop.shell; +import com.google.common.collect.ImmutableMap; import org.apache.sqoop.shell.core.Constants; import org.codehaus.groovy.tools.shell.Shell; -import java.util.List; - -import static org.apache.sqoop.shell.ShellEnvironment.*; - /** * */ public class UpdateCommand extends SqoopCommand { - private UpdateConnectionFunction connectionFunction; - private UpdateJobFunction jobFunction; - public UpdateCommand(Shell shell) { - super(shell, Constants.CMD_UPDATE, Constants.CMD_UPDATE_SC, - new String[] {Constants.FN_CONNECTION, Constants.FN_JOB}, - Constants.PRE_UPDATE, Constants.SUF_INFO); - } - - public Object executeCommand(List args) { - if (args.size() == 0) { - printlnResource(Constants.RES_UPDATE_USAGE, getUsage()); - return null; - } - - String func = (String)args.get(0); - if (func.equals(Constants.FN_CONNECTION)) { - if (connectionFunction == null) { - connectionFunction = new UpdateConnectionFunction(); - } - return connectionFunction.execute(args); - } else if (func.equals(Constants.FN_JOB)) { - if (jobFunction == null) { - jobFunction = new UpdateJobFunction(); - } - return jobFunction.execute(args); - } else { - printlnResource(Constants.RES_FUNCTION_UNKNOWN, func); - return null; - } + super(shell, + Constants.CMD_UPDATE, + Constants.CMD_UPDATE_SC, + ImmutableMap.of( + Constants.FN_CONNECTION, UpdateConnectionFunction.class, + Constants.FN_JOB, UpdateJobFunction.class + ) + ); } } diff --git a/shell/src/main/java/org/apache/sqoop/shell/core/Constants.java b/shell/src/main/java/org/apache/sqoop/shell/core/Constants.java index 908b44da..3ea56a48 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/core/Constants.java +++ b/shell/src/main/java/org/apache/sqoop/shell/core/Constants.java @@ -122,29 +122,16 @@ public class Constants { public static final String FN_VERSION = "version"; public static final String FN_FRAMEWORK = "framework"; - public static final String PRE_CLONE = "Clone"; - public static final String PRE_CREATE = "Create"; - public static final String PRE_DELETE = "Delete"; - public static final String PRE_SET = "Set"; - public static final String PRE_SHOW = "Show"; - public static final String PRE_UPDATE = "Update"; - public static final String PRE_START = "Start"; - public static final String PRE_STATUS = "Status"; - public static final String PRE_STOP = "Stop"; - public static final String PRE_ENABLE = "Enable"; - public static final String PRE_DISABLE = "Disable"; - public static final String SUF_INFO = "Info"; - - public static final String PROP_HOMEDIR = "user.home"; public static final String PROP_CURDIR = "user.dir"; public static final String SQOOP_PROMPT = "sqoop"; + // Shared resources + public static final String RES_SHARED_USAGE = "shared.usage"; + public static final String RES_SHARED_UNKNOWN_FUNCTION = "shared.unknown.function"; // Resource Keys for various messages - public static final String RES_FUNCTION_UNKNOWN = - "args.function.unknown"; public static final String RES_ARGS_XID_MISSING = "args.xid_missing"; public static final String RES_ARGS_FXID_MISSING = @@ -175,8 +162,6 @@ public class Constants { public static final String RES_PROMPT_FILL_JOB_METADATA = "prompt.fill_job_metadata"; - public static final String RES_CLONE_USAGE = - "clone.usage"; public static final String RES_CLONE_CONN_SUCCESSFUL = "clone.conn.successful"; public static final String RES_CLONE_JOB_SUCCESSFUL = @@ -186,8 +171,6 @@ public class Constants { public static final String RES_CLONE_CLONING_JOB = "clone.cloning_job"; - public static final String RES_CREATE_USAGE = - "create.usage"; public static final String RES_CREATE_CONN_SUCCESSFUL = "create.conn_successful"; public static final String RES_CREATE_JOB_SUCCESSFUL = @@ -197,18 +180,11 @@ public class Constants { public static final String RES_CREATE_CREATING_JOB = "create.creating_job"; - public static final String RES_DELETE_USAGE = - "delete.usage"; - - public static final String RES_DISABLE_USAGE = - "disable.usage"; public static final String RES_DISABLE_CONNECTION_SUCCESSFUL = "disable.conn_successful"; public static final String RES_DISABLE_JOB_SUCCESSFUL = "disable.job_successful"; - public static final String RES_ENABLE_USAGE = - "enable.usage"; public static final String RES_ENABLE_CONNECTION_SUCCESSFUL = "enable.conn_successful"; public static final String RES_ENABLE_JOB_SUCCESSFUL = @@ -234,8 +210,6 @@ public class Constants { public static final String RES_UNRECOGNIZED_CMD = "unrecognized.cmd"; - public static final String RES_SET_USAGE = - "set.usage"; public static final String RES_SET_PROMPT_OPT_NAME = "set.prompt_opt_name"; public static final String RES_SET_PROMPT_OPT_VALUE = @@ -261,14 +235,10 @@ public class Constants { public static final String RES_SET_SERVER_IGNORED = "set.server_ignored"; - public static final String RES_SHOW_USAGE = - "show.usage"; public static final String RES_SHOW_PROMPT_DISPLAY_ALL_CONNS = "show.prompt_display_all_conns"; public static final String RES_SHOW_PROMPT_DISPLAY_CONN_XID = "show.prompt_display_conn_xid"; - public static final String RES_SHOW_CONN_USAGE = - "show.conn_usage"; public static final String RES_SHOW_PROMPT_CONNS_TO_SHOW = "show.prompt_conns_to_show"; public static final String RES_SHOW_PROMPT_CONN_INFO = @@ -280,8 +250,6 @@ public class Constants { "show.prompt_display_all_connectors"; public static final String RES_SHOW_PROMPT_DISPLAY_CONNECTOR_CID = "show.prompt_display_connector_cid"; - public static final String RES_SHOW_CONNECTOR_USAGE = - "show.connector_usage"; public static final String RES_SHOW_PROMPT_CONNECTORS_TO_SHOW = "show.prompt_connectors_to_show"; public static final String RES_SHOW_PROMPT_CONNECTOR_INFO = @@ -296,8 +264,6 @@ public class Constants { "show.prompt_display_all_jobs"; public static final String RES_SHOW_PROMPT_DISPLAY_JOB_JID = "show.prompt_display_job_jid"; - public static final String RES_SHOW_JOB_USAGE = - "show.job_usage"; public static final String RES_SHOW_PROMPT_JOBS_TO_SHOW = "show.prompt_jobs_to_show"; public static final String RES_SHOW_PROMPT_JOB_INFO = @@ -342,17 +308,9 @@ public class Constants { public static final String RES_SHOW_PROMPT_VERSION_PROTOCOL = "show.prompt_version_protocol"; - public static final String RES_START_USAGE = - "start.usage"; - - public static final String RES_STATUS_USAGE = - "status.usage"; public static final String RES_PROMPT_SYNCHRONOUS = "start.prompt_synchronous"; - public static final String RES_STOP_USAGE = - "stop.usage"; - public static final String RES_SQOOP_SHELL_BANNER = "sqoop.shell_banner"; public static final String RES_SQOOP_PROMPT_SHELL_LOADRC = @@ -360,8 +318,6 @@ public class Constants { public static final String RES_SQOOP_PROMPT_SHELL_LOADEDRC = "sqoop.prompt_shell_loadedrc"; - public static final String RES_UPDATE_USAGE = - "update.usage"; public static final String RES_UPDATE_UPDATING_CONN = "update.conn"; public static final String RES_UPDATE_CONN_SUCCESSFUL = diff --git a/shell/src/main/resources/shell-resource.properties b/shell/src/main/resources/shell-resource.properties index d4c782e9..7fa56714 100644 --- a/shell/src/main/resources/shell-resource.properties +++ b/shell/src/main/resources/shell-resource.properties @@ -17,7 +17,7 @@ ############################ # Security Form -# +############################# object-name.label = Name object-name.help = Non unique name of the entity to help you remember \ it's purpose @@ -25,10 +25,13 @@ object-name.help = Non unique name of the entity to help you remember \ ############################# # Messages -# +############################# + +# Shared (for all commands/functions) +shared.usage = @|bold Usage:|@ {0} {1} +shared.unknown.function = The specified function "{0}" is not recognized. + # Argument related -# -args.function.unknown = The specified function "{0}" is not recognized. args.xid_missing = Required argument --xid is missing. args.fxid_missing = Required argument --fxid is missing. args.txid_missing = Required argument --txid is missing. @@ -37,7 +40,6 @@ args.cid_missing = Required argument --cid is missing. args.name_missing = Required argument --name is missing. args.value_missing = Required argument --value is missing. - ## Generic description of various ids, types etc prompt.conn_id = Connection ID prompt.connector_id = Connector ID @@ -53,17 +55,15 @@ connection object prompt.fill_job_metadata = Please fill following values to create new \ job object -# # Update command +update.description = Update objects in Sqoop repository update.conn = Updating connection with id {0} update.job = Updating job with id {0} -update.usage = Usage: update {0} update.conn_successful = Connection was successfully updated with status {0} update.job_successful = Job was successfully updated with status {0} -# # Clone command -clone.usage = Usage: clone {0} +clone.description = Create new object based on existing one clone.conn.successful = Connection was successfully created with validation \ status {0} and persistent id {1} clone.job.successful = Job was successfully created with validation \ @@ -71,34 +71,28 @@ clone.job.successful = Job was successfully created with validation \ clone.cloning_conn = Cloning connection with id {0} clone.cloning_job = Cloning job with id {0} -# # Create command -create.usage = Usage: create {0} +create.description = Create new object in Sqoop repository create.conn_successful = New connection was successfully created with \ validation status {0} and persistent id {1} create.job_successful = New job was successfully created with validation \ status {0} and persistent id {1} -## Creating messages create.creating_conn = Creating connection for connector with id {0} create.creating_job = Creating job for connections with id {0} and {1} -# # Delete command -delete.usage = Usage: delete {0} +delete.description = Delete existing object in Sqoop repository -# # Enable command -enable.usage = Usage: enable {0} +enable.description = Enable object in Sqoop repository enable.conn_successful = Connection {0} was successfully enabled enable.job_successful = Job {0} was successfully enabled -# # Disable command -disable.usage = Usage: disable {0} +disable.description = Disable object in Sqoop repository disable.conn_successful = Connection {0} was successfully disabled disable.job_successful = Job {0} was successfully disabled -# # Help command help.usage = [] help.description = Display this help message @@ -114,9 +108,8 @@ help.specific_cmd_info = For help on a specific command type: \ unrecognized.cmd = Unrecognized command {0} -# # Set command -set.usage = Usage: set {0} +set.description = Configure various client options and settings set.prompt_opt_name = Client option name set.prompt_opt_value = New option value set.verbose_changed = Verbose option was changed to {0} @@ -131,8 +124,8 @@ set.server_successful = Server is set successfully set.server_ignored = --host, --port or --webapp option is ignored, because --url option is given. -show.usage = Usage: show {0} - +# Show command +show.description = Display various objects and configuration options show.prompt_display_all_conns = Display all connections show.prompt_display_conn_xid = Display the connection with xid show.conn_usage = Usage: show connection @@ -182,12 +175,15 @@ sqoop.shell_banner = @|green Sqoop Shell:|@ Type '@|bold help|@' or '@|bold \\h| sqoop.prompt_shell_loadrc = Loading resource file {0} sqoop.prompt_shell_loadedrc = Resource file loaded. -start.usage = Usage: start {0} +# Start command +start.description = Start job start.prompt_synchronous = Wait for submission to finish -stop.usage = Usage: stop {0} +# Stop command +stop.description = Stop job -status.usage = Usage: status {0} +# Status command +status.description = Display status of a job # Various Table headers table.header.id = Id