Search in sources :

Example 1 with CommandModel

use of org.glassfish.api.admin.CommandModel 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 2 with CommandModel

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

the class CommandModelDataProvider method writeToStreamSimpleFormat.

/**
 * Super simple format possible because there can't be any problematic
 * symbol like EOL in attributes.
 *
 * @throws IOException
 */
public void writeToStreamSimpleFormat(CommandModel cm, OutputStream stream) throws IOException {
    if (cm == null) {
        return;
    }
    // @todo Java SE 7: Managed source
    BufferedWriter bw = null;
    OutputStreamWriter writer = null;
    try {
        writer = new OutputStreamWriter(stream, charset);
        bw = new BufferedWriter(writer);
        // command name
        String str = cm.getCommandName();
        if (str != null && !str.isEmpty()) {
            bw.write(ROOT_ELEMENT);
            bw.write(": ");
            bw.write(str);
            bw.newLine();
        }
        // ETag
        bw.write(ETAG_ELEMENT);
        bw.write(": ");
        bw.write(CachedCommandModel.computeETag(cm));
        bw.newLine();
        // unknown are operands
        if (cm.unknownOptionsAreOperands()) {
            bw.write(UNKNOWN_ARE_OPERANDS_ELEMENT);
            bw.write(": true");
            bw.newLine();
        }
        // CachedCommandModel specific staff
        if (cm instanceof CachedCommandModel) {
            CachedCommandModel ccm = (CachedCommandModel) cm;
            // unknown are operands
            if (ccm.isAddedUploadOption()) {
                bw.write(ADDEDUPLOADOPTIONS_ELEMENT);
                bw.write(": true");
                bw.newLine();
            }
            // usage
            str = ccm.getUsage();
            if (str != null && !str.isEmpty()) {
                bw.write(USAGE_ELEMENT);
                bw.write(": ");
                bw.write(escapeEndLines(str));
                bw.newLine();
            }
        }
        // Parameters
        for (CommandModel.ParamModel paramModel : cm.getParameters()) {
            bw.newLine();
            // parameter / name
            bw.write(NAME_ELEMENT);
            bw.write(": ");
            bw.write(paramModel.getName());
            bw.newLine();
            // parameter / class
            if (paramModel.getType() != null) {
                bw.write(CLASS_ELEMENT);
                bw.write(": ");
                bw.write(paramModel.getType().getName());
                bw.newLine();
            }
            Param param = paramModel.getParam();
            // parameter / shortName
            str = param.shortName();
            if (str != null && !str.isEmpty()) {
                bw.write(SHORTNAME_ELEMENT);
                bw.write(": ");
                bw.write(str);
                bw.newLine();
            }
            // parameter / alias
            str = param.alias();
            if (str != null && !str.isEmpty()) {
                bw.write(ALIAS_ELEMENT);
                bw.write(": ");
                bw.write(str);
                bw.newLine();
            }
            // parameter / optional
            if (param.optional()) {
                bw.write(OPTIONAL_ELEMENT);
                bw.write(": true");
                bw.newLine();
            }
            // parameter / obsolete
            if (param.obsolete()) {
                bw.write(OBSOLETE_ELEMENT);
                bw.write(": true");
                bw.newLine();
            }
            // parameter / defaultValue
            str = param.defaultValue();
            if (str != null && !str.isEmpty()) {
                bw.write(DEFAULT_VALUE_ELEMENT);
                bw.write(": ");
                bw.write(str);
                bw.newLine();
            }
            // parameter / primary
            if (param.primary()) {
                bw.write(PRIMARY_ELEMENT);
                bw.write(": true");
                bw.newLine();
            }
            // parameter / multiple
            if (param.multiple()) {
                bw.write(MULTIPLE_ELEMENT);
                bw.write(": true");
                bw.newLine();
            }
            // parameter / password
            if (param.password()) {
                bw.write(PASSWORD_ELEMENT);
                bw.write(": true");
                bw.newLine();
            }
            // parameter / prompt
            if (paramModel instanceof ParamModelData) {
                str = ((ParamModelData) paramModel).getPrompt();
                if (str != null && !str.isEmpty()) {
                    bw.write(PROMPT_ELEMENT);
                    bw.write(": ");
                    bw.write(escapeEndLines(str));
                    bw.newLine();
                }
                str = ((ParamModelData) paramModel).getPromptAgain();
                if (str != null && !str.isEmpty()) {
                    bw.write(PROMPT_AGAIN_ELEMENT);
                    bw.write(": ");
                    bw.write(escapeEndLines(str));
                    bw.newLine();
                }
            }
        }
    } finally {
        try {
            bw.close();
        } catch (Exception ex) {
        }
        try {
            writer.close();
        } catch (Exception ex) {
        }
    }
}
Also used : Param(org.glassfish.api.Param) ParamModelData(com.sun.enterprise.admin.util.CommandModelData.ParamModelData) CachedCommandModel(com.sun.enterprise.admin.util.CachedCommandModel) CommandModel(org.glassfish.api.admin.CommandModel) CachedCommandModel(com.sun.enterprise.admin.util.CachedCommandModel)

