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;
}
}
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);
}
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);
}
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;
}
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;
}
}
Aggregations