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.
final UpdateMessage um = sessionManager.get(request);
if (null != um) checkCancelled(um.type());
cmd.perform();
}
@Override public void revert() throws Exception {
cmd.revert();
}
};
}
@Override
public void transmit(final LogRecord record) throws Exception {
final UpdateMessage um = responseFor(request)
.type(PROGRESS_NOTICE)
.artifactDescriptor(anticipatedDescriptor)
.currentLocation(anticipatedLocation)
.build();
um.attachedLogs().add(record);
send(um);
}
} // Install
new Install().call();