Search in sources :

Example 1 with BaseOptions

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

the class NinjaContext method getCharset.

public Charset getCharset() {
    BaseOptions base = NinjaUtils.getOptions(jc, BaseOptions.class);
    String charset = base.getCharset();
    return Charset.forName(charset);
}
Also used : BaseOptions(com.evolveum.midpoint.ninja.opts.BaseOptions)

Example 2 with BaseOptions

use of com.evolveum.midpoint.ninja.opts.BaseOptions 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 3 with BaseOptions

use of com.evolveum.midpoint.ninja.opts.BaseOptions 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)

Aggregations

BaseOptions (com.evolveum.midpoint.ninja.opts.BaseOptions)3 JCommander (com.beust.jcommander.JCommander)2 ConnectionOptions (com.evolveum.midpoint.ninja.opts.ConnectionOptions)2 ParameterException (com.beust.jcommander.ParameterException)1 Command (com.evolveum.midpoint.ninja.impl.Command)1 NinjaContext (com.evolveum.midpoint.ninja.impl.NinjaContext)1 URL (java.net.URL)1 Path (java.nio.file.Path)1