Search in sources :

Example 16 with CommandException

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

the class InstanceInfo method pingInstance.

// TODO what about security????
private Future<InstanceCommandResult> pingInstance() {
    try {
        ActionReport aReport = report.addSubActionsReport();
        InstanceCommandResult aResult = new InstanceCommandResult();
        ParameterMap map = new ParameterMap();
        map.set("type", "terse");
        InstanceCommandExecutor ice = new InstanceCommandExecutor(habitat, "__locations", FailurePolicy.Error, FailurePolicy.Error, svr, host, port, logger, map, aReport, aResult);
        return stateService.submitJob(svr, ice, aResult);
    /*
            String ret = rac.executeCommand(map).trim();

            if (ret == null || (!ret.endsWith("/" + name)))
            return -1;
            running = true;
            String uptimeStr = rac.getAttributes().get("Uptime_value");
            return Long.parseLong(uptimeStr);
             */
    } catch (CommandException ex) {
        running = false;
        return null;
    }
}
Also used : InstanceCommandExecutor(com.sun.enterprise.admin.util.InstanceCommandExecutor) InstanceCommandResult(org.glassfish.api.admin.InstanceCommandResult) ParameterMap(org.glassfish.api.admin.ParameterMap) CommandException(org.glassfish.api.admin.CommandException) ActionReport(org.glassfish.api.ActionReport)

Example 17 with CommandException

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

the class RemoteAdminCommand method executeCommand.

/**
 * Run the command using the specified arguments.
 * Return the output of the command.
 * @param opts
 * @return
 * @throws org.glassfish.api.admin.CommandException
 */
public String executeCommand(ParameterMap opts) throws CommandException {
    // first, make sure we have the command model
    getCommandModel();
    // XXX : This is to take care of camel case from ReST calls that
    // do not go through usual CLI path
    // XXX : This is not clean; this should be handled the same way
    // it is handled for incoming CLI commands
    options = new ParameterMap();
    for (Map.Entry<String, List<String>> o : opts.entrySet()) {
        String key = o.getKey();
        List<String> value = o.getValue();
        options.set(key.toLowerCase(Locale.ENGLISH), value);
    }
    // "DEFAULT".toLowerCase()
    operands = options.get("default");
    try {
        initializeDoUpload();
        // if uploading, we need a payload
        if (doUpload) {
            outboundPayload = PayloadImpl.Outbound.newInstance();
        }
        StringBuilder uriString = getCommandURI();
        ParamModel operandParam = null;
        for (ParamModel opt : commandModel.getParameters()) {
            if (opt.getParam().primary()) {
                operandParam = opt;
                continue;
            }
            String paramName = opt.getName();
            List<String> paramValues = new ArrayList<String>(options.get(paramName.toLowerCase(Locale.ENGLISH)));
            if (!opt.getParam().alias().isEmpty() && !paramName.equalsIgnoreCase(opt.getParam().alias())) {
                paramValues.addAll(options.get(opt.getParam().alias().toLowerCase(Locale.ENGLISH)));
            }
            if (!opt.getParam().multiple() && paramValues.size() > 1) {
                throw new CommandException(strings.get("tooManyOptions", paramName));
            }
            if (paramValues.isEmpty()) {
                // perhaps it's set in the environment?
                String envValue = getFromEnvironment(paramName);
                if (envValue != null) {
                    paramValues.add(envValue);
                }
            }
            if (paramValues.isEmpty()) {
                /*
                     * Option still not set.  Note that we ignore the default
                     * value and don't send it explicitly on the assumption
                     * that the server will supply the default value itself.
                     *
                     * If the missing option is required, that's an error,
                     * which should never happen here because validate()
                     * should check it first.
                     */
                if (!opt.getParam().optional()) {
                    throw new CommandException(strings.get("missingOption", paramName));
                }
                // optional param not set, skip it
                continue;
            }
            for (String paramValue : paramValues) {
                if (opt.getType() == File.class || opt.getType() == File[].class) {
                    addFileOption(uriString, paramName, paramValue);
                } else if (opt.getParam().password()) {
                    addPasswordOption(uriString, paramName, paramValue);
                } else {
                    addStringOption(uriString, paramName, paramValue);
                }
            }
        }
        // add operands
        for (String operand : operands) {
            if (operandParam.getType() == File.class || operandParam.getType() == File[].class) {
                addFileOption(uriString, "DEFAULT", operand);
            } else {
                addStringOption(uriString, "DEFAULT", operand);
            }
        }
        // remove the last character, whether it was "?" or "&"
        uriString.setLength(uriString.length() - 1);
        executeRemoteCommand(uriString.toString());
    } catch (IOException ioex) {
        // possibly an error caused while reading or writing a file?
        throw new CommandException("I/O Error", ioex);
    }
    return output;
}
Also used : ArrayList(java.util.ArrayList) ParamModel(org.glassfish.api.admin.CommandModel.ParamModel) ParameterMap(org.glassfish.api.admin.ParameterMap) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) InvalidCommandException(org.glassfish.api.admin.InvalidCommandException) CommandException(org.glassfish.api.admin.CommandException) IOException(java.io.IOException) Map(java.util.Map) ParameterMap(org.glassfish.api.admin.ParameterMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) SmartFile(com.sun.enterprise.universal.io.SmartFile) File(java.io.File)

