protected ModelNode buildWritePropertyRequest(CommandContext ctx) throws CommandFormatException { final String name = this.name.getValue(ctx.getParsedCommandLine(), true); ModelNode composite = new ModelNode(); composite.get(Util.OPERATION).set(Util.COMPOSITE); composite.get(Util.ADDRESS).setEmptyList(); ModelNode steps = composite.get(Util.STEPS); final ParsedCommandLine args = ctx.getParsedCommandLine(); final String profile; if (isDependsOnProfile() && ctx.isDomainMode()) { profile = this.profile.getValue(args); if (profile == null) { throw new OperationFormatException("--profile argument value is missing."); } } else { profile = null; } final Map<String, CommandArgument> nodeProps = loadArguments(ctx, null); for (String argName : args.getPropertyNames()) { if (isDependsOnProfile() && argName.equals("--profile") || this.name.getFullName().equals(argName)) { continue; } final ArgumentWithValue arg = (ArgumentWithValue) nodeProps.get(argName); if (arg == null) { throw new CommandFormatException("Unrecognized argument name '" + argName + "'"); } DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder(); if (profile != null) { builder.addNode(Util.PROFILE, profile); } for (OperationRequestAddress.Node node : getRequiredAddress()) { builder.addNode(node.getType(), node.getName()); } builder.addNode(getRequiredType(), name); builder.setOperationName(Util.WRITE_ATTRIBUTE); final String propName; if (argName.charAt(1) == '-') { propName = argName.substring(2); } else { propName = argName.substring(1); } builder.addProperty(Util.NAME, propName); final String valueString = args.getPropertyValue(argName); ModelNode nodeValue = arg.getValueConverter().fromString(valueString); builder.getModelNode().get(Util.VALUE).set(nodeValue); steps.add(builder.buildRequest()); } return composite; }
protected void addHeaders(CommandContext ctx, ModelNode request) throws CommandFormatException { if (!headers.isPresent(ctx.getParsedCommandLine())) { return; } final ModelNode headersNode = headers.toModelNode(ctx); final ModelNode opHeaders = request.get(Util.OPERATION_HEADERS); opHeaders.set(headersNode); }
@Override public Collection<CommandArgument> getArguments(CommandContext ctx) { ParsedCommandLine args = ctx.getParsedCommandLine(); try { if (!name.isValueComplete(args)) { return staticArgs; } } catch (CommandFormatException e) { return Collections.emptyList(); } final String op = operation.getValue(args); return loadArguments(ctx, op).values(); }
@Override protected void printHelp(CommandContext ctx) { ParsedCommandLine args = ctx.getParsedCommandLine(); try { if (helpProperties.isPresent(args)) { try { printProperties(ctx, getNodeProperties(ctx)); } catch (Exception e) { ctx.printLine("Failed to obtain the list or properties: " + e.getLocalizedMessage()); return; } return; } } catch (CommandFormatException e) { ctx.printLine(e.getLocalizedMessage()); return; } try { if (helpCommands.isPresent(args)) { printCommands(ctx); return; } } catch (CommandFormatException e) { ctx.printLine(e.getLocalizedMessage()); return; } final String operationName = operation.getValue(args); if (operationName == null) { printNodeDescription(ctx); return; } try { ModelNode result = getOperationDescription(ctx, operationName); if (!result.hasDefined("description")) { ctx.printLine("Operation description is not available."); return; } final StringBuilder buf = new StringBuilder(); buf.append("\nDESCRIPTION:\n\n"); buf.append(result.get("description").asString()); ctx.printLine(buf.toString()); if (result.hasDefined("request-properties")) { printProperties(ctx, result.get("request-properties").asPropertyList()); } else { printProperties(ctx, Collections.<Property>emptyList()); } } catch (Exception e) { } }
public UndeployHandler() { super("undeploy", true); SimpleArgumentTabCompleter argsCompleter = (SimpleArgumentTabCompleter) this.getArgumentCompleter(); l = new ArgumentWithoutValue("-l"); l.setExclusive(true); argsCompleter.addArgument(l); name = new ArgumentWithValue( false, new CommandLineCompleter() { @Override public int complete( CommandContext ctx, String buffer, int cursor, List<String> candidates) { int nextCharIndex = 0; while (nextCharIndex < buffer.length()) { if (!Character.isWhitespace(buffer.charAt(nextCharIndex))) { break; } ++nextCharIndex; } if (ctx.getModelControllerClient() != null) { List<String> deployments = Util.getDeployments(ctx.getModelControllerClient()); if (deployments.isEmpty()) { return -1; } String opBuffer = buffer.substring(nextCharIndex).trim(); if (opBuffer.isEmpty()) { candidates.addAll(deployments); } else { for (String name : deployments) { if (name.startsWith(opBuffer)) { candidates.add(name); } } Collections.sort(candidates); } return nextCharIndex; } else { return -1; } } }, 0, "--name"); name.addCantAppearAfter(l); argsCompleter.addArgument(name); }
protected ModelNode initRequest(CommandContext ctx) { ModelNode request = new ModelNode(); ModelNode address = request.get(Util.ADDRESS); if (ctx.isDomainMode()) { final String profileName = profile.getValue(ctx.getParsedCommandLine()); if (profile == null) { ctx.printLine("--profile argument is required to get the node description."); return null; } address.add(Util.PROFILE, profileName); } for (OperationRequestAddress.Node node : nodePath) { address.add(node.getType(), node.getName()); } address.add(type, "?"); return request; }
protected ModelNode buildRequestWithoutHeaders(CommandContext ctx) throws CommandFormatException { final ModelNode req = super.buildRequestWithoutHeaders(ctx); final ModelNode steps = req.get(Util.STEPS); final ModelNode conPropsNode = conProps.toModelNode(ctx); if (conPropsNode != null) { final List<Property> propsList = conPropsNode.asPropertyList(); for (Property prop : propsList) { final ModelNode address = this.buildOperationAddress(ctx); address.add(CONNECTION_PROPERTIES, prop.getName()); final ModelNode addProp = new ModelNode(); addProp.get(Util.ADDRESS).set(address); addProp.get(Util.OPERATION).set(Util.ADD); addProp.get(Util.VALUE).set(prop.getValue()); steps.add(addProp); } } return req; }
public GenericTypeOperationHandler( String nodeType, String idProperty, List<String> excludeOperations) { super("generic-type-operation", true); if (nodeType == null) { throw new IllegalArgumentException("Node type is null."); } this.nodeType = nodeType; helpArg = new ArgumentWithoutValue(this, "--help", "-h") { @Override public boolean canAppearNext(CommandContext ctx) throws CommandFormatException { if (ctx.isDomainMode() && !profile.isValueComplete(ctx.getParsedCommandLine())) { return false; } return super.canAppearNext(ctx); } }; nodePath = new DefaultOperationRequestAddress(); CommandLineParser.CallbackHandler handler = new DefaultCallbackHandler(nodePath); try { ParserUtil.parseOperationRequest(nodeType, handler); } catch (CommandFormatException e) { throw new IllegalArgumentException("Failed to parse nodeType: " + e.getMessage()); } if (!nodePath.endsOnType()) { throw new IllegalArgumentException("The node path doesn't end on a type: '" + nodeType + "'"); } this.type = nodePath.getNodeType(); nodePath.toParentNode(); addRequiredPath(nodePath); this.commandName = type; this.idProperty = idProperty; this.excludeOps = excludeOperations; profile = new ArgumentWithValue( this, new DefaultCompleter( new CandidatesProvider() { @Override public List<String> getAllCandidates(CommandContext ctx) { return Util.getNodeNames(ctx.getModelControllerClient(), null, "profile"); } }), "--profile") { @Override public boolean canAppearNext(CommandContext ctx) throws CommandFormatException { if (!ctx.isDomainMode()) { return false; } return super.canAppearNext(ctx); } }; // profile.addCantAppearAfter(helpArg); operation = new ArgumentWithValue( this, new DefaultCompleter( new CandidatesProvider() { @Override public Collection<String> getAllCandidates(CommandContext ctx) { DefaultOperationRequestAddress address = new DefaultOperationRequestAddress(); if (ctx.isDomainMode()) { final String profileName = profile.getValue(ctx.getParsedCommandLine()); if (profileName == null) { return Collections.emptyList(); } address.toNode("profile", profileName); } for (OperationRequestAddress.Node node : nodePath) { address.toNode(node.getType(), node.getName()); } address.toNode(type, "?"); Collection<String> ops = ctx.getOperationCandidatesProvider().getOperationNames(ctx, address); ops.removeAll(excludeOps); return ops; } }), 0, "--operation") { @Override public boolean canAppearNext(CommandContext ctx) throws CommandFormatException { if (ctx.isDomainMode() && !profile.isValueComplete(ctx.getParsedCommandLine())) { return false; } return super.canAppearNext(ctx); } }; operation.addCantAppearAfter(helpArg); name = new ArgumentWithValue( this, new DefaultCompleter( new DefaultCompleter.CandidatesProvider() { @Override public List<String> getAllCandidates(CommandContext ctx) { ModelControllerClient client = ctx.getModelControllerClient(); if (client == null) { return Collections.emptyList(); } DefaultOperationRequestAddress address = new DefaultOperationRequestAddress(); if (ctx.isDomainMode()) { final String profileName = profile.getValue(ctx.getParsedCommandLine()); if (profile == null) { return Collections.emptyList(); } address.toNode("profile", profileName); } for (OperationRequestAddress.Node node : nodePath) { address.toNode(node.getType(), node.getName()); } return Util.getNodeNames(ctx.getModelControllerClient(), address, type); } }), (idProperty == null ? "--name" : "--" + idProperty)) { @Override public boolean canAppearNext(CommandContext ctx) throws CommandFormatException { if (ctx.isDomainMode() && !profile.isValueComplete(ctx.getParsedCommandLine())) { return false; } return super.canAppearNext(ctx); } }; name.addCantAppearAfter(helpArg); helpArg.addCantAppearAfter(name); helpProperties = new ArgumentWithoutValue(this, "--properties"); helpProperties.addRequiredPreceding(helpArg); helpProperties.addCantAppearAfter(operation); helpCommands = new ArgumentWithoutValue(this, "--commands"); helpCommands.addRequiredPreceding(helpArg); helpCommands.addCantAppearAfter(operation); helpCommands.addCantAppearAfter(helpProperties); helpProperties.addCantAppearAfter(helpCommands); /// staticArgs.add(helpArg); staticArgs.add(helpCommands); staticArgs.add(helpProperties); staticArgs.add(profile); staticArgs.add(name); staticArgs.add(operation); }
protected void printNodeDescription(CommandContext ctx) { ModelNode request = initRequest(ctx); if (request == null) { return; } request.get(Util.OPERATION).set(Util.READ_RESOURCE_DESCRIPTION); ModelNode result = null; try { result = ctx.getModelControllerClient().execute(request); if (!result.hasDefined(Util.RESULT)) { ctx.printLine("Node description is not available."); return; } result = result.get(Util.RESULT); if (!result.hasDefined(Util.DESCRIPTION)) { ctx.printLine("Node description is not available."); return; } } catch (Exception e) { } final StringBuilder buf = new StringBuilder(); buf.append("\nSYNOPSIS\n\n"); buf.append(commandName).append(" --help [--properties | --commands] |\n"); for (int i = 0; i <= commandName.length(); ++i) { buf.append(' '); } buf.append(name.getFullName()) .append("=<value> --<property>=<value> (--<property>=<value>)* |\n"); for (int i = 0; i <= commandName.length(); ++i) { buf.append(' '); } buf.append("<command> ").append(name.getFullName()).append("=<value> (--<parameter>=<value>)*"); buf.append("\n\nDESCRIPTION\n\n"); buf.append("The command is used to manage resources of type " + this.nodeType + "."); buf.append("\n\nRESOURCE DESCRIPTION\n\n"); if (result != null) { buf.append(result.get(Util.DESCRIPTION).asString()); } else { buf.append( "N/A. Please, open a jira issue at https://issues.jboss.org/browse/AS7 to get this fixed. Thanks!"); } buf.append("\n\nARGUMENTS\n"); buf.append("\n--help - prints this content."); buf.append( "\n--help --properties - prints the list of the resource properties including their access-type"); buf.append("\n (read/write/metric), value type, and the description."); buf.append( "\n--help --commands - prints the list of the commands available for the resource."); buf.append( "\n To get the complete description of a specific command (including its parameters,"); buf.append("\n their types and descriptions), execute ") .append(commandName) .append(" <command> --help."); buf.append("\n\n").append(name.getFullName()).append(" - "); if (idProperty == null) { buf.append("is the name of the resource that completes the path ") .append(nodeType) .append(" and \n"); } else { buf.append("corresponds to a property of the resourse which \n"); } for (int i = 0; i < name.getFullName().length() + 5; ++i) { buf.append(' '); } buf.append("is used to identify the resourse against which the command should be executed."); buf.append("\n\n<property> - property name of the resourse whose value should be updated."); buf.append( "\n For a complete list of available property names, their types and descriptions,"); buf.append("\n execute ").append(commandName).append(" --help --properties."); buf.append( "\n\n<command> - command name provided by the resourse. For a complete list of available commands,"); buf.append("\n execute ").append(commandName).append(" --help --commands."); buf.append("\n\n<parameter> - parameter name of the <command> provided by the resourse."); buf.append( "\n For a complete list of available parameter names of a specific <command>,"); buf.append("\n their types and descriptions, execute ") .append(commandName) .append(" <command> --help."); ctx.printLine(buf.toString()); }
protected ModelNode buildOperationRequest(CommandContext ctx, final String operation) throws CommandFormatException { ParsedCommandLine args = ctx.getParsedCommandLine(); DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder(); if (ctx.isDomainMode()) { final String profile = this.profile.getValue(args); if (profile == null) { throw new OperationFormatException("Required argument --profile is missing."); } builder.addNode("profile", profile); } final String name = this.name.getValue(ctx.getParsedCommandLine(), true); for (OperationRequestAddress.Node node : nodePath) { builder.addNode(node.getType(), node.getName()); } builder.addNode(type, name); builder.setOperationName(operation); final Map<String, CommandArgument> argsMap = loadArguments(ctx, operation); for (String argName : args.getPropertyNames()) { if (argName.equals("--profile")) { continue; } if (argsMap == null) { if (argName.equals(this.name.getFullName())) { continue; } throw new CommandFormatException( "Command '" + operation + "' is not expected to have arguments other than " + this.name.getFullName() + "."); } final ArgumentWithValue arg = (ArgumentWithValue) argsMap.get(argName); if (arg == null) { if (argName.equals(this.name.getFullName())) { continue; } throw new CommandFormatException( "Unrecognized argument " + argName + " for command '" + operation + "'."); } final String propName; if (argName.charAt(1) == '-') { propName = argName.substring(2); } else { propName = argName.substring(1); } final String valueString = args.getPropertyValue(argName); ModelNode nodeValue = arg.getValueConverter().fromString(valueString); builder.getModelNode().get(propName).set(nodeValue); } return builder.buildRequest(); }
public GenericTypeOperationHandler( CommandContext ctx, String nodeType, String idProperty, List<String> excludeOperations) { super(ctx, "generic-type-operation", true); if (nodeType == null || nodeType.isEmpty()) { throw new IllegalArgumentException("Node type is " + (nodeType == null ? "null." : "empty.")); } if (nodeType.startsWith("/profile=") || nodeType.startsWith("profile=")) { int nextSep = nodeType.indexOf('/', 7); if (nextSep < 0) { throw new IllegalArgumentException( "Failed to determine the path after the profile in '" + nodeType + "'."); } nodeType = nodeType.substring(nextSep); this.nodeType = nodeType; } else { this.nodeType = nodeType; } helpArg = new ArgumentWithoutValue(this, "--help", "-h"); addRequiredPath(nodeType); this.commandName = getRequiredType(); if (this.commandName == null) { throw new IllegalArgumentException("The node path doesn't end on a type: '" + nodeType + "'"); } this.idProperty = idProperty; this.excludeOps = excludeOperations; profile = new ArgumentWithValue( this, new DefaultCompleter( new CandidatesProvider() { @Override public List<String> getAllCandidates(CommandContext ctx) { return Util.getNodeNames(ctx.getModelControllerClient(), null, "profile"); } }), "--profile") { @Override public boolean canAppearNext(CommandContext ctx) throws CommandFormatException { if (!isDependsOnProfile()) { return false; } if (!ctx.isDomainMode()) { return false; } return super.canAppearNext(ctx); } }; // profile.addCantAppearAfter(helpArg); operation = new ArgumentWithValue( this, new DefaultCompleter( new CandidatesProvider() { @Override public Collection<String> getAllCandidates(CommandContext ctx) { DefaultOperationRequestAddress address = new DefaultOperationRequestAddress(); if (isDependsOnProfile() && ctx.isDomainMode()) { final String profileName = profile.getValue(ctx.getParsedCommandLine()); if (profileName == null) { return Collections.emptyList(); } address.toNode("profile", profileName); } for (OperationRequestAddress.Node node : getRequiredAddress()) { address.toNode(node.getType(), node.getName()); } address.toNode(getRequiredType(), "?"); Collection<String> ops = ctx.getOperationCandidatesProvider().getOperationNames(ctx, address); ops.removeAll(excludeOps); return ops; } }), 0, "--operation") { @Override public boolean canAppearNext(CommandContext ctx) throws CommandFormatException { if (isDependsOnProfile() && ctx.isDomainMode() && !profile.isValueComplete(ctx.getParsedCommandLine())) { return false; } return super.canAppearNext(ctx); } }; operation.addCantAppearAfter(helpArg); name = new ArgumentWithValue( this, new DefaultCompleter( new DefaultCompleter.CandidatesProvider() { @Override public List<String> getAllCandidates(CommandContext ctx) { ModelControllerClient client = ctx.getModelControllerClient(); if (client == null) { return Collections.emptyList(); } DefaultOperationRequestAddress address = new DefaultOperationRequestAddress(); if (isDependsOnProfile() && ctx.isDomainMode()) { final String profileName = profile.getValue(ctx.getParsedCommandLine()); if (profile == null) { return Collections.emptyList(); } address.toNode("profile", profileName); } for (OperationRequestAddress.Node node : getRequiredAddress()) { address.toNode(node.getType(), node.getName()); } return Util.getNodeNames( ctx.getModelControllerClient(), address, getRequiredType()); } }), (idProperty == null ? "--name" : "--" + idProperty)) { @Override public boolean canAppearNext(CommandContext ctx) throws CommandFormatException { if (isDependsOnProfile() && ctx.isDomainMode() && !profile.isValueComplete(ctx.getParsedCommandLine())) { return false; } return super.canAppearNext(ctx); } }; name.addCantAppearAfter(helpArg); helpArg.addCantAppearAfter(name); helpProperties = new ArgumentWithoutValue(this, "--properties"); helpProperties.addRequiredPreceding(helpArg); helpProperties.addCantAppearAfter(operation); helpCommands = new ArgumentWithoutValue(this, "--commands"); helpCommands.addRequiredPreceding(helpArg); helpCommands.addCantAppearAfter(operation); helpCommands.addCantAppearAfter(helpProperties); helpProperties.addCantAppearAfter(helpCommands); /// staticArgs.add(helpArg); staticArgs.add(helpCommands); staticArgs.add(helpProperties); staticArgs.add(profile); staticArgs.add(name); staticArgs.add(operation); }
public IfHandler() { super("if", true); condition = new ArgumentWithValue(this, 0, "--condition"); condition.addCantAppearAfter(helpArg); of = new ArgumentWithValue( this, new DefaultCompleter( new CandidatesProvider() { @Override public Collection<String> getAllCandidates(CommandContext ctx) { return Collections.singletonList("of"); } }), 1, "--of"); of.addRequiredPreceding(condition); final ArgumentWithValue line = new ArgumentWithValue( this, new CommandLineCompleter() { @Override public int complete( CommandContext ctx, String buffer, int cursor, List<String> candidates) { final ParsedCommandLine args = ctx.getParsedCommandLine(); final String lnStr = of.getValue(args); if (lnStr == null) { return -1; } final String originalLine = args.getOriginalLine(); String conditionStr; try { conditionStr = condition.getValue(args, true); } catch (CommandFormatException e) { return -1; } int i = originalLine.indexOf(conditionStr); if (i < 0) { return -1; } i = originalLine.indexOf("of ", i + conditionStr.length()); if (i < 0) { return -1; } final String cmd = originalLine.substring(i + 3); /* final DefaultCallbackHandler parsedLine = new DefaultCallbackHandler(); try { parsedLine.parse(ctx.getCurrentNodePath(), cmd); } catch (CommandFormatException e) { return -1; } int cmdResult = OperationRequestCompleter.INSTANCE.complete(ctx, parsedLine, cmd, 0, candidates); */ int cmdResult = ctx.getDefaultCommandCompleter().complete(ctx, cmd, 0, candidates); if (cmdResult < 0) { return cmdResult; } // escaping index correction int escapeCorrection = 0; int start = originalLine.length() - 1 - buffer.length(); while (start - escapeCorrection >= 0) { final char ch = originalLine.charAt(start - escapeCorrection); if (Character.isWhitespace(ch) || ch == '=') { break; } ++escapeCorrection; } return buffer.length() + escapeCorrection - (cmd.length() - cmdResult); } }, Integer.MAX_VALUE, "--line") {}; line.addRequiredPreceding(of); }
@Override protected Map<String, CommandArgument> loadArguments(CommandContext ctx) { final Map<String, CommandArgument> args = super.loadArguments(ctx); args.put(conProps.getFullName(), conProps); return args; }