diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeperMain.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeperMain.java index dad878cf87c..d7f4b3ccde5 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeperMain.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeperMain.java @@ -31,38 +31,15 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.NoSuchElementException; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; + import org.apache.yetus.audience.InterfaceAudience; import org.apache.zookeeper.admin.ZooKeeperAdmin; -import org.apache.zookeeper.cli.AddAuthCommand; -import org.apache.zookeeper.cli.AddWatchCommand; import org.apache.zookeeper.cli.CliCommand; -import org.apache.zookeeper.cli.CliException; -import org.apache.zookeeper.cli.CloseCommand; -import org.apache.zookeeper.cli.CommandNotFoundException; -import org.apache.zookeeper.cli.CreateCommand; -import org.apache.zookeeper.cli.DelQuotaCommand; -import org.apache.zookeeper.cli.DeleteAllCommand; -import org.apache.zookeeper.cli.DeleteCommand; -import org.apache.zookeeper.cli.GetAclCommand; -import org.apache.zookeeper.cli.GetAllChildrenNumberCommand; -import org.apache.zookeeper.cli.GetCommand; -import org.apache.zookeeper.cli.GetConfigCommand; -import org.apache.zookeeper.cli.GetEphemeralsCommand; -import org.apache.zookeeper.cli.ListQuotaCommand; -import org.apache.zookeeper.cli.LsCommand; -import org.apache.zookeeper.cli.MalformedCommandException; -import org.apache.zookeeper.cli.ReconfigCommand; -import org.apache.zookeeper.cli.RemoveWatchesCommand; -import org.apache.zookeeper.cli.SetAclCommand; -import org.apache.zookeeper.cli.SetCommand; -import org.apache.zookeeper.cli.SetQuotaCommand; -import org.apache.zookeeper.cli.StatCommand; -import org.apache.zookeeper.cli.SyncCommand; -import org.apache.zookeeper.cli.VersionCommand; +import org.apache.zookeeper.cli.CommandFactory; import org.apache.zookeeper.client.ZKClientConfig; import org.apache.zookeeper.server.ExitCode; import org.apache.zookeeper.util.ServiceUtils; @@ -99,34 +76,15 @@ public boolean getPrintWatches() { commandMap.put("redo", "cmdno"); commandMap.put("printwatches", "on|off"); commandMap.put("quit", ""); - - new CloseCommand().addToMap(commandMapCli); - new CreateCommand().addToMap(commandMapCli); - new DeleteCommand().addToMap(commandMapCli); - new DeleteAllCommand().addToMap(commandMapCli); - new SetCommand().addToMap(commandMapCli); - new GetCommand().addToMap(commandMapCli); - new LsCommand().addToMap(commandMapCli); - new GetAclCommand().addToMap(commandMapCli); - new SetAclCommand().addToMap(commandMapCli); - new StatCommand().addToMap(commandMapCli); - new SyncCommand().addToMap(commandMapCli); - new SetQuotaCommand().addToMap(commandMapCli); - new ListQuotaCommand().addToMap(commandMapCli); - new DelQuotaCommand().addToMap(commandMapCli); - new AddAuthCommand().addToMap(commandMapCli); - new ReconfigCommand().addToMap(commandMapCli); - new GetConfigCommand().addToMap(commandMapCli); - new RemoveWatchesCommand().addToMap(commandMapCli); - new GetEphemeralsCommand().addToMap(commandMapCli); - new GetAllChildrenNumberCommand().addToMap(commandMapCli); - new VersionCommand().addToMap(commandMapCli); - new AddWatchCommand().addToMap(commandMapCli); - - // add all to commandMap - for (Entry entry : commandMapCli.entrySet()) { - commandMap.put(entry.getKey(), entry.getValue().getOptionStr()); - } + Stream.of(CommandFactory.Command.values()) + .map(command -> CommandFactory.getInstance(command)) + // add all commands to commandMapCli and commandMap + .forEach(cliCommand ->{ + cliCommand.addToMap(commandMapCli); + commandMap.put( + cliCommand.getCmdStr(), + cliCommand.getOptionStr()); + }); } static void usage() { diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CommandFactory.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CommandFactory.java new file mode 100644 index 00000000000..22e445864da --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CommandFactory.java @@ -0,0 +1,58 @@ +package org.apache.zookeeper.cli; + +import org.apache.zookeeper.server.admin.Command; + +import java.util.function.Supplier; + +/** + * Factory class for creating instances of {@code CliCommand}. + */ +public class CommandFactory { + + /** + * All Cli Commands. + */ + public enum Command { + CLOSE(CloseCommand::new), + CREATE(CreateCommand::new), + DELETE(DeleteCommand::new), + DELETE_ALL(DeleteAllCommand::new), + SET(SetCommand::new), + GET(GetCommand::new), + LS(LsCommand::new), + GET_ACL(GetAclCommand::new), + SET_ACL(SetAclCommand::new), + STAT(StatCommand::new), + SYNC(SyncCommand::new), + SET_QUOTA(SetQuotaCommand::new), + LIST_QUOTA(ListQuotaCommand::new), + DEL_QUOTA(DelQuotaCommand::new), + ADD_AUTH(AddAuthCommand::new), + RECONFIG(ReconfigCommand::new), + GET_CONFIG(GetConfigCommand::new), + REMOVE_WATCHES(RemoveWatchesCommand::new), + GET_EPHEMERALS(GetEphemeralsCommand::new), + GET_ALL_CHILDREN_NUMBER(GetAllChildrenNumberCommand::new), + VERSION(VersionCommand::new), + ADD_WATCH(AddWatchCommand::new); + + private Supplier instantiator; + + private CliCommand getInstance() { + return instantiator.get(); + } + + Command(Supplier instantiator) { + this.instantiator = instantiator; + } + } + + /** + * Creates a new {@Code CliCommand} instance. + * @param command the + * @return the new instance + */ + public static CliCommand getInstance (Command command) { + return command.getInstance(); + } +}