Example 3 with CommandModel

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

the class PythonClientClassWriter method generateCommandMethod.

@Override
public void generateCommandMethod(String methodName, String httpMethod, String resourcePath, CommandModel cm) {
    String parametersSignature = Util.getMethodParameterList(cm, true, false);
    Boolean needsMultiPart = parametersSignature.contains("java.io.File");
    String parameters = Util.getMethodParameterList(cm, false, false);
    if (!parameters.isEmpty()) {
        parameters = ", " + parameters;
    }
    StringBuilder merge = new StringBuilder();
    Collection<CommandModel.ParamModel> params = cm.getParameters();
    if ((params != null) && (!params.isEmpty())) {
        for (CommandModel.ParamModel model : params) {
            Param param = model.getParam();
            if (param.optional()) {
                continue;
            }
            String key = (!param.alias().isEmpty()) ? param.alias() : model.getName();
            String paramName = Util.eleminateHypen(model.getName());
            merge.append("        optional['").append(key).append("'] = _").append(paramName).append("\n");
        }
    }
    source.append(TMPL_COMMAND_METHOD.replace("COMMAND", methodName).replace("PARAMS", parameters).replace("MERGE", merge.toString()).replace("PATH", resourcePath).replace("METHOD", httpMethod).replace("MULTIPART", Util.upperCaseFirstLetter(needsMultiPart.toString())));
}
Also used : Param(org.glassfish.api.Param) CommandModel(org.glassfish.api.admin.CommandModel)

Example 4 with CommandModel

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

the class CommandModelStaxProvider method writeContentToStream.

@Override
protected void writeContentToStream(CommandModel proxy, StreamWriter wr) throws Exception {
    if (proxy == null) {
        return;
    }
    wr.writeStartDocument();
    wr.writeStartObject("command");
    wr.writeAttribute("@name", proxy.getCommandName());
    if (proxy.unknownOptionsAreOperands()) {
        wr.writeAttribute("@unknown-options-are-operands", true);
    }
    if (proxy.isManagedJob()) {
        wr.writeAttribute("@managed-job", true);
    }
    String usage = proxy.getUsageText();
    if (StringUtils.ok(usage)) {
        wr.writeAttribute("usage", usage);
    }
    // Options
    wr.writeStartArray("option");
    for (CommandModel.ParamModel p : proxy.getParameters()) {
        Param par = p.getParam();
        wr.writeStartObject("option");
        wr.writeAttribute("@name", p.getName());
        wr.writeAttribute("@type", simplifiedTypeOf(p));
        if (par.primary()) {
            wr.writeAttribute("@primary", true);
        }
        if (par.multiple()) {
            wr.writeAttribute("@multiple", true);
        }
        if (par.optional()) {
            wr.writeAttribute("@optional", true);
        }
        if (par.obsolete()) {
            wr.writeAttribute("@obsolete", true);
        }
        String str = par.shortName();
        if (StringUtils.ok(str)) {
            wr.writeAttribute("@short", str);
        }
        str = par.defaultValue();
        if (StringUtils.ok(str)) {
            wr.writeAttribute("@default", str);
        }
        str = par.acceptableValues();
        if (StringUtils.ok(str)) {
            wr.writeAttribute("@acceptable-values", str);
        }
        str = par.alias();
        if (StringUtils.ok(str)) {
            wr.writeAttribute("@alias", str);
        }
        str = p.getLocalizedDescription();
        if (StringUtils.ok(str)) {
            wr.writeAttribute("@description", str);
        }
        str = p.getLocalizedPrompt();
        if (StringUtils.ok(str)) {
            wr.writeAttribute("@prompt", str);
        }
        str = p.getLocalizedPromptAgain();
        if (StringUtils.ok(str)) {
            wr.writeAttribute("@prompt-again", str);
        }
        wr.writeEndObject();
    }
    wr.writeEndArray();
    // </command>
    wr.writeEndObject();
    wr.writeEndDocument();
}
Also used : Param(org.glassfish.api.Param) CommandModel(org.glassfish.api.admin.CommandModel)

Example 5 with CommandModel

use of org.glassfish.api.admin.CommandModel 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)

Aggregations

CommandModel (org.glassfish.api.admin.CommandModel)10 Param (org.glassfish.api.Param)4 CachedCommandModel (com.sun.enterprise.admin.util.CachedCommandModel)2 CommandException (org.glassfish.api.admin.CommandException)2 CommandRunner (org.glassfish.api.admin.CommandRunner)2 ParameterMap (org.glassfish.api.admin.ParameterMap)2 Test (org.junit.Test)2 Parser (com.sun.enterprise.admin.cli.Parser)1 ParamModelData (com.sun.enterprise.admin.util.CommandModelData.ParamModelData)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 SSLException (javax.net.ssl.SSLException)1