From 1439411ebe1535005d6d2a4156eda52029363ab1 Mon Sep 17 00:00:00 2001 From: Florian Bauer Date: Mon, 2 Jan 2023 04:04:46 +0100 Subject: [PATCH] change: Properly throw an exception on failed addServer() instead of returning a null object. --- .../orchestrator/cli/CommandlineControls.java | 4 +-- .../orchestrator/ctx/ConnectionManager.java | 32 +++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/fau/clients/orchestrator/cli/CommandlineControls.java b/src/main/java/de/fau/clients/orchestrator/cli/CommandlineControls.java index c1d18e8..f665ba4 100644 --- a/src/main/java/de/fau/clients/orchestrator/cli/CommandlineControls.java +++ b/src/main/java/de/fau/clients/orchestrator/cli/CommandlineControls.java @@ -97,9 +97,9 @@ private void connectToServerList(List hostPortStrList) { try { hp = HostAndPort.fromString(hostPortStr); conManager.addServer(hp.getHost(), hp.getPort()); - } catch (ServerConnectionException ex) { + } catch (final ServerConnectionException ex) { System.err.println("Could not connect to '" + hostPortStr + "': " + ex.getMessage()); - } catch (IllegalArgumentException ex) { + } catch (final Exception ex) { System.err.println(ex.getMessage()); } } diff --git a/src/main/java/de/fau/clients/orchestrator/ctx/ConnectionManager.java b/src/main/java/de/fau/clients/orchestrator/ctx/ConnectionManager.java index 0b4e1ce..5ca0553 100644 --- a/src/main/java/de/fau/clients/orchestrator/ctx/ConnectionManager.java +++ b/src/main/java/de/fau/clients/orchestrator/ctx/ConnectionManager.java @@ -31,16 +31,18 @@ public static ConnectionManager getInstance() { } /** - * Connect to a server using a trusted certificate or through an unsecure connection - * if {@link ServerManager#setAllowUnsecureConnection(boolean)} has been set to true. - * Note that setting this to true is deprecated and not allowed by the SiLA Standard and should only be used for - * test purposes. + * Connect to a server using a trusted certificate or through an unsecure connection if + * {@link ServerManager#setAllowUnsecureConnection(boolean)} has been set to true. Note that + * setting this to true is deprecated and not allowed by the SiLA Standard and should only be + * used for test purposes. + * * @param host the server host * @param port the server host * @return the server UUID if connection is successful - * @throws ServerConnectionException if unable to connect to the server or is not a valid SiLA Server + * @throws ServerConnectionException if unable to connect to the server or is not a valid SiLA + * Server */ - public UUID addServer(final String host, int port) throws ServerConnectionException { + public UUID addServer(final String host, int port) throws Exception { // method is marked as deprecated but is not, sila_java 0.11.0 will fix this problem serverManager.addServer(host, port); for (final Server server : serverManager.getServers().values()) { @@ -52,21 +54,23 @@ public UUID addServer(final String host, int port) throws ServerConnectionExcept return serverUuid; } } - return null; + throw new Exception("Adding server failed for unknown reason (01)."); } /** - * Connect to a server using a untrusted (self-signed) certificate or through an unsecure connection - * if {@link ServerManager#setAllowUnsecureConnection(boolean)} has been set to true. - * Note that setting this to true is deprecated and not allowed by the SiLA Standard and should only be used for - * test purposes. + * Connect to a server using a untrusted (self-signed) certificate or through an unsecure + * connection if {@link ServerManager#setAllowUnsecureConnection(boolean)} has been set to true. + * Note that setting this to true is deprecated and not allowed by the SiLA Standard and should + * only be used for test purposes. + * * @param host the server host * @param port the server host * @param cert the server untrusted (self-signed) certificate * @return the server UUID if connection is successful - * @throws ServerConnectionException if unable to connect to the server or is not a valid SiLA Server + * @throws ServerConnectionException if unable to connect to the server or is not a valid SiLA + * Server */ - public UUID addServer(final String host, int port, String cert) throws ServerConnectionException { + public UUID addServer(final String host, int port, String cert) throws Exception { serverManager.addServer(host, port, cert); for (final Server server : serverManager.getServers().values()) { if (server.getHost().equals(host) && server.getPort() == port) { @@ -77,7 +81,7 @@ public UUID addServer(final String host, int port, String cert) throws ServerCon return serverUuid; } } - return null; + throw new Exception("Adding server failed for unknown reason (02)."); } private void addServerToContext(final UUID serverUuid, final Server server) {