Search in sources :

Example 1 with InvalidCommandException

use of org.glassfish.api.admin.InvalidCommandException in project Payara by payara.

the class LocalOSGiShellCommand method executeCommands.

/**
 * Read commands from the specified BufferedReader
 * and execute them.  If printPrompt is set, prompt first.
 *
 * @return the exit code of the last command executed
 */
private int executeCommands(ConsoleReader reader) throws CommandException, CommandValidationException, IOException {
    String line = null;
    int rc = 0;
    /*
         * Any program options we start with are copied to the environment
         * to serve as defaults for commands we run, and then we give each
         * command an empty program options.
         */
    programOpts.toEnvironment(env);
    String sessionId = startSession();
    try {
        for (; ; ) {
            if (printPrompt) {
                line = reader.readLine(shellType + "$ ");
            } else {
                line = reader.readLine();
            }
            if (line == null) {
                if (printPrompt) {
                    System.out.println();
                }
                break;
            }
            if (// ignore comment lines
            line.trim().startsWith("#")) {
                continue;
            }
            String[] args = null;
            try {
                args = getArgs(line);
            } catch (ArgumentTokenizer.ArgumentException ex) {
                logger.info(ex.getMessage());
                continue;
            }
            if (args.length == 0) {
                continue;
            }
            String command = args[0];
            if (command.trim().length() == 0) {
                continue;
            }
            // XXX - care about their arguments?
            if (command.equals("exit") || command.equals("quit")) {
                break;
            }
            ProgramOptions po = null;
            try {
                /*
                     * Every command gets its own copy of program options
                     * so that any program options specified in its
                     * command line options don't effect other commands.
                     * But all commands share the same environment.
                     */
                po = new ProgramOptions(env);
                // copy over AsadminMain info
                po.setClassPath(programOpts.getClassPath());
                po.setClassName(programOpts.getClassName());
                // remove the old one and replace it
                atomicReplace(locator, po);
                args = prepareArguments(sessionId, args);
                args = enhanceForTarget(args);
                String output = cmd.executeAndReturnOutput(args).trim();
                if (output != null && output.length() > 0) {
                    logger.info(output);
                }
            } catch (CommandValidationException cve) {
                logger.severe(cve.getMessage());
                logger.severe(cmd.getUsage());
                rc = ERROR;
            } catch (InvalidCommandException ice) {
                // find closest match with local or remote commands
                logger.severe(ice.getMessage());
            } catch (CommandException ce) {
                logger.severe(ce.getMessage());
                rc = ERROR;
            } finally {
                // restore the original program options
                // XXX - is this necessary?
                atomicReplace(locator, programOpts);
            }
            CLIUtil.writeCommandToDebugLog(name, env, args, rc);
        }
    } finally {
        // what if something breaks on the wire?
        rc = stopSession(sessionId);
    }
    return rc;
}
Also used : InvalidCommandException(org.glassfish.api.admin.InvalidCommandException) CommandValidationException(org.glassfish.api.admin.CommandValidationException) ArgumentTokenizer(com.sun.enterprise.admin.cli.ArgumentTokenizer) InvalidCommandException(org.glassfish.api.admin.InvalidCommandException) CommandException(org.glassfish.api.admin.CommandException) ProgramOptions(com.sun.enterprise.admin.cli.ProgramOptions)

Example 2 with InvalidCommandException

use of org.glassfish.api.admin.InvalidCommandException in project Payara by payara.

the class AdminMain method executeCommand.

