Search in sources :

Example 1 with OperationCommand

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

the class GenericTypeOperationHandler method getHandler.

private OperationCommand getHandler(CommandContext ctx, String op) throws CommandLineException {
    if (op == null) {
        if (writePropHandler == null) {
            writePropHandler = new WritePropertyHandler();
            Iterator<AttributeDescription> props = getNodeProperties(ctx);
            while (props.hasNext()) {
                final AttributeDescription prop = props.next();
                if (prop.isWriteAllowed()) {
                    CommandLineCompleter valueCompleter = null;
                    ArgumentValueConverter valueConverter = null;
                    if (propConverters != null) {
                        valueConverter = propConverters.get(prop.getName());
                    }
                    if (valueCompleters != null) {
                        valueCompleter = valueCompleters.get(prop.getName());
                    }
                    if (valueConverter == null) {
                        valueConverter = ArgumentValueConverter.DEFAULT;
                        final ModelType propType = prop.getType();
                        if (propType != null) {
                            if (ModelType.BOOLEAN == propType) {
                                if (valueCompleter == null) {
                                    valueCompleter = SimpleTabCompleter.BOOLEAN;
                                }
                            } else if (ModelType.STRING == propType) {
                                valueConverter = ArgumentValueConverter.NON_OBJECT;
                            } else if (prop.getName().endsWith("properties")) {
                                // TODO this is bad but can't rely on proper descriptions
                                valueConverter = ArgumentValueConverter.PROPERTIES;
                            } else if (ModelType.LIST == propType) {
                                if (asType(prop.getProperty(Util.VALUE_TYPE)) == ModelType.PROPERTY) {
                                    valueConverter = ArgumentValueConverter.PROPERTIES;
                                } else {
                                    valueConverter = ArgumentValueConverter.LIST;
                                }
                            }
                        }
                    }
                    final CommandArgument arg = new ArgumentWithValue(GenericTypeOperationHandler.this, valueCompleter, valueConverter, "--" + prop.getName());
                    writePropHandler.addArgument(arg);
                }
            }
        }
        return writePropHandler;
    } else {
        if (customHandlers != null && customHandlers.containsKey(op)) {
            final OperationCommand opHandler = customHandlers.get(op);
            if (opHandler != null) {
                return opHandler;
            }
        }
        if (opHandlers != null) {
            OperationCommand opHandler = opHandlers.get(op);
            if (opHandler != null) {
                return opHandler;
            }
        }
        final ModelNode descr = getOperationDescription(ctx, op);
        if (opHandlers == null) {
            opHandlers = new HashMap<String, OperationCommand>();
        }
        final OpHandler opHandler = new OpHandler(op);
        opHandlers.put(op, opHandler);
        opHandler.addArgument(this.headers);
        if (descr != null && descr.has(Util.REQUEST_PROPERTIES)) {
            final List<Property> propList = descr.get(Util.REQUEST_PROPERTIES).asPropertyList();
            for (Property prop : propList) {
                final ModelNode propDescr = prop.getValue();
                CommandLineCompleter valueCompleter = null;
                ArgumentValueConverter valueConverter = null;
                if (propConverters != null) {
                    valueConverter = propConverters.get(prop.getName());
                }
                if (valueCompleters != null) {
                    valueCompleter = valueCompleters.get(prop.getName());
                }
                if (valueConverter == null) {
                    valueConverter = ArgumentValueConverter.DEFAULT;
                    if (propDescr.has(Util.TYPE)) {
                        final ModelType type = propDescr.get(Util.TYPE).asType();
                        if (ModelType.BOOLEAN == type) {
                            if (valueCompleter == null) {
                                valueCompleter = SimpleTabCompleter.BOOLEAN;
                            }
                        } else if (ModelType.STRING == type) {
                            valueConverter = ArgumentValueConverter.NON_OBJECT;
                        } else if (prop.getName().endsWith("properties")) {
                            // TODO this is bad but can't rely on proper descriptions
                            valueConverter = ArgumentValueConverter.PROPERTIES;
                        } else if (ModelType.LIST == type) {
                            if (propDescr.hasDefined(Util.VALUE_TYPE) && asType(propDescr.get(Util.VALUE_TYPE)) == ModelType.PROPERTY) {
                                valueConverter = ArgumentValueConverter.PROPERTIES;
                            } else {
                                valueConverter = ArgumentValueConverter.LIST;
                            }
                        }
                    }
                }
                final CommandArgument arg = new ArgumentWithValue(GenericTypeOperationHandler.this, valueCompleter, valueConverter, "--" + prop.getName());
                opHandler.addArgument(arg);
            }
        }
        return opHandler;
    }
}
Also used : CommandArgument(org.jboss.as.cli.CommandArgument) CommandLineCompleter(org.jboss.as.cli.CommandLineCompleter) OperationCommand(org.jboss.as.cli.OperationCommand) ArgumentValueConverter(org.jboss.as.cli.ArgumentValueConverter) ArgumentWithValue(org.jboss.as.cli.impl.ArgumentWithValue) ModelType(org.jboss.dmr.ModelType) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property)

Example 2 with OperationCommand

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

the class GenericTypeOperationHandler method getArgument.

@Override
public CommandArgument getArgument(CommandContext ctx, String name) {
    final ParsedCommandLine args = ctx.getParsedCommandLine();
    try {
        if (!this.name.isValueComplete(args)) {
            return staticArgs.get(name);
        }
    } catch (CommandFormatException e) {
        return null;
    }
    final String op = operation.getValue(args);
    OperationCommand handler;
    try {
        handler = getHandler(ctx, op);
    } catch (CommandLineException e) {
        return null;
    }
    return handler.getArgument(ctx, name);
}
Also used : OperationCommand(org.jboss.as.cli.OperationCommand) CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) CommandLineException(org.jboss.as.cli.CommandLineException)

