}
@Override public Command decorate(
final Command cmd,
final CommandId id) {
final Command tcmd = time(cmd, id);
return CommandId.UNDEPLOY == id ? undeploy(tcmd) : checked(tcmd);
}
Command time(final Command cmd, final CommandId id) {
return Commands.time(cmd,
new CommandIdLogContext() {
@Override String loggerName() {
return CoreUpdateManager.class.getName();
}
@Override CommandId commandId() { return id; }
});
}
Command undeploy(final Command cmd) {
return new AbstractCommand() {
@Override protected void doStart() throws Exception {
onStartUndeployment();
}
@Override protected void doPerform() throws Exception {
cmd.perform();
onPerformUndeployment();
}
@Override protected void doRevert() throws Exception {
cmd.revert();
onRevertUndeployment();
}
};
}
void onStartUndeployment() throws Exception {
sendRedeploymentRequest();
final long stop = System.currentTimeMillis()
+ HANDSHAKE_TIMEOUT_MILLIS;
synchronized (sessionManager) {
while (true) {
final UpdateMessage um = sessionManager.get(request);
final Type type = um.type();
checkCancelled(type);
if (PROCEED_REDEPLOYMENT_RESPONSE.equals(type))
break;
final long remaining = stop - System.currentTimeMillis();
if (0 >= remaining)
throw new Exception(
"Timeout while waiting for a redeployment response from the update agent.");
sessionManager.wait(remaining);
}
}
}
void sendRedeploymentRequest() throws Exception {
final UpdateMessage redeploymentRequest = responseFor(request)
.type(REDEPLOYMENT_REQUEST)
.artifactDescriptor(artifactDescriptor())
.build();
sendAndLog(redeploymentRequest);
}
void checkCancelled(final Type type) throws Exception {
if (CANCEL_REDEPLOYMENT_RESPONSE.equals(type))
throw new Exception(
"The update agent has cancelled the update installation.");
}
void onPerformUndeployment() {
anticipatedDescriptor = request
.artifactDescriptor()
.update()
.version(request.updateVersion())
.build();
anticipatedLocation = request.updateLocation();
}
void onRevertUndeployment() {
anticipatedDescriptor = request.artifactDescriptor();
anticipatedLocation = request.currentLocation();
}
Command checked(final Command cmd) {
return new Command() {
@Override public void perform() throws Exception {
// Throw an InterruptedException if requested.
Thread.sleep(0);
// May be undeployed, so check for null.