}
@Override
protected void doHandle(CommandContext ctx) throws CommandFormatException {
ModelControllerClient client = ctx.getModelControllerClient();
ParsedCommandLine args = ctx.getParsedCommandLine();
boolean l = this.l.isPresent(args);
if (!args.hasProperties() || l) {
printList(ctx, Util.getDeployments(client), l);
return;
}
final String path = this.path.getValue(args);
final File f;
if(path != null) {
f = new File(path);
if(!f.exists()) {
ctx.printLine("Path " + f.getAbsolutePath() + " doesn't exist.");
return;
}
if(f.isDirectory()) {
ctx.printLine(f.getAbsolutePath() + " is a directory.");
return;
}
} else {
f = null;
}
String name = this.name.getValue(args);
if(name == null) {
if(f == null) {
ctx.printLine("Either path or --name is requied.");
return;
}
name = f.getName();
}
final String runtimeName = rtName.getValue(args);
if(Util.isDeploymentInRepository(name, client) && f != null) {
if(force.isPresent(args)) {
DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
ModelNode result;
// replace
builder = new DefaultOperationRequestBuilder();
builder.setOperationName(Util.FULL_REPLACE_DEPLOYMENT);
builder.addProperty("name", name);
if(runtimeName != null) {
builder.addProperty(Util.RUNTIME_NAME, runtimeName);
}
FileInputStream is = null;
try {
is = new FileInputStream(f);
ModelNode request = builder.buildRequest();
OperationBuilder op = new OperationBuilder(request);
op.addInputStream(is);
request.get(Util.CONTENT).get(0).get(Util.INPUT_STREAM_INDEX).set(0);
result = client.execute(op.build());
} catch(Exception e) {
ctx.printLine("Failed to replace the deployment: " + e.getLocalizedMessage());
return;
} finally {
StreamUtils.safeClose(is);
}
if(!Util.isSuccess(result)) {
ctx.printLine(Util.getFailureDescription(result));
return;
}
ctx.printLine("'" + name + "' re-deployed successfully.");
} else {
ctx.printLine("'" + name + "' is already deployed (use " + force.getFullName() + " to force re-deploy).");
}
return;
} else {
// deploy
// Actually, the add is performed first.
// But the deploy request is build before to make sure all the required parameters have been provided
ModelNode deployRequest = null;
if (!disabled.isPresent(args)) {
if (ctx.isDomainMode()) {
final List<String> serverGroups;
if (ctx.isDomainMode()) {
if(allServerGroups.isPresent(args)) {
serverGroups = Util.getServerGroups(client);
} else {
String serverGroupsStr = this.serverGroups.getValue(args);
if(serverGroupsStr == null) {
final StringBuilder buf = new StringBuilder();
buf.append("One of ");
if(f != null) {
buf.append(disabled.getFullName()).append(", ");
}
buf.append("--all-server-groups or --server-groups is missing.");
ctx.printLine(buf.toString());
return;
}
serverGroups = Arrays.asList(serverGroupsStr.split(","));
}
if(serverGroups.isEmpty()) {
ctx.printLine("No server group is available.");
return;
}
} else {
serverGroups = null;
}
deployRequest = new ModelNode();
deployRequest.get(Util.OPERATION).set(Util.COMPOSITE);
deployRequest.get(Util.ADDRESS).setEmptyList();
ModelNode steps = deployRequest.get(Util.STEPS);
for (String serverGroup : serverGroups) {
steps.add(Util.configureDeploymentOperation(Util.ADD, name, serverGroup));
}
for (String serverGroup : serverGroups) {
steps.add(Util.configureDeploymentOperation(Util.DEPLOY, name, serverGroup));
}
} else {
deployRequest = new ModelNode();
deployRequest.get(Util.OPERATION).set(Util.DEPLOY);
deployRequest.get(Util.ADDRESS, Util.DEPLOYMENT).set(name);
}
}
// add
if (f != null) {
ModelNode request = new ModelNode();
request.get(Util.OPERATION).set(Util.ADD);
request.get(Util.ADDRESS, Util.DEPLOYMENT).set(name);
if (runtimeName != null) {
request.get(Util.RUNTIME_NAME).set(runtimeName);
}
ModelNode result;
FileInputStream is = null;
try {
is = new FileInputStream(f);
OperationBuilder op = new OperationBuilder(request);
op.addInputStream(is);
request.get(Util.CONTENT).get(0).get(Util.INPUT_STREAM_INDEX).set(0);
result = client.execute(op.build());
} catch (Exception e) {
ctx.printLine("Failed to add the deployment content to the repository: " + e.getLocalizedMessage());
return;
} finally {
StreamUtils.safeClose(is);
}
if (!Util.isSuccess(result)) {
ctx.printLine(Util.getFailureDescription(result));
return;
}
}
if(deployRequest != null) {
ModelNode result;
try {
result = client.execute(deployRequest);
} catch (Exception e) {
ctx.printLine("Failed to deploy: " + e.getLocalizedMessage());
return;
}