This is an automated email from the ASF dual-hosted git repository.
andor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zookeeper.git
The following commit(s) were added to refs/heads/master by this push:
new 7e6386a ZOOKEEPER-3581: Use factory design pattern to refactor
ZooKeeperMain
7e6386a is described below
commit 7e6386aa9b404dd236b6bdc3b5c714116f3986db
Author: Jono <[email protected]>
AuthorDate: Wed Apr 1 17:02:20 2020 +0200
ZOOKEEPER-3581: Use factory design pattern to refactor ZooKeeperMain
Using a factory design pattern to refactor ZooKeeperMain, making the code
more elegant.
Author: Jono <[email protected]>
Reviewers: [email protected]
Closes #1255 from jono-morris/ZOOKEEPER-3581 and squashes the following
commits:
556e1763e [Jono] Fix style errors with import statements.
e10dfb872 [Jono] Add licence text.
4957d89b5 [Jono] 1. Reinstate missing imports to ZooKeeperMain.
ed76f14da [Jono] Minor Javadoc updates.
cc8de4920 [Jono] Add Factory to create CliCommand classes. Initial Commit.
---
.../java/org/apache/zookeeper/ZooKeeperMain.java | 62 ++++--------------
.../org/apache/zookeeper/cli/CommandFactory.java | 74 ++++++++++++++++++++++
.../apache/zookeeper/cli/CommandFactoryTest.java | 38 +++++++++++
3 files changed, 123 insertions(+), 51 deletions(-)
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 243aca3..ff8ce62 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,17 @@ import java.util.Iterator;
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.CommandFactory;
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.client.ZKClientConfig;
import org.apache.zookeeper.server.ExitCode;
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
@@ -100,34 +79,15 @@ public class ZooKeeperMain {
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() {
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 0000000..87f0163
--- /dev/null
+++
b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CommandFactory.java
@@ -0,0 +1,74 @@
+/*
+ * 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.zookeeper.cli;
+
+import java.util.function.Supplier;
+
+/**
+ * Factory class for creating instances of {@link 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 {@link CliCommand} instance.
+ * @param command the {@link Command} to create a new instance of
+ * @return the new {@code CliCommand} instance
+ */
+ public static CliCommand getInstance (Command command) {
+ return command.getInstance();
+ }
+}
diff --git
a/zookeeper-server/src/test/java/org/apache/zookeeper/cli/CommandFactoryTest.java
b/zookeeper-server/src/test/java/org/apache/zookeeper/cli/CommandFactoryTest.java
new file mode 100644
index 0000000..a118399
--- /dev/null
+++
b/zookeeper-server/src/test/java/org/apache/zookeeper/cli/CommandFactoryTest.java
@@ -0,0 +1,38 @@
+/*
+ * 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.zookeeper.cli;
+
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link CommandFactory}.
+ */
+public class CommandFactoryTest {
+
+ /**
+ * Verify that the {@code CommandFactory} can create a command instance.
+ */
+ @Test
+ public void testCommandCreation() {
+ CliCommand cliCommand =
+ CommandFactory.getInstance(CommandFactory.Command.CREATE);
+ assertTrue(cliCommand instanceof CreateCommand);
+ }
+}