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.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map.Entry;
26 import java.util.Properties;
27
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.hbase.HBaseConfiguration;
30 import org.apache.zookeeper.KeeperException;
31 import org.apache.zookeeper.ZooKeeperMain;
32
33
34
35
36
37 public class ZooKeeperMainServer {
38 private static final String SERVER_ARG = "-server";
39
40 public String parse(final Configuration c) {
41
42
43
44 Properties zkProps = ZKConfig.makeZKProps(c);
45 String clientPort = null;
46 List<String> hosts = new ArrayList<String>();
47 for (Entry<Object, Object> entry: zkProps.entrySet()) {
48 String key = entry.getKey().toString().trim();
49 String value = entry.getValue().toString().trim();
50 if (key.startsWith("server.")) {
51 String[] parts = value.split(":");
52 hosts.add(parts[0]);
53 } else if (key.endsWith("clientPort")) {
54 clientPort = value;
55 }
56 }
57 if (hosts.isEmpty() || clientPort == null) return null;
58 StringBuilder host = new StringBuilder();
59 for (int i = 0; i < hosts.size(); i++) {
60 if (i > 0) host.append("," + hosts.get(i));
61 else host.append(hosts.get(i));
62 host.append(":");
63 host.append(clientPort);
64 }
65 return host.toString();
66 }
67
68
69
70
71
72 private static class HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain extends ZooKeeperMain {
73 public HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(String[] args)
74 throws IOException, InterruptedException {
75 super(args);
76 }
77
78
79
80
81
82
83
84 void runCmdLine() throws KeeperException, IOException, InterruptedException {
85 processCmd(this.cl);
86 System.exit(0);
87 }
88 }
89
90
91
92
93
94 private static boolean hasServer(final String args[]) {
95 return args.length > 0 && args[0].equals(SERVER_ARG);
96 }
97
98
99
100
101
102 private static boolean hasCommandLineArguments(final String args[]) {
103 if (hasServer(args)) {
104 if (args.length < 2) throw new IllegalStateException("-server param but no value");
105 return args.length > 2;
106 }
107 return args.length > 0;
108 }
109
110
111
112
113
114 public static void main(String args[]) throws Exception {
115 String [] newArgs = args;
116 if (!hasServer(args)) {
117
118 Configuration conf = HBaseConfiguration.create();
119 String hostport = new ZooKeeperMainServer().parse(conf);
120 if (hostport != null && hostport.length() > 0) {
121 newArgs = new String[args.length + 2];
122 System.arraycopy(args, 0, newArgs, 2, args.length);
123 newArgs[0] = "-server";
124 newArgs[1] = hostport;
125 }
126 }
127
128 if (hasCommandLineArguments(args)) {
129 HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain zkm =
130 new HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(newArgs);
131 zkm.runCmdLine();
132 } else {
133 ZooKeeperMain.main(newArgs);
134 }
135 }
136 }