Skip to content

Commit

Permalink
Add Factory to create CliCommand classes. Initial Commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonomorris committed Feb 19, 2020
1 parent 3aa922c commit cc8de49
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, CliCommand> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<? extends CliCommand> instantiator;

private CliCommand getInstance() {
return instantiator.get();
}

Command(Supplier<? extends CliCommand> 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();
}
}

0 comments on commit cc8de49

Please sign in to comment.