Example 3 with OperationCommand

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

the class GenericTypeOperationHandler method buildRequestWithoutHeaders.

@Override
public ModelNode buildRequestWithoutHeaders(CommandContext ctx) throws CommandFormatException {
    final String operation = this.operation.getValue(ctx.getParsedCommandLine());
    OperationCommand opHandler;
    try {
        opHandler = getHandler(ctx, operation);
    } catch (CommandFormatException e) {
        throw e;
    } catch (CommandLineException e) {
        throw new CommandFormatException("Command is not supported or unavailable in the current context", e);
    }
    return opHandler.buildRequest(ctx);
}
Also used : OperationCommand(org.jboss.as.cli.OperationCommand) CommandFormatException(org.jboss.as.cli.CommandFormatException) CommandLineException(org.jboss.as.cli.CommandLineException)

Example 4 with OperationCommand

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

the class GenericTypeOperationHandler method getArguments.

@Override
public Collection<CommandArgument> getArguments(CommandContext ctx) {
    final ParsedCommandLine args = ctx.getParsedCommandLine();
    List<CommandArgument> arguments = new ArrayList<>();
    try {
        if (isChildNode) {
            arguments.addAll(staticArgs.values());
        } else {
            if (!name.isValueComplete(args)) {
                return staticArgs.values();
            }
        }
    } catch (CommandFormatException e) {
        return Collections.emptyList();
    }
    final String op = operation.getValue(args);
    OperationCommand handler;
    try {
        handler = getHandler(ctx, op);
    } catch (CommandLineException e) {
        // Return the static arguments in case of child node.
        return arguments;
    }
    arguments.addAll(handler.getArguments(ctx));
    return arguments;
}
Also used : OperationCommand(org.jboss.as.cli.OperationCommand) CommandFormatException(org.jboss.as.cli.CommandFormatException) CommandArgument(org.jboss.as.cli.CommandArgument) ArrayList(java.util.ArrayList) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) CommandLineException(org.jboss.as.cli.CommandLineException)

Example 5 with OperationCommand

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

the class CommandContextImpl method buildRequest.

protected HandledRequest buildRequest(String line, boolean batchMode) throws CommandFormatException {
    if (line == null || line.isEmpty()) {
        throw new OperationFormatException("The line is null or empty.");
    }
    final DefaultCallbackHandler originalParsedArguments = this.parsedCmd;
    final String originalCmdLine = this.cmdLine;
    try {
        this.parsedCmd = new DefaultCallbackHandler();
        resetArgs(line);
        if (parsedCmd.getFormat() == OperationFormat.INSTANCE) {
            final ModelNode request = this.parsedCmd.toOperationRequest(this);
            return new HandledRequest(request, null);
        }
        final CommandHandler handler = cmdRegistry.getCommandHandler(parsedCmd.getOperationName());
        if (handler != null) {
            if (batchMode) {
                if (!handler.isBatchMode(this)) {
                    throw new OperationFormatException("The command is not allowed in a batch.");
                }
                Batch batch = getBatchManager().getActiveBatch();
                return ((OperationCommand) handler).buildHandledRequest(this, batch.getAttachments());
            } else if (!(handler instanceof OperationCommand)) {
                throw new OperationFormatException("The command does not translate to an operation request.");
            }
            return new HandledRequest(((OperationCommand) handler).buildRequest(this), null);
        } else {
            return buildAeshCommandRequest(parsedCmd, batchMode);
        }
    } finally {
        clear(Scope.REQUEST);
        this.parsedCmd = originalParsedArguments;
        this.cmdLine = originalCmdLine;
    }
}
Also used : OperationCommand(org.jboss.as.cli.OperationCommand) OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) Batch(org.jboss.as.cli.batch.Batch) HandledRequest(org.jboss.as.cli.OperationCommand.HandledRequest) DefaultCallbackHandler(org.jboss.as.cli.operation.impl.DefaultCallbackHandler) CommandHandler(org.jboss.as.cli.CommandHandler) CommandCommandHandler(org.jboss.as.cli.handlers.CommandCommandHandler) ModelNode(org.jboss.dmr.ModelNode)

Aggregations

OperationCommand (org.jboss.as.cli.OperationCommand)6 CommandFormatException (org.jboss.as.cli.CommandFormatException)4 CommandLineException (org.jboss.as.cli.CommandLineException)4 CommandArgument (org.jboss.as.cli.CommandArgument)2 HandledRequest (org.jboss.as.cli.OperationCommand.HandledRequest)2 Batch (org.jboss.as.cli.batch.Batch)2 ParsedCommandLine (org.jboss.as.cli.operation.ParsedCommandLine)2 ModelNode (org.jboss.dmr.ModelNode)2 ArrayList (java.util.ArrayList)1 ArgumentValueConverter (org.jboss.as.cli.ArgumentValueConverter)1 CommandHandler (org.jboss.as.cli.CommandHandler)1 CommandLineCompleter (org.jboss.as.cli.CommandLineCompleter)1 BatchedCommand (org.jboss.as.cli.batch.BatchedCommand)1 DefaultBatchedCommand (org.jboss.as.cli.batch.impl.DefaultBatchedCommand)1 CommandCommandHandler (org.jboss.as.cli.handlers.CommandCommandHandler)1 ArgumentWithValue (org.jboss.as.cli.impl.ArgumentWithValue)1 OperationFormatException (org.jboss.as.cli.operation.OperationFormatException)1 DefaultCallbackHandler (org.jboss.as.cli.operation.impl.DefaultCallbackHandler)1 ModelType (org.jboss.dmr.ModelType)1 Property (org.jboss.dmr.Property)1