Forráskód Böngészése

ZOOKEEPER-3581: Use factory design pattern to refactor ZooKeeperMain

Using a factory design pattern to refactor ZooKeeperMain, making the code more elegant.

Author: Jono <jono.morris@xtra.co.nz>

Reviewers: andor@apache.org

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.
Jono 5 éve
szülő
commit
7e6386aa9b

+ 11 - 51
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() {

+ 74 - 0
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();
+    }
+}

+ 38 - 0
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);
+    }
+}