Package org.jboss.as.cli.batch

Examples of org.jboss.as.cli.batch.Batch


        BatchManager batchManager = ctx.getBatchManager();
        if(!batchManager.isBatchActive()) {
            throw new CommandFormatException("No active batch.");
        }

        Batch batch = batchManager.getActiveBatch();
        final int batchSize = batch.size();
        if(batchSize == 0) {
            throw new CommandFormatException("The batch is empty.");
        }

        String argsStr = ctx.getArgumentsString();
        if(argsStr == null) {
            throw new CommandFormatException("Missing line number.");
        }

        int i = 0;
        while(i < argsStr.length()) {
            if(Character.isWhitespace(argsStr.charAt(i))) {
                break;
            }
            ++i;
        }

        if(i == argsStr.length()) {
            throw new CommandFormatException("Missing the new command line after the index.");
        }

        String intStr = argsStr.substring(0, i);
        int lineNumber;
        try {
            lineNumber = Integer.parseInt(intStr);
        } catch(NumberFormatException e) {
            throw new CommandFormatException("Failed to parse line number '" + intStr + "': " + e.getLocalizedMessage());
        }

        if(lineNumber < 1 || lineNumber > batchSize) {
            throw new CommandFormatException(lineNumber + " isn't in range [1.." + batchSize + "].");
        }

        String editedLine = argsStr.substring(i).trim();
        if(editedLine.length() == 0) {
            throw new CommandFormatException("Missing the new command line after the index.");
        }

        if(editedLine.charAt(0) == '"') {
            if(editedLine.length() > 1 && editedLine.charAt(editedLine.length() - 1) == '"') {
                editedLine = editedLine.substring(1, editedLine.length() - 1);
            }
        }

        BatchedCommand newCmd = ctx.toBatchedCommand(editedLine);
        batch.set(lineNumber - 1, newCmd);
        ctx.printLine("#" + lineNumber + " " + newCmd.getCommand());
    }
View Full Code Here


        BatchManager batchManager = ctx.getBatchManager();
        if(!batchManager.isBatchActive()) {
            throw new CommandFormatException("No active batch.");
        }

        Batch batch = batchManager.getActiveBatch();
        final int batchSize = batch.size();
        if(batchSize == 0) {
            throw new CommandFormatException("The batch is empty.");
        }

        List<String> arguments = ctx.getParsedCommandLine().getOtherProperties();
        if(arguments.isEmpty()) {
            throw new CommandFormatException("Missing line number.");
        }

        if(arguments.size() != 2) {
            throw new CommandFormatException("Expected two arguments but received: " + arguments);
        }

        String intStr = arguments.get(0);
        final int lineNumber;
        try {
            lineNumber = Integer.parseInt(intStr);
        } catch(NumberFormatException e) {
            throw new CommandFormatException("Failed to parse line number '" + intStr + "': " + e.getLocalizedMessage());
        }

        if(lineNumber < 1 || lineNumber > batchSize) {
            throw new CommandFormatException(lineNumber + " isn't in range [1.." + batchSize + "].");
        }

        intStr = arguments.get(1);
        final int toLineNumber;
        try {
            toLineNumber = Integer.parseInt(intStr);
        } catch(NumberFormatException e) {
            throw new CommandFormatException("Failed to parse line number '" + intStr + "': " + e.getLocalizedMessage());
        }

        if(toLineNumber < 1 || toLineNumber > batchSize) {
            throw new CommandFormatException(toLineNumber + " isn't in range [1.." + batchSize + "].");
        }

        batch.move(lineNumber - 1, toLineNumber - 1);
    }
