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.util.ArrayList;
23  import java.util.List;
24  import java.util.Map.Entry;
25  import java.util.Properties;
26  
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.HBaseConfiguration;
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  public class ZooKeeperMainServer {
36    public String parse(final Configuration c) {
37      // Note that we do not simply grab the property
38      // HConstants.ZOOKEEPER_QUORUM from the HBaseConfiguration because the
39      // user may be using a zoo.cfg file.
40      Properties zkProps = ZKConfig.makeZKProps(c);
41      String clientPort = null;
42      List<String> hosts = new ArrayList<String>();
43      for (Entry<Object, Object> entry: zkProps.entrySet()) {
44        String key = entry.getKey().toString().trim();
45        String value = entry.getValue().toString().trim();
46        if (key.startsWith("server.")) {
47          String[] parts = value.split(":");
48          hosts.add(parts[0]);
49        } else if (key.endsWith("clientPort")) {
50          clientPort = value;
51        }
52      }
53      if (hosts.isEmpty() || clientPort == null) return null;
54      StringBuilder host = new StringBuilder();
55      for (int i = 0; i < hosts.size(); i++) {
56        if (i > 0)  host.append("," + hosts.get(i));
57        else host.append(hosts.get(i));
58        host.append(":");
59        host.append(clientPort);
60      }
61      return host.toString();
62    }
63  
64    /**
65     * Run the tool.
66     * @param args Command line arguments. First arg is path to zookeepers file.
67     */
68    public static void main(String args[]) throws Exception {
69      Configuration conf = HBaseConfiguration.create();
70      String hostport = new ZooKeeperMainServer().parse(conf);
71      String[] newArgs = args;
72      if (hostport != null && hostport.length() > 0) {
73        newArgs = new String[args.length + 2];
74        System.arraycopy(args, 0, newArgs, 2, args.length);
75        newArgs[0] = "-server";
76        newArgs[1] = hostport;
77      }
78      ZooKeeperMain.main(newArgs);
79    }
80  }