|
22 | 22 | import java.io.IOException;
|
23 | 23 | import java.io.InputStream;
|
24 | 24 | import java.io.InputStreamReader;
|
25 |
| -import java.util.List; |
| 25 | +import java.util.*; |
26 | 26 | import java.util.concurrent.Callable;
|
27 | 27 | import java.util.stream.Collectors;
|
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
28 | 30 |
|
29 | 31 | public class Host {
|
30 | 32 |
|
| 33 | + private static final Logger LOGGER = LoggerFactory.getLogger(Host.class); |
| 34 | + |
31 | 35 | private static final String DOCKER_PREFIX = "DOCKER:";
|
32 | 36 |
|
33 | 37 | private static final Gson GSON = new Gson();
|
@@ -130,13 +134,56 @@ public static Process killConnection(String connectionName) {
|
130 | 134 | public static List<ConnectionInfo> listConnections() {
|
131 | 135 | try {
|
132 | 136 | Process process =
|
133 |
| - rabbitmqctl("list_stream_connections --formatter json conn_name,client_properties"); |
134 |
| - return toConnectionInfoList(capture(process.getInputStream())); |
| 137 | + rabbitmqctl("list_stream_connections -q --formatter table conn_name,client_properties"); |
| 138 | + List<ConnectionInfo> connectionInfoList = Collections.emptyList(); |
| 139 | + if (process.exitValue() != 0) { |
| 140 | + LOGGER.warn( |
| 141 | + "Error while trying to list stream connections. Standard output: {}, error output: {}", |
| 142 | + capture(process.getInputStream()), |
| 143 | + capture(process.getErrorStream())); |
| 144 | + return connectionInfoList; |
| 145 | + } |
| 146 | + String content = capture(process.getInputStream()); |
| 147 | + String[] lines = content.split(System.getProperty("line.separator")); |
| 148 | + if (lines.length > 1) { |
| 149 | + connectionInfoList = new ArrayList<>(lines.length - 1); |
| 150 | + for (int i = 1; i < lines.length; i++) { |
| 151 | + String line = lines[i]; |
| 152 | + String[] fields = line.split("\t"); |
| 153 | + String connectionName = fields[0]; |
| 154 | + Map<String, String> clientProperties = Collections.emptyMap(); |
| 155 | + if (fields.length > 1 && fields[1].length() > 1) { |
| 156 | + clientProperties = buildClientProperties(fields); |
| 157 | + } |
| 158 | + connectionInfoList.add(new ConnectionInfo(connectionName, clientProperties)); |
| 159 | + } |
| 160 | + } |
| 161 | + return connectionInfoList; |
135 | 162 | } catch (IOException e) {
|
136 | 163 | throw new RuntimeException(e);
|
137 | 164 | }
|
138 | 165 | }
|
139 | 166 |
|
| 167 | + private static Map<String, String> buildClientProperties(String[] fields) { |
| 168 | + String clientPropertiesString = fields[1]; |
| 169 | + clientPropertiesString = |
| 170 | + clientPropertiesString |
| 171 | + .replace("[", "") |
| 172 | + .replace("]", "") |
| 173 | + .replace("},{", "|") |
| 174 | + .replace("{", "") |
| 175 | + .replace("}", ""); |
| 176 | + Map<String, String> clientProperties = new LinkedHashMap<>(); |
| 177 | + String[] clientPropertyEntries = clientPropertiesString.split("\\|"); |
| 178 | + for (String clientPropertyEntry : clientPropertyEntries) { |
| 179 | + String[] clientProperty = clientPropertyEntry.split("\",\""); |
| 180 | + clientProperties.put( |
| 181 | + clientProperty[0].substring(1), |
| 182 | + clientProperty[1].substring(0, clientProperty[1].length() - 1)); |
| 183 | + } |
| 184 | + return clientProperties; |
| 185 | + } |
| 186 | + |
140 | 187 | static List<ConnectionInfo> toConnectionInfoList(String json) {
|
141 | 188 | return GSON.fromJson(json, new TypeToken<List<ConnectionInfo>>() {}.getType());
|
142 | 189 | }
|
@@ -278,29 +325,30 @@ public void close() throws Exception {
|
278 | 325 |
|
279 | 326 | public static class ConnectionInfo {
|
280 | 327 |
|
281 |
| - private String conn_name; |
282 |
| - private List<List<String>> client_properties; |
| 328 | + private final String name; |
| 329 | + private final Map<String, String> clientProperties; |
| 330 | + |
| 331 | + public ConnectionInfo(String name, Map<String, String> clientProperties) { |
| 332 | + this.name = name; |
| 333 | + this.clientProperties = clientProperties; |
| 334 | + } |
283 | 335 |
|
284 | 336 | public String name() {
|
285 |
| - return this.conn_name; |
| 337 | + return this.name; |
286 | 338 | }
|
287 | 339 |
|
288 | 340 | public String clientProvidedName() {
|
289 |
| - return client_properties.stream() |
290 |
| - .filter(p -> "connection_name".equals(p.get(0))) |
291 |
| - .findFirst() |
292 |
| - .get() |
293 |
| - .get(2); |
| 341 | + return this.clientProperties.get("connection_name"); |
294 | 342 | }
|
295 | 343 |
|
296 | 344 | @Override
|
297 | 345 | public String toString() {
|
298 | 346 | return "ConnectionInfo{"
|
299 |
| - + "conn_name='" |
300 |
| - + conn_name |
| 347 | + + "name='" |
| 348 | + + name |
301 | 349 | + '\''
|
302 |
| - + ", client_properties=" |
303 |
| - + client_properties |
| 350 | + + ", clientProperties=" |
| 351 | + + clientProperties |
304 | 352 | + '}';
|
305 | 353 | }
|
306 | 354 | }
|
|
0 commit comments