public int executeCommand(String[] argv) {
    CLICommand cmd = null;
    try {
        // if the first argument is an option, we're using the new form
        if (argv.length > 0 && argv[0].startsWith("-")) {
            /*
                 * Parse all the admin options, stopping at the first
                 * non-option, which is the command name.
                 */
            Parser rcp = new Parser(argv, 0, ProgramOptions.getValidOptions(), false);
            ParameterMap params = rcp.getOptions();
            po = new ProgramOptions(params, env);
            readAndMergeOptionsFromAuxInput(po);
            List<String> operands = rcp.getOperands();
            argv = operands.toArray(new String[operands.size()]);
        } else {
            po = new ProgramOptions(env);
        }
        po.toEnvironment(env);
        po.setClassPath(classPath);
        po.setClassName(className);
        po.setCommandName(getCommandName());
        if (argv.length == 0) {
            if (po.isHelp()) {
                argv = new String[] { "help" };
            } else {
                argv = new String[] { "multimode" };
            }
        }
        command = argv[0];
        cliContainer.setEnvironment(env);
        cliContainer.setProgramOptions(po);
        cmd = CLICommand.getCommand(cliContainer, command);
        int result = cmd.execute(argv);
        return result;
    } catch (CommandValidationException cve) {
        logger.severe(cve.getMessage());
        if (// error parsing program options
        cmd == null) {
            printUsage();
        } else {
            logger.severe(cmd.getUsage());
        }
        return ERROR;
    } catch (InvalidCommandException ice) {
        // find closest match with local or remote commands
        logger.severe(ice.getMessage());
        try {
            po.setEcho(false);
            CLIUtil.displayClosestMatch(command, CLIUtil.getAllCommands(cliContainer, po, env), strings.get("ClosestMatchedLocalAndRemoteCommands"), logger);
        } catch (InvalidCommandException e) {
        // not a big deal if we cannot help
        }
        return ERROR;
    } catch (CommandException ce) {
        if (ce.getCause() instanceof java.net.ConnectException) {
            // find closest match with local commands
            logger.severe(ce.getMessage());
            try {
                CLIUtil.displayClosestMatch(command, CLIUtil.getLocalCommands(cliContainer), strings.get("ClosestMatchedLocalCommands"), logger);
            } catch (InvalidCommandException e) {
                logger.info(strings.get("InvalidRemoteCommand", command));
            }
        } else {
            logger.severe(ce.getMessage());
        }
        return ERROR;
    }
}
Also used : InvalidCommandException(org.glassfish.api.admin.InvalidCommandException) CommandValidationException(org.glassfish.api.admin.CommandValidationException) ParameterMap(org.glassfish.api.admin.ParameterMap) InvalidCommandException(org.glassfish.api.admin.InvalidCommandException) CommandException(org.glassfish.api.admin.CommandException)

Example 3 with InvalidCommandException

use of org.glassfish.api.admin.InvalidCommandException in project Payara by payara.

the class RemoteAdminCommand method handleResponse.

private void handleResponse(ParameterMap params, InputStream in, int code) throws IOException, CommandException {
    RemoteResponseManager rrm = null;
    try {
        rrm = new RemoteResponseManager(in, code, logger);
        rrm.process();
    } catch (RemoteSuccessException rse) {
        // save results
        output = rse.getMessage();
        assert rrm != null;
        attrs = rrm.getMainAtts();
    } catch (RemoteException rfe) {
        // XXX - gross
        if (rfe.getRemoteCause().indexOf("CommandNotFoundException") >= 0) {
            // the closest matching commands
            throw new InvalidCommandException(rfe.getMessage());
        }
        throw new CommandException("remote failure: " + rfe.getMessage(), rfe);
    }
}
Also used : InvalidCommandException(org.glassfish.api.admin.InvalidCommandException) InvalidCommandException(org.glassfish.api.admin.InvalidCommandException) CommandException(org.glassfish.api.admin.CommandException)

Aggregations

CommandException (org.glassfish.api.admin.CommandException)3 InvalidCommandException (org.glassfish.api.admin.InvalidCommandException)3 CommandValidationException (org.glassfish.api.admin.CommandValidationException)2 ArgumentTokenizer (com.sun.enterprise.admin.cli.ArgumentTokenizer)1 ProgramOptions (com.sun.enterprise.admin.cli.ProgramOptions)1 ParameterMap (org.glassfish.api.admin.ParameterMap)1