Search in sources :

Example 61 with ParsedCommandLine

use of org.jboss.as.cli.operation.ParsedCommandLine in project wildfly-core by wildfly.

the class PatchHandler method doInspect.

protected void doInspect(CommandContext ctx) throws CommandLineException {
    final ParsedCommandLine parsedLine = ctx.getParsedCommandLine();
    final String patchPath = path.getValue(parsedLine, true);
    final File patchFile = new File(patchPath);
    if (!patchFile.exists()) {
        throw new CommandLineException("Failed to locate " + patchFile.getAbsolutePath());
    }
    ZipFile patchZip = null;
    InputStream is = null;
    try {
        patchZip = new ZipFile(patchFile);
        ZipEntry patchXmlEntry = patchZip.getEntry(PatchBundleXml.MULTI_PATCH_XML);
        if (patchXmlEntry == null) {
            patchXmlEntry = patchZip.getEntry(PatchXml.PATCH_XML);
            if (patchXmlEntry == null) {
                throw new CommandLineException("Neither " + PatchBundleXml.MULTI_PATCH_XML + " nor " + PatchXml.PATCH_XML + " were found in " + patchFile.getAbsolutePath());
            }
            is = patchZip.getInputStream(patchXmlEntry);
            final Patch patch = PatchXml.parse(is).resolvePatch(null, null);
            displayPatchXml(ctx, patch);
        } else {
            is = patchZip.getInputStream(patchXmlEntry);
            final List<BundledPatchEntry> patches = PatchBundleXml.parse(is).getPatches();
            displayPatchBundleXml(ctx, patches, patchZip);
        }
    } catch (ZipException e) {
        throw new CommandLineException("Failed to open " + patchFile.getAbsolutePath(), e);
    } catch (IOException e) {
        throw new CommandLineException("Failed to open " + patchFile.getAbsolutePath(), e);
    } catch (PatchingException e) {
        throw new CommandLineException("Failed to resolve parsed patch", e);
    } catch (XMLStreamException e) {
        throw new CommandLineException("Failed to parse patch.xml", e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
        if (patchZip != null) {
            try {
                patchZip.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : PatchingException(org.jboss.as.patching.PatchingException) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) CommandLineException(org.jboss.as.cli.CommandLineException) BundledPatchEntry(org.jboss.as.patching.metadata.BundledPatch.BundledPatchEntry) ZipFile(java.util.zip.ZipFile) XMLStreamException(javax.xml.stream.XMLStreamException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) ZipFile(java.util.zip.ZipFile) File(java.io.File) Patch(org.jboss.as.patching.metadata.Patch)

Example 62 with ParsedCommandLine

use of org.jboss.as.cli.operation.ParsedCommandLine in project wildfly-core by wildfly.

the class SetVariableHandler method doHandle.

/* (non-Javadoc)
     * @see org.jboss.as.cli.handlers.CommandHandlerWithHelp#doHandle(org.jboss.as.cli.CommandContext)
     */
@Override
protected void doHandle(CommandContext ctx) throws CommandLineException {
    ParsedCommandLine parsedArgs = ctx.getParsedCommandLine();
    final List<String> vars = parsedArgs.getOtherProperties();
    if (vars.isEmpty()) {
        final Collection<String> defined = ctx.getVariables();
        if (defined.isEmpty()) {
            return;
        }
        final List<String> pairs = new ArrayList<String>(defined.size());
        for (String var : defined) {
            pairs.add(var + '=' + ctx.getVariable(var));
        }
        Collections.sort(pairs);
        for (String pair : pairs) {
            ctx.printLine(pair);
        }
        return;
    }
    for (String arg : vars) {
        arg = ArgumentWithValue.resolveValue(arg);
        if (arg.charAt(0) == '$') {
            arg = arg.substring(1);
            if (arg.isEmpty()) {
                throw new CommandFormatException("Variable name is missing after '$'");
            }
        }
        final int equals = arg.indexOf('=');
        if (equals < 1) {
            throw new CommandFormatException("'=' is missing for variable '" + arg + "'");
        }
        final String name = arg.substring(0, equals);
        if (name.isEmpty()) {
            throw new CommandFormatException("The name is missing in '" + arg + "'");
        }
        if (equals == arg.length() - 1) {
            ctx.setVariable(name, null);
        } else {
            String value = arg.substring(equals + 1);
            if (value.length() > 2 && value.charAt(0) == '`' && value.charAt(value.length() - 1) == '`') {
                value = Util.getResult(ctx, value.substring(1, value.length() - 1));
            }
            ctx.setVariable(name, value);
        }
    }
}
Also used : CommandFormatException(org.jboss.as.cli.CommandFormatException) ArrayList(java.util.ArrayList) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine)

Example 63 with ParsedCommandLine

use of org.jboss.as.cli.operation.ParsedCommandLine in project wildfly-core by wildfly.

the class ShutdownHandler method buildRequestWithoutHeaders.

@Override
protected ModelNode buildRequestWithoutHeaders(CommandContext ctx) throws CommandFormatException {
    final ModelNode op = new ModelNode();
    final ParsedCommandLine args = ctx.getParsedCommandLine();
    if (ctx.isDomainMode()) {
        final String hostName = host.getValue(args);
        if (hostName == null) {
            throw new CommandFormatException("Missing required argument " + host.getFullName());
        }
        op.get(Util.ADDRESS).add(Util.HOST, hostName);
        if (timeout.isPresent(args)) {
            throw new CommandFormatException(timeout.getFullName() + " is not allowed in the domain mode.");
        }
    } else {
        if (host.isPresent(args)) {
            throw new CommandFormatException(host.getFullName() + " is not allowed in the standalone mode.");
        }
        if (timeout.isPresent(args) && suspendTimeout.isPresent(args)) {
            throw new CommandFormatException(timeout.getFullName() + " cannot be used in conjunction with suspend-timeout.");
        }
        op.get(Util.ADDRESS).setEmptyList();
    }
    op.get(Util.OPERATION).set(Util.SHUTDOWN);
    setBooleanArgument(args, op, restart, Util.RESTART);
    setIntArgument(args, op, timeout, Util.TIMEOUT);
    setIntArgument(args, op, suspendTimeout, Util.SUSPEND_TIMEOUT);
    return op;
}
Also used : CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) ModelNode(org.jboss.dmr.ModelNode)

Example 64 with ParsedCommandLine

use of org.jboss.as.cli.operation.ParsedCommandLine in project wildfly-core by wildfly.

the class UndeployHandler method buildRequestWithoutHeaders.

@Override
public ModelNode buildRequestWithoutHeaders(CommandContext ctx) throws CommandFormatException {
    final ParsedCommandLine args = ctx.getParsedCommandLine();
    String p = this.path.getValue(args);
    if (p != null) {
        File f = new File(p);
        if (DeployArchiveCommand.isCliArchive(f)) {
            UndeployArchiveCommand command = new UndeployArchiveCommand(ctx);
            command.file = f;
            command.script = script.getValue(args);
            return command.buildRequest(ctx);
        }
    }
    if (name == null) {
        throw new CommandFormatException("Deployment name is missing.");
    }
    UndeployCommand command = null;
    boolean keepContent = this.keepContent.isPresent(args);
    if (keepContent) {
        command = new DisableCommand(ctx);
    } else {
        command = new UndeployCommand(ctx);
    }
    final String name = this.name.getValue(ctx.getParsedCommandLine());
    command.allRelevantServerGroups = allRelevantServerGroups.isPresent(args);
    if (name != null) {
        command.name = name;
    }
    command.serverGroups = serverGroups.getValue(args);
    return command.buildRequest(ctx);
}
Also used : UndeployCommand(org.jboss.as.cli.impl.aesh.cmd.deployment.UndeployCommand) CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) File(java.io.File) DisableCommand(org.jboss.as.cli.impl.aesh.cmd.deployment.DisableCommand) UndeployArchiveCommand(org.jboss.as.cli.impl.aesh.cmd.deployment.UndeployArchiveCommand)

Example 65 with ParsedCommandLine

use of org.jboss.as.cli.operation.ParsedCommandLine in project wildfly-core by wildfly.

the class UndeployHandler method doHandle.

@Override
protected void doHandle(CommandContext ctx) throws CommandLineException {
    final ParsedCommandLine args = ctx.getParsedCommandLine();
    final boolean l = this.l.isPresent(args);
    if (!args.hasProperties() || l) {
        try {
            ListCommand.listDeployments(ctx, l);
        } catch (CommandException ex) {
            throw new CommandLineException(ex.getLocalizedMessage());
        }
        return;
    }
    final String path = this.path.getValue(args);
    final File f;
    if (path != null) {
        f = new File(path);
        if (DeployArchiveCommand.isCliArchive(f)) {
            UndeployArchiveCommand command = new UndeployArchiveCommand(ctx);
            command.file = f;
            command.script = script.getValue(args);
            try {
                command.execute(ctx);
            } catch (CommandException ex) {
                throw new CommandLineException(ex.getLocalizedMessage());
            }
            return;
        }
    }
    final String name = this.name.getValue(ctx.getParsedCommandLine());
    if (name == null) {
        try {
            ListCommand.listDeployments(ctx, l);
        } catch (CommandException ex) {
            throw new CommandLineException(ex.getLocalizedMessage());
        }
        return;
    }
    UndeployCommand command = null;
    boolean keepContent = this.keepContent.isPresent(args);
    if (keepContent) {
        command = new DisableCommand(ctx);
    } else {
        command = new UndeployCommand(ctx);
    }
    command.allRelevantServerGroups = allRelevantServerGroups.isPresent(args);
    final ModelNode headersNode = headers.toModelNode(ctx);
    if (headersNode != null && headersNode.getType() != ModelType.OBJECT) {
        throw new CommandFormatException("--headers option has wrong value '" + headersNode + "'");
    }
    command.headers = headersNode;
    if (name != null) {
        command.name = name;
    }
    command.serverGroups = serverGroups.getValue(args);
    try {
        command.execute(ctx);
    } catch (CommandException ex) {
        throw new CommandLineException(ex.getLocalizedMessage());
    }
}
Also used : UndeployCommand(org.jboss.as.cli.impl.aesh.cmd.deployment.UndeployCommand) CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) CommandException(org.aesh.command.CommandException) ModelNode(org.jboss.dmr.ModelNode) File(java.io.File) CommandLineException(org.jboss.as.cli.CommandLineException) DisableCommand(org.jboss.as.cli.impl.aesh.cmd.deployment.DisableCommand) UndeployArchiveCommand(org.jboss.as.cli.impl.aesh.cmd.deployment.UndeployArchiveCommand)

Aggregations

ParsedCommandLine (org.jboss.as.cli.operation.ParsedCommandLine)95 Test (org.junit.Test)42 CommandFormatException (org.jboss.as.cli.CommandFormatException)38 ModelNode (org.jboss.dmr.ModelNode)26 CommandLineException (org.jboss.as.cli.CommandLineException)14 OperationRequestAddress (org.jboss.as.cli.operation.OperationRequestAddress)14 File (java.io.File)12 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)9 DefaultOperationRequestAddress (org.jboss.as.cli.operation.impl.DefaultOperationRequestAddress)8 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)8 CommandArgument (org.jboss.as.cli.CommandArgument)4 CommandContext (org.jboss.as.cli.CommandContext)4 OperationFormatException (org.jboss.as.cli.operation.OperationFormatException)4 ZipException (java.util.zip.ZipException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 CommandException (org.aesh.command.CommandException)3 BatchManager (org.jboss.as.cli.batch.BatchManager)3 CommandLineParser (org.jboss.as.cli.operation.CommandLineParser)3 DefaultCallbackHandler (org.jboss.as.cli.operation.impl.DefaultCallbackHandler)3