View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.zookeeper;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseConfiguration;
26  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.zookeeper.KeeperException;
29  import org.apache.zookeeper.ZooKeeperMain;
30  
31  /**
32   * Tool for running ZookeeperMain from HBase by  reading a ZooKeeper server
33   * from HBase XML configuration.
34   */
35  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
36  public class ZooKeeperMainServer {
37    private static final String SERVER_ARG = "-server";
38  
39    public String parse(final Configuration c) {
40      return ZKConfig.getZKQuorumServersString(c);
41    }
42  
43    /**
44     * ZooKeeper 3.4.6 broke being able to pass commands on command line.
45     * See ZOOKEEPER-1897.  This class is a hack to restore this faclity.
46     */
47    private static class HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain extends ZooKeeperMain {
48      public HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(String[] args)
49      throws IOException, InterruptedException {
50        super(args);
51      }
52  
53      /**
54       * Run the command-line args passed.  Calls System.exit when done.
55       * @throws KeeperException
56       * @throws IOException
57       * @throws InterruptedException
58       */
59      void runCmdLine() throws KeeperException, IOException, InterruptedException {
60        processCmd(this.cl);
61        System.exit(0);
62      }
63    }
64  
65    /**
66     * @param args
67     * @return True if argument strings have a '-server' in them.
68     */
69    private static boolean hasServer(final String args[]) {
70      return args.length > 0 && args[0].equals(SERVER_ARG);
71    }
72  
73    /**
74     * @param args
75     * @return True if command-line arguments were passed.
76     */
77    private static boolean hasCommandLineArguments(final String args[]) {
78      if (hasServer(args)) {
79        if (args.length < 2) throw new IllegalStateException("-server param but no value");
80        return args.length > 2;
81      }
82      return args.length > 0;
83    }
84  
85    /**
86     * Run the tool.
87     * @param args Command line arguments. First arg is path to zookeepers file.
88     */
89    public static void main(String args[]) throws Exception {
90      String [] newArgs = args;
91      if (!hasServer(args)) {
92        // Add the zk ensemble from configuration if none passed on command-line.
93        Configuration conf = HBaseConfiguration.create();
94        String hostport = new ZooKeeperMainServer().parse(conf);
95        if (hostport != null && hostport.length() > 0) {
96          newArgs = new String[args.length + 2];
97          System.arraycopy(args, 0, newArgs, 2, args.length);
98          newArgs[0] = "-server";
99          newArgs[1] = hostport;
100       }
101     }
102     // If command-line arguments, run our hack so they are executed.
103     if (hasCommandLineArguments(args)) {
104       HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain zkm =
105         new HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(newArgs);
106       zkm.runCmdLine();
107     } else {
108       ZooKeeperMain.main(newArgs);
109     }
110   }
111 }