Skip to content

Commit

Permalink
MID-8842 ninja - improved parsing and looging, getting rid of few todos
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed Jul 7, 2023
1 parent 2be000a commit 36550dd
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;

import com.evolveum.midpoint.ninja.util.InputParameterException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.fusesource.jansi.AnsiConsole;
Expand Down Expand Up @@ -113,6 +116,8 @@ protected <T> Object run(String[] args) {
} finally {
action.destroy();
}
} catch (InputParameterException ex) {
err.println("ERROR: " + ex.getMessage());
} catch (Exception ex) {
handleException(base, ex);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
import com.evolveum.midpoint.common.configuration.api.MidpointConfiguration;
import com.evolveum.midpoint.init.AuditFactory;
import com.evolveum.midpoint.ninja.impl.NinjaApplicationContextLevel;
import com.evolveum.midpoint.ninja.util.ConsoleFormat;
import com.evolveum.midpoint.ninja.util.InputParameterException;
import com.evolveum.midpoint.ninja.util.NinjaUtils;
import com.evolveum.midpoint.repo.api.RepositoryServiceFactoryException;
import com.evolveum.midpoint.repo.sqale.SqaleRepositoryConfiguration;
import com.evolveum.midpoint.repo.sqale.audit.SqaleAuditServiceFactory;
import com.evolveum.midpoint.repo.sqlbase.DataSourceFactory;

// todo proper logging
public class RunSqlAction extends Action<RunSqlOptions, Void> {

@Override
Expand All @@ -57,11 +58,7 @@ private boolean preferCustomJdbcConnection(List<Object> allOptions) {
}

ConnectionOptions connectionOpts = NinjaUtils.getOptions(allOptions, ConnectionOptions.class);
if (connectionOpts == null || StringUtils.isEmpty(connectionOpts.getMidpointHome())) {
return true;
}

return false;
return connectionOpts == null || StringUtils.isEmpty(connectionOpts.getMidpointHome());
}

@Override
Expand Down Expand Up @@ -134,15 +131,15 @@ private void setScriptsDefaults(RunSqlOptions.Mode mode, RunSqlOptions options)

private HikariDataSource setupCustomDataSource() {
if (StringUtils.isEmpty(options.getJdbcUrl())) {
throw new IllegalStateException("JDBC url parameter not defined");
throw new InputParameterException("JDBC url parameter not defined");
}

if (StringUtils.isEmpty(options.getJdbcUsername())) {
throw new IllegalStateException("JDBC username parameter not defined");
throw new InputParameterException("JDBC username parameter not defined");
}

if (StringUtils.isEmpty(options.getPassword())) {
throw new IllegalStateException("JDBC password parameter not defined");
throw new InputParameterException("JDBC password parameter not defined");
}

HikariConfig config = new HikariConfig();
Expand Down Expand Up @@ -235,15 +232,16 @@ private void executeScripts(@NotNull DataSource dataSource, @NotNull List<File>
}

private void printStatementResults(Statement stmt, boolean hasResult, int index) throws SQLException {
String resultHeader = ConsoleFormat.formatInfoMessageWithParameter("Result #", index) + ": ";
if (!hasResult) {
int updateCount = stmt.getUpdateCount();
if (updateCount != -1) {
context.out.println("Result #" + index + ": " + updateCount + " updated rows ");
context.out.println(resultHeader + updateCount + " updated rows ");
}
return;
}

context.out.println("Result set #" + index + ":");
context.out.println(resultHeader);
try (ResultSet set = stmt.getResultSet()) {
printResultSet(set);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;

import com.evolveum.midpoint.ninja.util.NonBlankValidator;
import com.evolveum.midpoint.ninja.util.RunModeConverterValidator;

@Parameters(resourceBundle = "messages", commandDescriptionKey = "runSql")
Expand Down Expand Up @@ -65,16 +66,16 @@ public enum Mode {
@Parameter(names = { P_MODE }, descriptionKey = "runSql.mode", validateWith = RunModeConverterValidator.class, converter = RunModeConverterValidator.class)
private Mode mode;

@Parameter(names = { P_JDBC_URL_LONG }, descriptionKey = "runSql.jdbcUrl")
@Parameter(names = { P_JDBC_URL_LONG }, descriptionKey = "runSql.jdbcUrl", validateWith = NonBlankValidator.class)
private String jdbcUrl;

@Parameter(names = { P_JDBC_USERNAME_LONG }, descriptionKey = "runSql.jdbcUsername")
@Parameter(names = { P_JDBC_USERNAME_LONG }, descriptionKey = "runSql.jdbcUsername", validateWith = NonBlankValidator.class)
private String jdbcUsername;

@Parameter(names = { P_JDBC_PASSWORD_LONG }, descriptionKey = "runSql.jdbcPassword")
@Parameter(names = { P_JDBC_PASSWORD_LONG }, descriptionKey = "runSql.jdbcPassword", validateWith = NonBlankValidator.class)
private String jdbcPassword;

@Parameter(names = { P_JDBC_ASK_PASSWORD_LONG }, descriptionKey = "runSql.jdbcAskPassword", password = true)
@Parameter(names = { P_JDBC_ASK_PASSWORD_LONG }, descriptionKey = "runSql.jdbcAskPassword", password = true, validateWith = NonBlankValidator.class)
private String jdbcAskPassword;

@Parameter(names = { P_UPGRADE }, descriptionKey = "runSql.upgrade")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ private void verifyAsCsv(Writer writer, PrismObject<?> object, UpgradeValidation
}

private List<String> createReportRecord(UpgradeValidationItem item, PrismObject<?> object) {
// todo populate
String identifier = item.getIdentifier();
UpgradePhase phase = item.getPhase();
UpgradePriority priority = item.getPriority();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2010-2023 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.ninja.util;

/**
* Exception that is thrown when action execution is in progress and input parameters combination is invalid,
* e.g. when two mutually exclusive parameters are specified
*
* Not to be used for validation of individual parameters, parameter parsing using {@link com.beust.jcommander.IParameterValidator}
* that happens in JCommander throws {@link com.beust.jcommander.ParameterException} for that.
*/
public class InputParameterException extends RuntimeException {

public InputParameterException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2010-2023 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.ninja.util;

import com.beust.jcommander.IParameterValidator;
import com.beust.jcommander.ParameterException;
import org.apache.commons.lang3.StringUtils;

public class NonBlankValidator implements IParameterValidator {

@Override
public void validate(String name, String value) throws ParameterException {
if (StringUtils.isBlank(value)) {
throw new ParameterException("Parameter " + name + " should not be empty");
}
}
}

0 comments on commit 36550dd

Please sign in to comment.