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.concurrent.TimeUnit;
24
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.HBaseConfiguration;
27 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.zookeeper.KeeperException;
30 import org.apache.zookeeper.ZooKeeperMain;
31
32
33
34
35
36 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
37 public class ZooKeeperMainServer {
38 private static final String SERVER_ARG = "-server";
39
40 public String parse(final Configuration c) {
41 return ZKConfig.getZKQuorumServersString(c);
42 }
43
44
45
46
47
48 private static class HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain extends ZooKeeperMain {
49 public HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(String[] args)
50 throws IOException, InterruptedException {
51 super(args);
52
53
54 long startTime = System.currentTimeMillis();
55 while (!this.zk.getState().isConnected()) {
56 Thread.sleep(1);
57 if ((System.currentTimeMillis() - startTime) > 10000) {
58 throw new InterruptedException("Failed connect " + this.zk);
59 }
60 }
61 }
62
63
64
65
66
67
68
69 void runCmdLine() throws KeeperException, IOException, InterruptedException {
70 processCmd(this.cl);
71 System.exit(0);
72 }
73 }
74
75
76
77
78
79 private static boolean hasServer(final String args[]) {
80 return args.length > 0 && args[0].equals(SERVER_ARG);
81 }
82
83
84
85
86
87 private static boolean hasCommandLineArguments(final String args[]) {
88 if (hasServer(args)) {
89 if (args.length < 2) throw new IllegalStateException("-server param but no value");
90 return args.length > 2;
91 }
92 return args.length > 0;
93 }
94
95
96
97
98
99 public static void main(String args[]) throws Exception {
100 String [] newArgs = args;
101 if (!hasServer(args)) {
102
103 Configuration conf = HBaseConfiguration.create();
104 String hostport = new ZooKeeperMainServer().parse(conf);
105 if (hostport != null && hostport.length() > 0) {
106 newArgs = new String[args.length + 2];
107 System.arraycopy(args, 0, newArgs, 2, args.length);
108 newArgs[0] = "-server";
109 newArgs[1] = hostport;
110 }
111 }
112
113
114
115 if (hasCommandLineArguments(args)) {
116 HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain zkm =
117 new HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(newArgs);
118 zkm.runCmdLine();
119 } else {
120 ZooKeeperMain.main(newArgs);
121 }
122 }
123 }