private final FilenameTabCompleter pathCompleter;
public DeploymentOverlayHandler(CommandContext ctx) {
super(ctx, "deployment-overlay", true);
l = new ArgumentWithoutValue(this, "-l") {
@Override
public boolean canAppearNext(CommandContext ctx) throws CommandFormatException {
final String actionStr = action.getValue(ctx.getParsedCommandLine());
if(actionStr == null || LIST_CONTENT.equals(actionStr) || LIST_LINKS.equals(actionStr)) {
return super.canAppearNext(ctx);
}
return false;
}
};
action = new ArgumentWithValue(this, new SimpleTabCompleter(
new String[]{ADD, LINK, LIST_CONTENT, LIST_LINKS, REDEPLOY_AFFECTED, REMOVE, UPLOAD}), 0, "--action");
name = new ArgumentWithValue(this, new DefaultCompleter(new CandidatesProvider(){
@Override
public Collection<String> getAllCandidates(CommandContext ctx) {
final ModelControllerClient client = ctx.getModelControllerClient();
if(client == null) {
return Collections.emptyList();
}
final ModelNode op = new ModelNode();
op.get(Util.OPERATION).set(Util.READ_CHILDREN_NAMES);
op.get(Util.ADDRESS).setEmptyList();
op.get(Util.CHILD_TYPE).set(Util.DEPLOYMENT_OVERLAY);
final ModelNode response;
try {
response = client.execute(op);
} catch (IOException e) {
return Collections.emptyList();
}
final ModelNode result = response.get(Util.RESULT);
if(!result.isDefined()) {
return Collections.emptyList();
}
final List<String> names = new ArrayList<String>();
for(ModelNode node : result.asList()) {
names.add(node.asString());
}
return names;
}}), "--name");
name.addRequiredPreceding(action);
pathCompleter = Util.isWindows() ? new WindowsFilenameTabCompleter(ctx) : new DefaultFilenameTabCompleter(ctx);
content = new ArgumentWithValue(this, new CommandLineCompleter(){
@Override
public int complete(CommandContext ctx, String buffer, int cursor, List<String> candidates) {
final String actionStr = action.getValue(ctx.getParsedCommandLine());
if (ADD.equals(actionStr) || UPLOAD.equals(actionStr)) {
// TODO add support for quoted paths
int i = buffer.lastIndexOf(',');
i = buffer.indexOf('=', i + 1);
if (i < 0) {
return -1;
}
final String path = buffer.substring(i + 1);
int pathResult = pathCompleter.complete(ctx, path, 0, candidates);
if (pathResult < 0) {
return -1;
}
return i + 1 + pathResult;
} else if(REMOVE.equals(actionStr)) {
final String nameStr = name.getValue(ctx.getParsedCommandLine());
if(nameStr == null) {
return -1;
}
final List<String> existing;
try {
existing = loadContentFor(ctx.getModelControllerClient(), nameStr);
} catch (CommandLineException e) {
return -1;
}
if(existing.isEmpty()) {
return buffer.length();
}
candidates.addAll(existing);
if(buffer.isEmpty()) {
return 0;
}
final String[] specified = buffer.split(",+");
candidates.removeAll(Arrays.asList(specified));
if(buffer.charAt(buffer.length() - 1) == ',') {
return buffer.length();
}
final String chunk = specified[specified.length - 1];
for(int i = 0; i < candidates.size(); ++i) {
if(!candidates.get(i).startsWith(chunk)) {
candidates.remove(i);
}
}
return buffer.length() - chunk.length();
} else {
return -1;
}
}}, "--content") {
@Override
public boolean canAppearNext(CommandContext ctx) throws CommandFormatException {
final String actionStr = action.getValue(ctx.getParsedCommandLine());
if(actionStr == null) {
return false;
}
if(ADD.equals(actionStr) || UPLOAD.equals(actionStr) || REMOVE.equals(actionStr)) {
return super.canAppearNext(ctx);
}
return false;
}
};
content.addRequiredPreceding(name);
content.addCantAppearAfter(l);
serverGroups = new ArgumentWithValue(this, new CommaSeparatedCompleter() {
@Override
protected Collection<String> getAllCandidates(CommandContext ctx) {
return Util.getServerGroups(ctx.getModelControllerClient());
}} , "--server-groups") {
@Override
public boolean canAppearNext(CommandContext ctx) throws CommandFormatException {
if(!ctx.isDomainMode()) {
return false;
}
final String actionStr = action.getValue(ctx.getParsedCommandLine());
if(actionStr == null) {
return false;
}
if(ADD.equals(actionStr) || LINK.equals(actionStr)
|| REMOVE.equals(actionStr) || LIST_LINKS.equals(actionStr)) {
return super.canAppearNext(ctx);
}
return false;
}
};
serverGroups.addRequiredPreceding(name);
allRelevantServerGroups = new ArgumentWithoutValue(this, "--all-relevant-server-groups") {
@Override
public boolean canAppearNext(CommandContext ctx) throws CommandFormatException {
if(!ctx.isDomainMode()) {
return false;
}
final String actionStr = action.getValue(ctx.getParsedCommandLine());
if(actionStr == null) {
return false;
}
if(REMOVE.equals(actionStr)) {
return super.canAppearNext(ctx);
}
return false;
}
};
allRelevantServerGroups.addRequiredPreceding(name);
allRelevantServerGroups.addCantAppearAfter(serverGroups);
serverGroups.addCantAppearAfter(allRelevantServerGroups);
allServerGroups = new ArgumentWithoutValue(this, "--all-server-groups") {
@Override
public boolean canAppearNext(CommandContext ctx) throws CommandFormatException {
if(!ctx.isDomainMode()) {
return false;
}
final String actionStr = action.getValue(ctx.getParsedCommandLine());
if(actionStr == null) {
return false;
}
if(ADD.equals(actionStr) || LINK.equals(actionStr)) {
return super.canAppearNext(ctx);
}
return false;
}
};
allServerGroups.addRequiredPreceding(name);
allServerGroups.addCantAppearAfter(serverGroups);
serverGroups.addCantAppearAfter(allServerGroups);
deployments = new ArgumentWithValue(this, new CommaSeparatedCompleter() {
@Override
protected Collection<String> getAllCandidates(CommandContext ctx) {
final String actionValue = action.getValue(ctx.getParsedCommandLine());
final ModelControllerClient client = ctx.getModelControllerClient();
if(REMOVE.equals(actionValue)) {
final String overlay = name.getValue(ctx.getParsedCommandLine());
if(overlay == null) {
return Collections.emptyList();
}
try {
if(ctx.isDomainMode()) {
final String groupsStr = serverGroups.getValue(ctx.getParsedCommandLine());
if(groupsStr != null) {
final String[] groups = groupsStr.split(",+");
if(groups.length == 1) {
return filterLinks(loadLinkResources(client, overlay, groups[0]));
} else if(groups.length > 1) {
final Set<String> commonLinks = new HashSet<String>();
commonLinks.addAll(filterLinks(loadLinkResources(client, overlay, groups[0])));
for(int i = 1; i < groups.length; ++i) {
commonLinks.retainAll(filterLinks(loadLinkResources(client, overlay, groups[i])));
}
return commonLinks;
}
}
} else {
return filterLinks(loadLinkResources(client, overlay, null));
}
} catch(CommandLineException e) {
}
return Collections.emptyList();
}
return Util.getDeployments(client);
}}, "--deployments") {
@Override
public boolean canAppearNext(CommandContext ctx) throws CommandFormatException {
if(ctx.isDomainMode()) {
if(serverGroups.isPresent(ctx.getParsedCommandLine()) || allServerGroups.isPresent(ctx.getParsedCommandLine())) {
return super.canAppearNext(ctx);
}
return false;
}
final String actionStr = action.getValue(ctx.getParsedCommandLine());
if(actionStr == null) {
return false;
}
if(ADD.equals(actionStr) || LINK.equals(actionStr) || REMOVE.equals(actionStr)) {
return super.canAppearNext(ctx);
}
return false;
}
};
deployments.addRequiredPreceding(name);
deployments.addCantAppearAfter(l);
redeployAffected = new ArgumentWithoutValue(this, "--redeploy-affected") {
@Override
public boolean canAppearNext(CommandContext ctx) throws CommandFormatException {
if(deployments.isPresent(ctx.getParsedCommandLine())) {
return super.canAppearNext(ctx);
}