Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK-376: SDK should prompt for database uri again if an invalid uri was entered #325

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 31 additions & 24 deletions maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ public class Setup extends AbstractServerTask {
@Parameter(property = "debug")
private String debug;

@Parameter(property = "serverPort")
private String serverPort;

@Parameter(defaultValue = "false", property = "run")
private boolean run;

Expand Down Expand Up @@ -367,18 +370,20 @@ private void wipeDatabase(Server server) throws MojoExecutionException {
}

private void setServerPort(Server server) throws MojoExecutionException {
String message = "What port would you like your server to use?";
String port = wizard.promptForValueIfMissingWithDefault(
message,
server.getParam("tomcat.port"),
"port number",
String.valueOf(Setup.DEFAULT_PORT));
if (!StringUtils.isNumeric(port) || !this.serverHelper.isPort(Integer.parseInt(port))) {
wizard.showMessage("Port must be numeric and less or equal 65535.");
this.setServerPort(server);
return;
}
server.setPort(port);
if (StringUtils.isBlank(serverPort)) {
String message = "What port (-D%s) would you like your server to use?";
serverPort = wizard.promptForValueIfMissingWithDefault(
message,
server.getParam("tomcat.port"),
"serverPort",
String.valueOf(Setup.DEFAULT_PORT));
if (!StringUtils.isNumeric(serverPort) || !this.serverHelper.isPort(Integer.parseInt(serverPort))) {
wizard.showMessage("Port must be numeric and less or equal 65535.");
this.setServerPort(server);
return;
}
server.setPort(serverPort);
}
}

private void setDebugPort(Server server) throws MojoExecutionException {
Expand Down Expand Up @@ -428,34 +433,36 @@ private void setupDatabaseForServer(Server server) throws MojoExecutionException
}

if (server.isMySqlDb() || server.isPostgreSqlDb()) {
String uri = getUriWithoutDb(server);
boolean connectionEstablished = false;
int maxAttempts = 3;
int attempts = 0;

while (!connectionEstablished && attempts < maxAttempts) {
attempts++;
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.");
connectionEstablished = true;
}
catch (SQLException e) {
if (e.getMessage().contains("Invalid database credentials")) {
String message = e.getMessage();
wizard.showMessage(String.format("Database connection failed (attempt %d of %d): %s", attempts, maxAttempts, message));
if (message.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
);
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);
wizard.promptForDbCredentialsAgain(server, dbUser, dbPassword);
continue;
} else if (message.contains("Incorrect Database Uri")) {
if (attempts == maxAttempts) {
throw new MojoExecutionException(String.format("Failed to connect to database after %d attempts. " +
"Please verify your database uri.", maxAttempts), e);
}

wizard.promptForNewUriAndCredentials(server, dbUser, dbPassword, dbUri);
continue;
}
throw new MojoExecutionException("Failed to connect to the specified database " + server.getDbUri(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public DBConnector(String url, String user, String pass, String dbName) throws S
} catch (SQLException e2) {
if (e2.getMessage().contains("Access denied")) {
throw new SQLException("Invalid database credentials. Please check your username and password.", e2);
} else if (e2.getMessage().contains("No suitable driver found for") || e2.getMessage().contains("Communications link failure")) {
throw new SQLException("Incorrect Database Uri. Please provide a correct database uri.", e2);
}
throw e2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,51 @@ public void promptForDbCredentialsIfMissing(Server server) throws MojoExecutionE
server.setDbPassword(dbPassword);
}

@Override
public void promptForDbCredentialsAgain(Server server, String dbUser, String dbPassword) throws MojoExecutionException {
String newUser = promptForValueIfMissingWithDefault("Please specify correct database username (-D%s)", dbUser,
"dbUser", "root");

String newPassword = promptForPasswordIfMissingWithDefault("Please specify correct database password (-D%s)", dbPassword,
"dbPassword", "");

server.setDbUser(newUser);
server.setDbPassword(newPassword);
}

@Override
public void promptForNewUriAndCredentials(Server server, String dbUser, String dbPassword, String dbUri) throws MojoExecutionException {
showMessage("Prompting for new database URI and credentials...");
String uriTemplate;
if (server.isMySqlDb()) {
uriTemplate = SDKConstants.URI_MYSQL;
} else if (server.isPostgreSqlDb()) {
uriTemplate = SDKConstants.URI_POSTGRESQL;
} else {
return;
}

String newUri = promptForValueIfMissingWithDefault(
"The distribution requires a " +
(server.isMySqlDb() ? "MySQL" : "PostgreSQL") +
" database. Please specify a valid database uri (-D%s)",
dbUri, "dbUri", uriTemplate);

if (server.isMySqlDb()) {
newUri = addMySQLParamsIfMissing(newUri);
} else if (server.isPostgreSqlDb()) {
newUri = addPostgreSQLParamsIfMissing(newUri);
}

newUri = newUri.replace(DBNAME_URL_VARIABLE, server.getServerId());
server.setDbUri(newUri);

String newUser = promptForValueIfMissingWithDefault("Please enter the database username (-D%s):", dbUser, "dbUser", "root");
String newPassword = promptForPasswordIfMissingWithDefault("Please enter the database password (-D%s):", dbPassword, "dbPassword", "");
server.setDbUser(newUser);
server.setDbPassword(newPassword);
}

/**
* Get servers with recently used first
*/
Expand Down Expand Up @@ -988,7 +1033,7 @@ private URIBuilder addDefaultParamsIfMissing(String dbUri) throws MojoExecutionE
uri = new URIBuilder(noJdbc);
}
catch (URISyntaxException e) {
throw new MojoExecutionException("Database URI \"" + noJdbc + "\" is not a valid URI " + e.getMessage(), e);
throw new MojoExecutionException("Database URI \"" + noJdbc + "\" is not a valid URI. " + e.getMessage(), e);
}
uri.setParameter("autoReconnect", "true");
uri.setParameter("useUnicode", "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public interface Wizard {

String promptForExistingServerIdIfMissing(String serverId) throws MojoExecutionException;

void promptForNewUriAndCredentials(Server server, String dbUser, String dbPassword, String dbUri) throws MojoExecutionException;

List<String> getListOfServers() throws MojoExecutionException;

void showJdkErrorMessage(String jdk, String platform, String recommendedJdk, String pathToProps);
Expand All @@ -64,5 +66,5 @@ public interface Wizard {

void setAnswers(ArrayDeque<String> batchAnswers);

String promptForPasswordIfMissingWithDefault(String s, String dbPassword, String dbPassword1, String s1) throws MojoExecutionException;
void promptForDbCredentialsAgain(Server server, String dbUser, String dbPassword) throws MojoExecutionException;
}
Loading