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 List<String> getAllCandidates(CommandContext ctx) {
final boolean writeAttribute;
try {
writeAttribute = name.isPresent(ctx.getParsedArguments());
} catch (CommandFormatException e) {
return Collections.emptyList();
}
if(writeAttribute) {
Set<String> specified;
try {
specified = ctx.getParsedArguments().getArgumentNames();
} catch (CommandFormatException e) {
return Collections.emptyList();
}
final List<String> theProps = new ArrayList<String>();
for(Property prop : getNodeProperties(ctx)) {
final String propName = "--" + prop.getName();
if(!specified.contains(propName) && prop.getValue().has("access-type") &&
prop.getValue().get("access-type").asString().contains("write")) {
theProps.add(propName + "=");
}
}
return theProps;
}
DefaultOperationRequestAddress address = new DefaultOperationRequestAddress();
if(ctx.isDomainMode()) {
final String profileName = profile.getValue(ctx.getParsedArguments());
if(profile == null) {
return Collections.emptyList();
}
address.toNode("profile", profileName);
}
for(OperationRequestAddress.Node node : nodePath) {
address.toNode(node.getType(), node.getName());
}
address.toNode(type, "?");
List<String> ops = ctx.getOperationCandidatesProvider().getOperationNames(address);
ops.removeAll(excludeOps);
return ops;
}}), 0, "--operation") {
@Override
public boolean canAppearNext(CommandContext ctx) throws CommandFormatException {
if(ctx.isDomainMode() && !profile.isPresent(ctx.getParsedArguments())) {
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.getParsedArguments());
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) {
@Override
public boolean canAppearNext(CommandContext ctx) throws CommandFormatException {
if(ctx.isDomainMode() && !profile.isPresent(ctx.getParsedArguments())) {
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);
///
genericCompleter = new BaseArgumentTabCompleter(){
private final List<CommandArgument> staticArgs = new ArrayList<CommandArgument>();
{
staticArgs.add(helpArg);
staticArgs.add(helpCommands);
staticArgs.add(helpProperties);
staticArgs.add(profile);
staticArgs.add(name);
staticArgs.add(operation);
}
private List<CommandArgument> nodeProps;
private Map<String, List<CommandArgument>> propsByOp;
@Override
protected Iterable<CommandArgument> getAllArguments(CommandContext ctx) {
ParsedArguments args = ctx.getParsedArguments();
final String theName = name.getValue(args);
if(theName == null) {
return staticArgs;
}
final String op = operation.getValue(args);
if(op == null) {
// list node properties
if(nodeProps == null) {
nodeProps = new ArrayList<CommandArgument>();
for(Property prop : getNodeProperties(ctx)) {
final ModelNode propDescr = prop.getValue();
if(propDescr.has("access-type") && "read-write".equals(propDescr.get("access-type").asString())) {
nodeProps.add(new ArgumentWithValue(GenericTypeOperationHandler.this, "--" + prop.getName()));
}
}
}
return nodeProps;
} else {
// list operation properties
if(propsByOp == null) {
propsByOp = new HashMap<String, List<CommandArgument>>();
}
List<CommandArgument> opProps = propsByOp.get(op);
if(opProps == null) {
final ModelNode descr;
try {
descr = getOperationDescription(ctx, op);
} catch (IOException e1) {
return Collections.emptyList();
}
if(descr == null || !descr.has("request-properties")) {
opProps = Collections.emptyList();
} else {
opProps = new ArrayList<CommandArgument>();
for (Property prop : descr.get("request-properties").asPropertyList()) {
opProps.add(new ArgumentWithValue(GenericTypeOperationHandler.this, "--" + prop.getName()));
}
}
propsByOp.put(op, opProps);
}
return opProps;