Skip to content

Commit eabdcbd

Browse files
authored
SDK-368: SDK should prompt for the database logins again if invalid logins were entered (#324)
1 parent 0280368 commit eabdcbd

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,37 @@ private void setupDatabaseForServer(Server server) throws MojoExecutionException
429429

430430
if (server.isMySqlDb() || server.isPostgreSqlDb()) {
431431
String uri = getUriWithoutDb(server);
432-
try (DBConnector connector = new DBConnector(uri, server.getDbUser(), server.getDbPassword(), server.getDbName())) {
433-
connector.checkAndCreate(server);
434-
wizard.showMessage("Connected to the database.");
435-
}
436-
catch (SQLException e) {
437-
throw new MojoExecutionException("Failed to connect to the specified database " + server.getDbUri(), e);
432+
boolean connectionEstablished = false;
433+
int maxAttempts = 3;
434+
int attempts = 0;
435+
436+
while (!connectionEstablished && attempts < maxAttempts) {
437+
attempts++;
438+
try (DBConnector connector = new DBConnector(uri, server.getDbUser(), server.getDbPassword(), server.getDbName())) {
439+
connector.checkAndCreate(server);
440+
wizard.showMessage("Connected to the database.");
441+
connectionEstablished = true;
442+
}
443+
catch (SQLException e) {
444+
if (e.getMessage().contains("Invalid database credentials")) {
445+
if (attempts == maxAttempts) {
446+
throw new MojoExecutionException(
447+
String.format("Failed to connect to database after %d attempts. Please verify your credentials and try again.", maxAttempts),
448+
e
449+
);
450+
}
451+
452+
wizard.showMessage(String.format("Database connection failed (attempt %d of %d): %s", attempts, maxAttempts, e.getMessage()));
453+
String newUser = wizard.promptForValueIfMissingWithDefault("Please specify correct database username (-D%s)", dbUser, "dbUser", "root");
454+
String newPassword = wizard.promptForPasswordIfMissingWithDefault("Please specify correct database password (-D%s)", dbPassword, "dbPassword", "");
455+
456+
server.setDbUser(newUser);
457+
server.setDbPassword(newPassword);
458+
459+
continue;
460+
}
461+
throw new MojoExecutionException("Failed to connect to the specified database " + server.getDbUri(), e);
462+
}
438463
}
439464

440465
if (hasDbTables(server)) {

maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ public DBConnector(String url, String user, String pass, String dbName) throws S
2222
try {
2323
this.connection = DriverManager.getConnection(url, user, pass);
2424
} catch (SQLException e) {
25-
this.connection = DriverManager.getConnection(url, user, pass);
25+
try {
26+
this.connection = DriverManager.getConnection(url, user, pass);
27+
} catch (SQLException e2) {
28+
if (e2.getMessage().contains("Access denied")) {
29+
throw new SQLException("Invalid database credentials. Please check your username and password.", e2);
30+
}
31+
throw e2;
32+
}
2633
}
2734
this.dbName = dbName;
2835
}

sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@ public interface Wizard {
6363
boolean promptForConfirmDistroUpgrade(UpgradeDifferential upgradeDifferential) throws MojoExecutionException;
6464

6565
void setAnswers(ArrayDeque<String> batchAnswers);
66+
67+
String promptForPasswordIfMissingWithDefault(String s, String dbPassword, String dbPassword1, String s1) throws MojoExecutionException;
6668
}

0 commit comments

Comments
 (0)