Example 18 with CommandException

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

the class RemoteAdminCommand method getCommandModel.

/**
 * Get the CommandModel for the command from the server.
 * If the CommandModel hasn't been set, it's fetched from
 * the server.
 *
 * @return the model for the command
 * @throws CommandException if the server can't be contacted
 */
public CommandModel getCommandModel() throws CommandException {
    if (commandModel == null && !omitCache) {
        long startNanos = System.nanoTime();
        try {
            commandModel = AdminCacheUtils.getCache().get(createCommandCacheKey(), CommandModel.class);
            if (commandModel != null) {
                this.commandModelFromCache = true;
                if (commandModel instanceof CachedCommandModel) {
                    CachedCommandModel ccm = (CachedCommandModel) commandModel;
                    this.usage = ccm.getUsage();
                    addedUploadOption = ccm.isAddedUploadOption();
                }
                if (logger.isLoggable(Level.FINEST)) {
                    logger.log(Level.FINEST, "Command model for command {0} was successfully loaded from the cache. [Duration: {1} nanos]", new Object[] { name, System.nanoTime() - startNanos });
                }
            } else {
                if (logger.isLoggable(Level.FINEST)) {
                    logger.log(Level.FINEST, "Command model for command {0} is not in cache. It must be fatched from server.", name);
                }
            }
        } catch (Exception ex) {
            if (logger.isLoggable(Level.FINEST)) {
                logger.log(Level.FINEST, "Can not get data from cache under key " + createCommandCacheKey(), ex);
            }
        }
    }
    if (commandModel == null) {
        fetchCommandModel();
    }
    return commandModel;
}
Also used : CachedCommandModel(com.sun.enterprise.admin.util.CachedCommandModel) CommandModel(org.glassfish.api.admin.CommandModel) CachedCommandModel(com.sun.enterprise.admin.util.CachedCommandModel) FileNotFoundException(java.io.FileNotFoundException) CommandValidationException(org.glassfish.api.admin.CommandValidationException) SSLException(javax.net.ssl.SSLException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvalidCommandException(org.glassfish.api.admin.InvalidCommandException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) CommandException(org.glassfish.api.admin.CommandException) ConnectException(java.net.ConnectException) MalformedURLException(java.net.MalformedURLException) AuthenticationException(org.glassfish.api.admin.AuthenticationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 19 with CommandException

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

the class CommandExecutorImpl method getParameters.

ParameterMap getParameters(String command, String[] args) throws CommandException {
    CommandModel commandModel = commandRunner.getModel(command, logger);
    if (commandModel == null) {
        throw new CommandException("Command lookup failed for command " + command);
    }
    // Filter out the global options.
    // We are interested only in --passwordfile option. No other options are relevant when GlassFish is running in embedded mode.
    Parser parser = new Parser(args, 0, ProgramOptions.getValidOptions(), true);
    ParameterMap globalOptions = parser.getOptions();
    List<String> operands = parser.getOperands();
    String[] argv = operands.toArray(new String[operands.size()]);
    parser = new Parser(argv, 0, commandModel.getParameters(), false);
    ParameterMap options = parser.getOptions();
    operands = parser.getOperands();
    options.set("DEFAULT", operands);
    // if command has a "terse" option, set it in options
    if (commandModel.getModelFor("terse") != null)
        options.set("terse", Boolean.toString(terse));
    // Read the passwords from the password file and set it in command options.
    if (globalOptions.size() > 0) {
        String pwfile = globalOptions.getOne(ProgramOptions.PASSWORDFILE);
        if (pwfile != null && pwfile.length() > 0) {
            Map<String, String> passwords = CLIUtil.readPasswordFileOptions(pwfile, false);
            for (CommandModel.ParamModel opt : commandModel.getParameters()) {
                if (opt.getParam().password()) {
                    String pwdname = opt.getName();
                    String pwd = passwords.get(pwdname);
                    if (pwd != null) {
                        options.set(pwdname, pwd);
                    }
                }
            }
        }
    }
    return options;
}
Also used : ParameterMap(org.glassfish.api.admin.ParameterMap) CommandModel(org.glassfish.api.admin.CommandModel) CommandException(org.glassfish.api.admin.CommandException) Parser(com.sun.enterprise.admin.cli.Parser)

Example 20 with CommandException

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

the class DeployerImpl method undeploy.

@Override
public void undeploy(String appName, String... params) throws GlassFishException {
    String[] newParams = new String[params.length + 1];
    System.arraycopy(params, 0, newParams, 0, params.length);
    newParams[params.length] = appName;
    CommandExecutorImpl executer = habitat.getService(CommandExecutorImpl.class);
    try {
        ActionReport actionReport = executer.executeCommand("undeploy", newParams);
        if (actionReport.hasSuccesses()) {
            logger.log(Level.INFO, "{0} was successfully undeployed", appName);
        }
    } catch (CommandException e) {
        throw new GlassFishException(e);
    }
}
Also used : GlassFishException(org.glassfish.embeddable.GlassFishException) CommandException(org.glassfish.api.admin.CommandException) ActionReport(org.glassfish.api.ActionReport)

Aggregations

CommandException (org.glassfish.api.admin.CommandException)61 File (java.io.File)20 CommandValidationException (org.glassfish.api.admin.CommandValidationException)16 IOException (java.io.IOException)13 ParameterMap (org.glassfish.api.admin.ParameterMap)11 InvalidCommandException (org.glassfish.api.admin.InvalidCommandException)10 ActionReport (org.glassfish.api.ActionReport)9 ArrayList (java.util.ArrayList)6 RemoteCLICommand (com.sun.enterprise.admin.cli.remote.RemoteCLICommand)5 MiniXmlParserException (com.sun.enterprise.universal.xml.MiniXmlParserException)5 HostAndPort (com.sun.enterprise.util.HostAndPort)4 BackupException (com.sun.enterprise.backup.BackupException)3 BackupWarningException (com.sun.enterprise.backup.BackupWarningException)3 SmartFile (com.sun.enterprise.universal.io.SmartFile)3 FileNotFoundException (java.io.FileNotFoundException)3 UnknownHostException (java.net.UnknownHostException)3 Logger (java.util.logging.Logger)3 SFTPClient (org.glassfish.cluster.ssh.sftp.SFTPClient)3 CLIProcessExecutor (com.sun.enterprise.admin.cli.CLIProcessExecutor)2 PEDomainsManager (com.sun.enterprise.admin.servermgmt.pe.PEDomainsManager)2