1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.linecorp.centraldogma.server;
17
18 import static com.google.common.base.Preconditions.checkArgument;
19 import static java.util.Objects.requireNonNull;
20
21 import java.util.Objects;
22
23 import javax.annotation.Nullable;
24
25 import com.fasterxml.jackson.annotation.JsonCreator;
26 import com.fasterxml.jackson.annotation.JsonProperty;
27 import com.google.common.base.MoreObjects;
28
29
30
31
32 public final class ZooKeeperServerConfig {
33
34 private final String host;
35 private final int quorumPort;
36 private final int electionPort;
37 private final int clientPort;
38 @Nullable
39 private final Integer groupId;
40 private final int weight;
41
42
43
44
45
46
47
48
49
50
51
52 @JsonCreator
53 public ZooKeeperServerConfig(
54 @JsonProperty(value = "host", required = true) String host,
55 @JsonProperty(value = "quorumPort", required = true) int quorumPort,
56 @JsonProperty(value = "electionPort", required = true) int electionPort,
57 @JsonProperty(value = "clientPort", defaultValue = "0") @Nullable Integer clientPort,
58 @JsonProperty("groupId") @Nullable Integer groupId,
59 @JsonProperty(value = "weight", defaultValue = "1") @Nullable Integer weight) {
60
61 this.host = requireNonNull(host, "host");
62 this.quorumPort = validatePort(quorumPort, "quorumPort");
63 this.electionPort = validatePort(electionPort, "electionPort");
64
65 if (clientPort == null) {
66 clientPort = 0;
67 }
68 checkArgument(clientPort >= 0 && clientPort <= 65535,
69 "clientPort: %s (expected: 0-65535)", clientPort);
70 this.clientPort = clientPort;
71 this.groupId = groupId;
72 if (weight == null) {
73 weight = 1;
74 }
75 checkArgument(weight >= 0, "weight: %s (expected: >= 0)", weight);
76 this.weight = weight;
77 }
78
79 private static int validatePort(int port, String name) {
80 checkArgument(port > 0 && port <= 65535, "%s: %s (expected: 1-65535)", name, port);
81 return port;
82 }
83
84
85
86
87 @JsonProperty
88 public String host() {
89 return host;
90 }
91
92
93
94
95 @JsonProperty
96 public int quorumPort() {
97 return quorumPort;
98 }
99
100
101
102
103 @JsonProperty
104 public int electionPort() {
105 return electionPort;
106 }
107
108
109
110
111 @JsonProperty
112 public int clientPort() {
113 return clientPort;
114 }
115
116
117
118
119 @Nullable
120 @JsonProperty
121 public Integer groupId() {
122 return groupId;
123 }
124
125
126
127
128 @JsonProperty
129 public int weight() {
130 return weight;
131 }
132
133 @Override
134 public int hashCode() {
135 return Objects.hash(host, quorumPort, electionPort, clientPort, groupId, weight);
136 }
137
138 @Override
139 public boolean equals(Object o) {
140 if (this == o) {
141 return true;
142 }
143
144 if (o == null || getClass() != o.getClass()) {
145 return false;
146 }
147
148 final ZooKeeperServerConfig that = (ZooKeeperServerConfig) o;
149 return host.equals(that.host) &&
150 quorumPort == that.quorumPort &&
151 electionPort == that.electionPort &&
152 clientPort == that.clientPort &&
153 Objects.equals(groupId, that.groupId) &&
154 weight == that.weight;
155 }
156
157 @Override
158 public String toString() {
159 return MoreObjects.toStringHelper(this)
160 .add("host", host)
161 .add("quorumPort", quorumPort)
162 .add("electionPort", electionPort)
163 .add("clientPort", clientPort)
164 .add("groupId", groupId)
165 .add("weight", weight)
166 .toString();
167 }
168 }