private StateType awaitCompletion(ProgressObject progress, long timeout) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
progress.addProgressListener(new ProgressListener() {
public void handleProgressEvent(ProgressEvent event) {
DeploymentStatus status = event.getDeploymentStatus();
if (status.isCompleted() || status.isFailed()) {
latch.countDown();
}
}
});
final DeploymentStatus status = progress.getDeploymentStatus();
if (status.isCompleted())
return status.getState();
if (latch.await(timeout, TimeUnit.MILLISECONDS) == false)
throw new IllegalStateException("Deployment timeout: " + progress);
return status.getState();
}