Search in sources :

Example 1 with RemoteCommand

use of com.sun.enterprise.admin.cli.remote.RemoteCommand in project Payara by payara.

the class CLICommand method prevalidate.

/**
 * The prevalidate method supplies missing options from
 * the environment.  It also supplies passwords from the password
 * file or prompts for them if interactive.
 *
 * @throws CommandException if execution of the command fails
 * @throws CommandValidationException if there's something wrong
 *          with the options or arguments
 */
protected void prevalidate() throws CommandException {
    /*
         * First, check that the command has the proper scope.
         * (Could check this in getCommand(), but at that point we
         * don't have the CommandModel yet.)
         * Remote commands are checked on the server.
         */
    if (!(this instanceof RemoteCommand) && !(this instanceof RemoteCLICommand)) {
        Class<? extends Annotation> myScope = getScope(this.getClass());
        if (myScope == null) {
            throw new CommandException(strings.get("NoScope", name));
        } else if (Singleton.class.equals(myScope)) {
            // check that there are no parameters for this command
            if (commandModel.getParameters().size() > 0) {
                throw new CommandException(strings.get("HasParams", name));
            }
        }
    }
    /*
         * Check for missing options and operands.
         */
    Console cons = programOpts.isInteractive() ? System.console() : null;
    boolean missingOption = false;
    for (ParamModel opt : commandModel.getParameters()) {
        if (opt.getParam().password())
            // passwords are handled later
            continue;
        if (opt.getParam().obsolete() && getOption(opt.getName()) != null)
            logger.info(strings.get("ObsoleteOption", opt.getName()));
        if (opt.getParam().optional())
            continue;
        if (opt.getParam().primary())
            continue;
        // if option isn't set, prompt for it (if interactive)
        if (getOption(opt.getName()) == null && cons != null && !missingOption) {
            cons.printf("%s", strings.get("optionPrompt", lc(opt.getName())));
            String val = cons.readLine();
            if (ok(val))
                options.set(opt.getName(), val);
        }
        // if it's still not set, that's an error
        if (getOption(opt.getName()) == null) {
            missingOption = true;
            logger.info(strings.get("missingOption", "--" + opt.getName()));
        }
        if (// a required obsolete option?
        opt.getParam().obsolete())
            logger.info(strings.get("ObsoleteOption", opt.getName()));
    }
    if (missingOption)
        throw new CommandValidationException(strings.get("missingOptions", name));
    int operandMin = 0;
    int operandMax = 0;
    ParamModel operandParam = getOperandModel();
    if (operandParam != null) {
        operandMin = operandParam.getParam().optional() ? 0 : 1;
        operandMax = operandParam.getParam().multiple() ? Integer.MAX_VALUE : 1;
    }
    if (operands.size() < operandMin && cons != null) {
        cons.printf("%s", strings.get("operandPrompt", operandParam.getName()));
        String val = cons.readLine();
        if (ok(val)) {
            operands = new ArrayList<String>();
            operands.add(val);
        }
    }
    if (operands.size() < operandMin)
        throw new CommandValidationException(strings.get("notEnoughOperands", name, operandParam.getType()));
    if (operands.size() > operandMax) {
        if (operandMax == 0)
            throw new CommandValidationException(strings.get("noOperandsAllowed", name));
        else if (operandMax == 1)
            throw new CommandValidationException(strings.get("tooManyOperands1", name));
        else
            throw new CommandValidationException(strings.get("tooManyOperands", name, operandMax));
    }
    initializeCommandPassword();
}
Also used : Singleton(javax.inject.Singleton) RemoteCommand(com.sun.enterprise.admin.cli.remote.RemoteCommand) ParamModel(org.glassfish.api.admin.CommandModel.ParamModel) RemoteCLICommand(com.sun.enterprise.admin.cli.remote.RemoteCLICommand)

Aggregations

RemoteCLICommand (com.sun.enterprise.admin.cli.remote.RemoteCLICommand)1 RemoteCommand (com.sun.enterprise.admin.cli.remote.RemoteCommand)1 Singleton (javax.inject.Singleton)1 ParamModel (org.glassfish.api.admin.CommandModel.ParamModel)1