String getPrompt() { StringBuilder buffer = new StringBuilder(); if (promptConnectPart == null) { buffer.append('['); if (controllerHost != null) { if (domainMode) { buffer.append("domain@"); } else { buffer.append("standalone@"); } buffer.append(controllerHost).append(':').append(controllerPort).append(' '); promptConnectPart = buffer.toString(); } else { buffer.append("disconnected "); } } else { buffer.append(promptConnectPart); } if (prefix.isEmpty()) { buffer.append('/'); } else { buffer.append(prefix.getNodeType()); final String nodeName = prefix.getNodeName(); if (nodeName != null) { buffer.append('=').append(nodeName); } } if (isBatchMode()) { buffer.append(" #"); } buffer.append("] "); return buffer.toString(); }
/* (non-Javadoc) * @see org.jboss.as.cli.CandidatesProvider#getNodeTypes(org.jboss.as.cli.Prefix) */ @Override public List<String> getNodeTypes(OperationRequestAddress prefix) { ModelControllerClient client = ctx.getModelControllerClient(); if (client == null) { return Collections.emptyList(); } if (prefix.endsOnType()) { throw new IllegalArgumentException("The prefix isn't expected to end on a type."); } ModelNode request; DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder(prefix); try { builder.operationName("read-children-types"); request = builder.buildRequest(); } catch (OperationFormatException e1) { throw new IllegalStateException("Failed to build operation", e1); } List<String> result; try { ModelNode outcome = client.execute(request); if (!Util.isSuccess(outcome)) { // TODO logging... exception? result = Collections.emptyList(); } else { result = Util.getList(outcome); } } catch (Exception e) { result = Collections.emptyList(); } return result; }
/* (non-Javadoc) * @see org.jboss.as.cli.PrefixFormatter#format(org.jboss.as.cli.Prefix) */ @Override public String format(OperationRequestAddress prefix) { Iterator<Node> iterator = prefix.iterator(); if (!iterator.hasNext()) { return "/"; } StringBuilder builder = new StringBuilder(); builder.append('/'); Node next = iterator.next(); builder.append(next.getType()); if (next.getName() != null) { builder.append('=').append(next.getName()); } while (iterator.hasNext()) { builder.append('/'); next = iterator.next(); builder.append(next.getType()); if (next.getName() != null) { builder.append('=').append(next.getName()); } } return builder.toString(); }
/** * Adds a node path which is required to exist before the command can be used. * * @param requiredPath node path which is required to exist before the command can be used. */ protected void addRequiredPath(OperationRequestAddress requiredPath) { if (requiredPath == null) { throw new IllegalArgumentException("Required path can't be null."); } // there perhaps could be more but for now only one is allowed if (requiredAddress != null) { throw new IllegalStateException("Only one required address is allowed, atm."); } requiredAddress = requiredPath; final Iterator<Node> iterator = requiredAddress.iterator(); if (iterator.hasNext()) { final String firstType = iterator.next().getType(); dependsOnProfile = Util.SUBSYSTEM.equals(firstType) || Util.PROFILE.equals(firstType); } if (requiredAddress.endsOnType()) { requiredType = requiredAddress.toParentNode().getType(); } }
@Override public List<String> getPropertyNames(String operationName, OperationRequestAddress address) { ModelControllerClient client = ctx.getModelControllerClient(); if (client == null) { return Collections.emptyList(); } if (address.endsOnType()) { throw new IllegalArgumentException("The prefix isn't expected to end on a type."); } ModelNode request; DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder(address); try { builder.operationName("read-operation-description"); builder.property("name", operationName, -1); request = builder.buildRequest(); } catch (OperationFormatException e1) { throw new IllegalStateException("Failed to build operation", e1); } List<String> result; try { ModelNode outcome = client.execute(request); if (!Util.isSuccess(outcome)) { result = Collections.emptyList(); } else { outcome.get("request-properties"); result = Util.getRequestPropertyNames(outcome); } } catch (Exception e) { result = Collections.emptyList(); } return result; }
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); }