public GenericTypeOperationHandler(String nodeType, String idProperty, List<String> excludeOperations) {
super("generic-type-operation", true);
helpArg.setExclusive(false);
nodePath = new DefaultOperationRequestAddress();
OperationRequestParser.CallbackHandler handler = new DefaultOperationCallbackHandler(nodePath);
try {
DefaultOperationRequestParser.INSTANCE.parse(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();
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) {