Search in sources :

Example 1 with ConnectionOptions

use of com.evolveum.midpoint.ninja.opts.ConnectionOptions in project midpoint by Evolveum.

the class NinjaContextTest method setupRepositoryViaMidpointHome.

@Test
public void setupRepositoryViaMidpointHome() throws Exception {
    JCommander jc = NinjaUtils.setupCommandLineParser();
    jc.parse("-m", "./target/midpoint-home", "-U", "jdbc:postgresql://localhost/midpoint", "-u", "midpoint", "-p", "qwe123");
    ConnectionOptions options = NinjaUtils.getOptions(jc, ConnectionOptions.class);
    NinjaContext ctx = new NinjaContext(null);
    ctx.init(options);
    RepositoryService service = ctx.getRepository();
    OperationResult result = new OperationResult("get user");
    PrismObject obj = service.getObject(UserType.class, SystemObjectsType.USER_ADMINISTRATOR.value(), null, result);
    System.out.println(obj.debugDump());
}
Also used : NinjaContext(com.evolveum.midpoint.ninja.impl.NinjaContext) PrismObject(com.evolveum.midpoint.prism.PrismObject) JCommander(com.beust.jcommander.JCommander) ConnectionOptions(com.evolveum.midpoint.ninja.opts.ConnectionOptions) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService) Test(org.testng.annotations.Test)

Example 2 with ConnectionOptions

use of com.evolveum.midpoint.ninja.opts.ConnectionOptions in project midpoint by Evolveum.

the class BaseTest method setupNinjaContext.

protected NinjaContext setupNinjaContext(String[] input) {
    JCommander jc = NinjaUtils.setupCommandLineParser();
    jc.parse(input);
    NinjaContext context = new NinjaContext(jc);
    ConnectionOptions connection = NinjaUtils.getOptions(jc, ConnectionOptions.class);
    context.init(connection);
    return context;
}
Also used : NinjaContext(com.evolveum.midpoint.ninja.impl.NinjaContext) JCommander(com.beust.jcommander.JCommander) ConnectionOptions(com.evolveum.midpoint.ninja.opts.ConnectionOptions)

Example 3 with ConnectionOptions

use of com.evolveum.midpoint.ninja.opts.ConnectionOptions in project midpoint by Evolveum.

the class NinjaUtils method setupCommandLineParser.

public static JCommander setupCommandLineParser() {
    BaseOptions base = new BaseOptions();
    ConnectionOptions connection = new ConnectionOptions();
    JCommander.Builder builder = JCommander.newBuilder().expandAtSign(false).addObject(base).addObject(connection);
    for (Command cmd : Command.values()) {
        builder.addCommand(cmd.getCommandName(), cmd.createOptions());
    }
    JCommander jc = builder.build();
    jc.setProgramName("java [-Dloader.path=<jdbc_driver_jar_path>] -jar ninja.jar");
    jc.setColumnSize(110);
    jc.setAtFileCharset(Charset.forName(base.getCharset()));
    return jc;
}
Also used : BaseOptions(com.evolveum.midpoint.ninja.opts.BaseOptions) Command(com.evolveum.midpoint.ninja.impl.Command) JCommander(com.beust.jcommander.JCommander) ConnectionOptions(com.evolveum.midpoint.ninja.opts.ConnectionOptions)

Example 4 with ConnectionOptions

use of com.evolveum.midpoint.ninja.opts.ConnectionOptions in project midpoint by Evolveum.

the class Main method run.

protected <T> void run(String[] args) {
    JCommander jc = NinjaUtils.setupCommandLineParser();
    try {
        jc.parse(args);
    } catch (ParameterException ex) {
        System.err.println(ex.getMessage());
        return;
    }
    String parsedCommand = jc.getParsedCommand();
    BaseOptions base = Objects.requireNonNull(NinjaUtils.getOptions(jc, BaseOptions.class));
    if (base.isVersion()) {
        try {
            URL versionResource = Objects.requireNonNull(Main.class.getResource("/version"));
            Path path = Paths.get(versionResource.toURI());
            String version = FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8);
            System.out.println(version);
        } catch (Exception ex) {
        // ignored
        }
        return;
    }
    if (base.isHelp() || parsedCommand == null) {
        printHelp(jc, parsedCommand);
        return;
    }
    if (base.isVerbose() && base.isSilent()) {
        System.err.println("Cant' use " + BaseOptions.P_VERBOSE + " and " + BaseOptions.P_SILENT + " together (verbose and silent)");
        printHelp(jc, parsedCommand);
        return;
    }
    NinjaContext context = null;
    try {
        ConnectionOptions connection = Objects.requireNonNull(NinjaUtils.getOptions(jc, ConnectionOptions.class));
        Action<T> action;
        if (connection.isUseWebservice()) {
            action = Command.createRestAction(parsedCommand);
        } else {
            action = Command.createRepositoryAction(parsedCommand);
        }
        if (action == null) {
            String strConnection = connection.isUseWebservice() ? "webservice" : "repository";
            System.err.println("Action for command '" + parsedCommand + "' not found (connection: '" + strConnection + "')");
            return;
        }
        // noinspection unchecked
        T options = (T) jc.getCommands().get(parsedCommand).getObjects().get(0);
        context = new NinjaContext(jc);
        preInit(context);
        action.init(context, options);
        preExecute(context);
        action.execute();
        postExecute(context);
    } catch (Exception ex) {
        handleException(base, ex);
    } finally {
        cleanupResources(base, context);
    }
}
Also used : BaseOptions(com.evolveum.midpoint.ninja.opts.BaseOptions) Path(java.nio.file.Path) NinjaContext(com.evolveum.midpoint.ninja.impl.NinjaContext) JCommander(com.beust.jcommander.JCommander) ParameterException(com.beust.jcommander.ParameterException) ConnectionOptions(com.evolveum.midpoint.ninja.opts.ConnectionOptions) URL(java.net.URL) ParameterException(com.beust.jcommander.ParameterException)

Example 5 with ConnectionOptions

use of com.evolveum.midpoint.ninja.opts.ConnectionOptions in project midpoint by Evolveum.

the class Action method init.

public void init(NinjaContext context, O options) {
    this.context = context;
    this.options = options;
    LogTarget target = getInfoLogTarget();
    log = new Log(target, this.context);
    this.context.setLog(log);
    ConnectionOptions connection = Objects.requireNonNull(NinjaUtils.getOptions(this.context.getJc(), ConnectionOptions.class));
    this.context.init(connection);
}
Also used : Log(com.evolveum.midpoint.ninja.util.Log) ConnectionOptions(com.evolveum.midpoint.ninja.opts.ConnectionOptions) LogTarget(com.evolveum.midpoint.ninja.impl.LogTarget)

Aggregations

ConnectionOptions (com.evolveum.midpoint.ninja.opts.ConnectionOptions)5 JCommander (com.beust.jcommander.JCommander)4 NinjaContext (com.evolveum.midpoint.ninja.impl.NinjaContext)3 BaseOptions (com.evolveum.midpoint.ninja.opts.BaseOptions)2 ParameterException (com.beust.jcommander.ParameterException)1 Command (com.evolveum.midpoint.ninja.impl.Command)1 LogTarget (com.evolveum.midpoint.ninja.impl.LogTarget)1 Log (com.evolveum.midpoint.ninja.util.Log)1 PrismObject (com.evolveum.midpoint.prism.PrismObject)1 RepositoryService (com.evolveum.midpoint.repo.api.RepositoryService)1 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 Test (org.testng.annotations.Test)1