1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package org.apache.zookeeper.server.command;
20 
21 import java.io.PrintWriter;
22 import org.apache.zookeeper.server.ServerCnxn;
23 import org.apache.zookeeper.server.ServerCnxnFactory;
24 import org.apache.zookeeper.server.ZooKeeperServer;
25 
26 public class CommandExecutor {
27 
28     /**
29      * This class decides which command to be executed and then executes
30      */
execute( ServerCnxn serverCnxn, PrintWriter pwriter, final int commandCode, ZooKeeperServer zkServer, ServerCnxnFactory factory)31     public boolean execute(
32         ServerCnxn serverCnxn,
33         PrintWriter pwriter,
34         final int commandCode,
35         ZooKeeperServer zkServer,
36         ServerCnxnFactory factory) {
37         AbstractFourLetterCommand command = getCommand(serverCnxn, pwriter, commandCode);
38 
39         if (command == null) {
40             return false;
41         }
42 
43         command.setZkServer(zkServer);
44         command.setFactory(factory);
45         command.start();
46         return true;
47     }
48 
getCommand( ServerCnxn serverCnxn, PrintWriter pwriter, final int commandCode)49     private AbstractFourLetterCommand getCommand(
50         ServerCnxn serverCnxn,
51         PrintWriter pwriter,
52         final int commandCode) {
53         AbstractFourLetterCommand command = null;
54         if (commandCode == FourLetterCommands.ruokCmd) {
55             command = new RuokCommand(pwriter, serverCnxn);
56         } else if (commandCode == FourLetterCommands.getTraceMaskCmd) {
57             command = new TraceMaskCommand(pwriter, serverCnxn);
58         } else if (commandCode == FourLetterCommands.enviCmd) {
59             command = new EnvCommand(pwriter, serverCnxn);
60         } else if (commandCode == FourLetterCommands.confCmd) {
61             command = new ConfCommand(pwriter, serverCnxn);
62         } else if (commandCode == FourLetterCommands.srstCmd) {
63             command = new StatResetCommand(pwriter, serverCnxn);
64         } else if (commandCode == FourLetterCommands.crstCmd) {
65             command = new CnxnStatResetCommand(pwriter, serverCnxn);
66         } else if (commandCode == FourLetterCommands.dirsCmd) {
67             command = new DirsCommand(pwriter, serverCnxn);
68         } else if (commandCode == FourLetterCommands.dumpCmd) {
69             command = new DumpCommand(pwriter, serverCnxn);
70         } else if (commandCode == FourLetterCommands.statCmd || commandCode == FourLetterCommands.srvrCmd) {
71             command = new StatCommand(pwriter, serverCnxn, commandCode);
72         } else if (commandCode == FourLetterCommands.consCmd) {
73             command = new ConsCommand(pwriter, serverCnxn);
74         } else if (commandCode == FourLetterCommands.wchpCmd
75                    || commandCode == FourLetterCommands.wchcCmd
76                    || commandCode == FourLetterCommands.wchsCmd) {
77             command = new WatchCommand(pwriter, serverCnxn, commandCode);
78         } else if (commandCode == FourLetterCommands.mntrCmd) {
79             command = new MonitorCommand(pwriter, serverCnxn);
80         } else if (commandCode == FourLetterCommands.isroCmd) {
81             command = new IsroCommand(pwriter, serverCnxn);
82         } else if (commandCode == FourLetterCommands.hashCmd) {
83             command = new DigestCommand(pwriter, serverCnxn);
84         }
85 
86         return command;
87     }
88 
89 }
90