1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
33
34
35 public class ZooKeeperMainServer {
36 public String parse(final Configuration c) {
37
38
39
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
66
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 }