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