View Full Code Here

            } else {
                throw new CommandLineException("else block did not activate batch mode.");
            }
        }

        final Batch batch = batchManager.getActiveBatch();
        batchManager.discardActiveBatch();

        final ModelControllerClient client = ctx.getModelControllerClient();
        if(client == null) {
            throw new CommandLineException("The connection to the controller has not been established.");
        }

        final ModelNode conditionRequest = ifBlock.getConditionRequest();
        if(conditionRequest == null) {
            throw new CommandLineException("The condition request is not available.");
        }

        final Operation expression = ifBlock.getConditionExpression();
        if(expression == null) {
            throw new CommandLineException("The if expression is not available.");
        }

        ModelNode targetValue;
        try {
            targetValue = client.execute(conditionRequest);
        } catch (IOException e) {
            throw new CommandLineException("condition request failed", e);
        }

        final Object value = expression.resolveValue(ctx, targetValue);
        if(value == null) {
            throw new CommandLineException("if expression resolved to a null");
        }

        if(Boolean.TRUE.equals(value)) {
            ModelNode ifRequest = ifBlock.getIfRequest();
            if(ifRequest == null) {
                if(batch.size() == 0) {
                    throw new CommandLineException("if request is missing.");
                }
                ifRequest = batch.toRequest();
            }
            try {
                final ModelNode response = client.execute(ifRequest);
                if(!Util.isSuccess(response)) {
                    new CommandLineException("if request failed: " + Util.getFailureDescription(response));
                }
            } catch (IOException e) {
                throw new CommandLineException("if request failed", e);
            }
        } else if(ifBlock.isInElse()) {
            if(batch.size() == 0) {
                throw new CommandLineException("else block is empty.");
            }
            try {
                final ModelNode response = client.execute(batch.toRequest());
                if(!Util.isSuccess(response)) {
                    throw new CommandLineException("else request failed: " + Util.getFailureDescription(response));
                }
            } catch (IOException e) {
                throw new CommandLineException("else request failed", e);
View Full Code Here

        final BatchManager batchManager = ctx.getBatchManager();
        if(!batchManager.isBatchActive()) {
            throw new CommandLineException("if block did not activate batch mode.");
        }

        final Batch ifBatch = batchManager.getActiveBatch();
        if(ifBatch.size() == 0) {
            throw new CommandLineException("if block is empty.");
        }

        ifBlock.setIfRequest(ifBatch.toRequest());
        ifBlock.setInElse();

        batchManager.discardActiveBatch();
        if(!batchManager.activateNewBatch()) {
            IfElseBlock.remove(ctx);
View Full Code Here

            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new FileReader(f));
                String line = reader.readLine();
                batchManager.activateNewBatch();
                final Batch batch = batchManager.getActiveBatch();
                while(line != null) {
                    batch.add(ctx.toBatchedCommand(line));
                    line = reader.readLine();
                }
            } catch(IOException e) {
                batchManager.discardActiveBatch();
                throw new CommandLineException("Failed to read file " + f.getAbsolutePath(), e);
            } catch(CommandFormatException e) {
                batchManager.discardActiveBatch();
                throw new CommandLineException("Failed to create batch from " + f.getAbsolutePath(), e);
            } finally {
                if(baseDir != null) {
                    ctx.setCurrentDir(currentDir);
                }
                if(reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {}
                }
            }
            return;
        }

        boolean activated;
        if(batchManager.isHeldback(name)) {
            activated = batchManager.activateHeldbackBatch(name);
            if (activated) {
                final String msg = name == null ? "Re-activated batch" : "Re-activated batch '" + name + "'";
                ctx.printLine(msg);
                List<BatchedCommand> batch = batchManager.getActiveBatch().getCommands();
                if (!batch.isEmpty()) {
                    for (int i = 0; i < batch.size(); ++i) {
                        BatchedCommand cmd = batch.get(i);
                        ctx.printLine("#" + (i + 1) + ' ' + cmd.getCommand());
                    }
                }
            }
        } else if(name != null) {
View Full Code Here

        final BatchManager batchManager = ctx.getBatchManager();
        if(batchManager.isBatchActive()) {
            if(path != null) {
                throw new CommandFormatException("--file is not allowed in the batch mode.");
            }
            final Batch batch = batchManager.getActiveBatch();
            List<BatchedCommand> currentBatch = batch.getCommands();
            if(currentBatch.isEmpty()) {
                batchManager.discardActiveBatch();
                throw new CommandFormatException("The batch is empty.");
            }
            return batch.toRequest();
        }

        if(path != null) {
            final File f = new File(path);
            if(!f.exists()) {
                throw new CommandFormatException("File " + f.getAbsolutePath() + " does not exist.");
            }

            final File currentDir = ctx.getCurrentDir();
            final File baseDir = f.getParentFile();
            if(baseDir != null) {
                ctx.setCurrentDir(baseDir);
            }

            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new FileReader(f));
                String line = reader.readLine();
                batchManager.activateNewBatch();
                final Batch batch = batchManager.getActiveBatch();
                while(line != null) {
                    batch.add(ctx.toBatchedCommand(line));
                    line = reader.readLine();
                }
                return batch.toRequest();
            } catch(IOException e) {
                throw new CommandFormatException("Failed to read file " + f.getAbsolutePath(), e);
            } catch(CommandFormatException e) {
                throw new CommandFormatException("Failed to create batch from " + f.getAbsolutePath(), e);
            } finally {
View Full Code Here

                if (isBatchMode()) {
                    StringBuilder op = new StringBuilder();
                    op.append(getNodePathFormatter().format(parsedCmd.getAddress()));
                    op.append(line.substring(line.indexOf(':')));
                    DefaultBatchedCommand batchedCmd = new DefaultBatchedCommand(op.toString(), request);
                    Batch batch = getBatchManager().getActiveBatch();
                    batch.add(batchedCmd);
                    printLine("#" + batch.size() + " " + batchedCmd.getCommand());
                } else {
                    set("OP_REQ", request);
                    try {
                        operationHandler.handle(this);
                    } finally {
                        set("OP_REQ", null);
                    }
                }
            } else {
                final String cmdName = parsedCmd.getOperationName();
                CommandHandler handler = cmdRegistry.getCommandHandler(cmdName.toLowerCase());
                if (handler != null) {
                    if (isBatchMode() && handler.isBatchMode(this)) {
                        if (!(handler instanceof OperationCommand)) {
                            throw new CommandLineException("The command is not allowed in a batch.");
                        } else {
                            try {
                                ModelNode request = ((OperationCommand) handler).buildRequest(this);
                                BatchedCommand batchedCmd = new DefaultBatchedCommand(line, request);
                                Batch batch = getBatchManager().getActiveBatch();
                                batch.add(batchedCmd);
                                printLine("#" + batch.size() + " " + batchedCmd.getCommand());
                            } catch (CommandFormatException e) {
                                throw new CommandFormatException("Failed to add to batch '" + line + "'", e);
                            }
                        }
                    } else {
View Full Code Here

        if(!batchManager.isBatchActive()) {
            ctx.error("No active batch.");
            return;
        }

        Batch batch = batchManager.getActiveBatch();
        final int batchSize = batch.size();
        if(batchSize == 0) {
            ctx.error("The batch is empty.");
            return;
        }

        List<String> arguments = ctx.getParsedCommandLine().getOtherProperties();
        if(arguments.isEmpty()) {
            ctx.error("Missing line number.");
            return;
        }

        if(arguments.size() != 1) {
            ctx.error("Expected only one argument - the line number but received: " + arguments);
            return;
        }

        String intStr = arguments.get(0);
        int lineNumber;
        try {
            lineNumber = Integer.parseInt(intStr);
        } catch(NumberFormatException e) {
            ctx.error("Failed to parse line number '" + intStr + "': " + e.getLocalizedMessage());
            return;
        }

        if(lineNumber < 1 || lineNumber > batchSize) {
            ctx.error(lineNumber + " isn't in range [1.." + batchSize + "].");
            return;
        }

        batch.remove(lineNumber - 1);
    }
View Full Code Here

     * @see org.jboss.as.cli.handlers.CommandHandlerWithHelp#doHandle(org.jboss.as.cli.CommandContext)
     */
    @Override
    protected void doHandle(CommandContext ctx) {

        Batch batch = ctx.getBatchManager().getActiveBatch();
        if(batch == null) {
            ctx.error("No active batch.");
            return;
        }
        batch.clear();
    }
View Full Code Here

        if(!batchManager.isBatchActive()) {
            ctx.error("No active batch.");
            return;
        }

        Batch batch = batchManager.getActiveBatch();
        final int batchSize = batch.size();
        if(batchSize == 0) {
            ctx.error("The batch is empty.");
            return;
        }

        String argsStr = ctx.getArgumentsString();
        if(argsStr == null) {
            ctx.error("Missing line number.");
            return;
        }

        int i = 0;
        while(i < argsStr.length()) {
            if(Character.isWhitespace(argsStr.charAt(i))) {
                break;
            }
            ++i;
        }

        if(i == argsStr.length()) {
            ctx.error("Missing the new command line after the index.");
            return;
        }

        String intStr = argsStr.substring(0, i);
        int lineNumber;
        try {
            lineNumber = Integer.parseInt(intStr);
        } catch(NumberFormatException e) {
            ctx.error("Failed to parse line number '" + intStr + "': " + e.getLocalizedMessage());
            return;
        }

        if(lineNumber < 1 || lineNumber > batchSize) {
            ctx.error(lineNumber + " isn't in range [1.." + batchSize + "].");
            return;
        }

        String editedLine = argsStr.substring(i).trim();
        if(editedLine.length() == 0) {
            ctx.error("Missing the new command line after the index.");
            return;
        }

        if(editedLine.charAt(0) == '"') {
            if(editedLine.length() > 1 && editedLine.charAt(editedLine.length() - 1) == '"') {
                editedLine = editedLine.substring(1, editedLine.length() - 1);
            }
        }

        try {
            BatchedCommand newCmd = ctx.toBatchedCommand(editedLine);
            batch.set(lineNumber - 1, newCmd);
            ctx.printLine("#" + lineNumber + " " + newCmd.getCommand());
        } catch (CommandFormatException e) {
            ctx.error("Failed to process command line '" + editedLine + "': " + e.getLocalizedMessage());
        }
    }
View Full Code Here

TOP

Related Classes of org.jboss.as.cli.batch.Batch

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.