From bef83ef9bdd279ad7ee9a5d8cdb8881dafc6bfa8 Mon Sep 17 00:00:00 2001 From: mherman22 Date: Wed, 8 Jan 2025 09:43:19 +0300 Subject: [PATCH] SDK-368: SDK should prompt for the database logins again if invalid logins were entered --- .../java/org/openmrs/maven/plugins/Setup.java | 37 ++++++++++++++++--- .../maven/plugins/utility/DBConnector.java | 9 ++++- .../openmrs/maven/plugins/utility/Wizard.java | 2 + 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java index b1889f69..133afbbe 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java @@ -429,12 +429,37 @@ private void setupDatabaseForServer(Server server) throws MojoExecutionException if (server.isMySqlDb() || server.isPostgreSqlDb()) { String uri = getUriWithoutDb(server); - try (DBConnector connector = new DBConnector(uri, server.getDbUser(), server.getDbPassword(), server.getDbName())) { - connector.checkAndCreate(server); - wizard.showMessage("Connected to the database."); - } - catch (SQLException e) { - throw new MojoExecutionException("Failed to connect to the specified database " + server.getDbUri(), e); + boolean connectionEstablished = false; + int maxAttempts = 3; + int attempts = 0; + + while (!connectionEstablished && attempts < maxAttempts) { + attempts++; + try (DBConnector connector = new DBConnector(uri, server.getDbUser(), server.getDbPassword(), server.getDbName())) { + connector.checkAndCreate(server); + wizard.showMessage("Connected to the database."); + connectionEstablished = true; + } + catch (SQLException e) { + if (e.getMessage().contains("Invalid database credentials")) { + if (attempts == maxAttempts) { + throw new MojoExecutionException( + String.format("Failed to connect to database after %d attempts. Please verify your credentials and try again.", maxAttempts), + e + ); + } + + wizard.showMessage(String.format("Database connection failed (attempt %d of %d): %s", attempts, maxAttempts, e.getMessage())); + String newUser = wizard.promptForValueIfMissingWithDefault("Please specify correct database username (-D%s)", dbUser, "dbUser", "root"); + String newPassword = wizard.promptForPasswordIfMissingWithDefault("Please specify correct database password (-D%s)", dbPassword, "dbPassword", ""); + + server.setDbUser(newUser); + server.setDbPassword(newPassword); + + continue; + } + throw new MojoExecutionException("Failed to connect to the specified database " + server.getDbUri(), e); + } } if (hasDbTables(server)) { diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java index e7d9173b..274ad1c9 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java @@ -22,7 +22,14 @@ public DBConnector(String url, String user, String pass, String dbName) throws S try { this.connection = DriverManager.getConnection(url, user, pass); } catch (SQLException e) { - this.connection = DriverManager.getConnection(url, user, pass); + try { + this.connection = DriverManager.getConnection(url, user, pass); + } catch (SQLException e2) { + if (e2.getMessage().contains("Access denied")) { + throw new SQLException("Invalid database credentials. Please check your username and password.", e2); + } + throw e2; + } } this.dbName = dbName; } diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java index f0d57572..7f3609b8 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java @@ -63,4 +63,6 @@ public interface Wizard { boolean promptForConfirmDistroUpgrade(UpgradeDifferential upgradeDifferential) throws MojoExecutionException; void setAnswers(ArrayDeque batchAnswers); + + String promptForPasswordIfMissingWithDefault(String s, String dbPassword, String dbPassword1, String s1) throws MojoExecutionException; }