1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.application;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.process.Props;
/**
* Properties of the cluster configuration
*/
final class ClusterProperties {
private static final Logger LOGGER = LoggerFactory.getLogger(ClusterProperties.class);
private final int port;
private final boolean enabled;
private final boolean portAutoincrement;
private final List<String> members;
private final List<String> interfaces;
private final String name;
private final String logLevel;
ClusterProperties(@Nonnull Props props) {
port = props.valueAsInt(ClusterParameters.PORT.getName(), ClusterParameters.PORT.getDefaultValueAsInt());
enabled = props.valueAsBoolean(ClusterParameters.ENABLED.getName(), ClusterParameters.ENABLED.getDefaultValueAsBoolean());
portAutoincrement = props.valueAsBoolean(ClusterParameters.PORT_AUTOINCREMENT.getName(), ClusterParameters.PORT_AUTOINCREMENT.getDefaultValueAsBoolean());
interfaces = extractInterfaces(
props.value(ClusterParameters.INTERFACES.getName(), ClusterParameters.INTERFACES.getDefaultValue())
);
name = props.value(ClusterParameters.NAME.getName(), ClusterParameters.NAME.getDefaultValue());
logLevel = props.value(ClusterParameters.HAZELCAST_LOG_LEVEL.getName(), ClusterParameters.HAZELCAST_LOG_LEVEL.getDefaultValue());
members = extractMembers(
props.value(ClusterParameters.MEMBERS.getName(), ClusterParameters.MEMBERS.getDefaultValue())
);
}
void populateProps(@Nonnull Props props) {
props.set(ClusterParameters.PORT.getName(), Integer.toString(port));
props.set(ClusterParameters.ENABLED.getName(), Boolean.toString(enabled));
props.set(ClusterParameters.PORT_AUTOINCREMENT.getName(), Boolean.toString(portAutoincrement));
props.set(ClusterParameters.INTERFACES.getName(), interfaces.stream().collect(Collectors.joining(",")));
props.set(ClusterParameters.NAME.getName(), name);
props.set(ClusterParameters.HAZELCAST_LOG_LEVEL.getName(), logLevel);
props.set(ClusterParameters.MEMBERS.getName(), members.stream().collect(Collectors.joining(",")));
}
int getPort() {
return port;
}
boolean isEnabled() {
return enabled;
}
boolean isPortAutoincrement() {
return portAutoincrement;
}
List<String> getMembers() {
return members;
}
List<String> getInterfaces() {
return interfaces;
}
String getName() {
return name;
}
String getLogLevel() {
return logLevel;
}
void validate() {
if (!enabled) {
return;
}
// Name is required in cluster mode
checkArgument(
StringUtils.isNotEmpty(name),
"Cluster have been enabled but a %s has not been defined.",
ClusterParameters.NAME.getName()
);
// Test validity of port
checkArgument(
port > 0 && port < 65_536,
"Cluster port have been set to %d which is outside the range [1-65535].",
port
);
// Test the interfaces parameter
try {
List<String> localInterfaces = findAllLocalIPs();
interfaces.forEach(
inet -> checkArgument(
StringUtils.isEmpty(inet) || localInterfaces.contains(inet),
"Interface %s is not available on this machine.",
inet
)
);
} catch (SocketException e) {
LOGGER.warn("Unable to retrieve network interfaces. Interfaces won't be checked", e);
}
}
private static List<String> extractMembers(final String members) {
List<String> result = new ArrayList<>();
for (String member : members.split(",")) {
if (StringUtils.isNotEmpty(member)) {
if (!member.contains(":")) {
result.add(
String.format("%s:%s", member, ClusterParameters.PORT.getDefaultValue())
);
} else {
result.add(member);
}
}
}
return result;
}
private static List<String> extractInterfaces(final String interfaces) {
List<String> result = new ArrayList<>();
for (String iface : interfaces.split(",")) {
if (StringUtils.isNotEmpty(iface)) {
result.add(iface);
}
}
return result;
}
private static List<String> findAllLocalIPs() throws SocketException {
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
List<String> localInterfaces = new ArrayList<>();
while (netInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = netInterfaces.nextElement();
Enumeration<InetAddress> ips = networkInterface.getInetAddresses();
while (ips.hasMoreElements()) {
InetAddress ip = ips.nextElement();
localInterfaces.add(ip.getHostAddress());
}
}
return localInterfaces;
}
private static void checkArgument(boolean expression,
@Nullable String messageTemplate,
@Nullable Object... args) {
if (!expression) {
throw new IllegalArgumentException(String.format(messageTemplate, args));
}
}
}
|