boolean debugEnabled = logger.isDebugEnabled();
if (debugEnabled) {
logger.debug(portal + " is going to wait windows.");
}
WindowListener listener = portal;
long deadline;
long begin = System.currentTimeMillis();
if (portal.getTimeout() > 0) {
deadline = begin + portal.getTimeout();
if (debugEnabled) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
logger.debug(portal + ".maxWait=" + portal.getTimeout() + "; deadline="
+ sdf.format(new Date(deadline)));
}
} else {
deadline = 0;
if (debugEnabled) {
logger.debug(portal + ".maxWait=(forever)");
}
}
int winSize = portal.getWindows().size();
int winIndex = 0;
List<Window> windows = portal.getWindows();
for (Window window : windows) {
winIndex++;
Future<?> future = window.getFuture();
if (future.isDone()) {
if (debugEnabled) {
if (future.isCancelled()) {
logger.debug("[" + winIndex + "/" + winSize + "] continue[cancelled]: "
+ window.getName());
}
if (future.isDone()) {
logger.debug("[" + winIndex + "/" + winSize + "] continue[done]: "
+ window.getName());
}
}
continue;
}
long awaitTime = 0;
try {
long begineWait = System.currentTimeMillis();
if (deadline > 0) {
awaitTime = deadline - begineWait;
if (awaitTime > 0) {
if (debugEnabled) {
logger.debug("[" + winIndex + "/" + winSize + "] waiting[begin]: "
+ window.getName() + "; maxWait=" + awaitTime);
}
future.get(awaitTime, TimeUnit.MILLISECONDS);
if (debugEnabled) {
logger.debug("[" + winIndex + "/" + winSize + "] waiting[done]: "
+ window.getName() + "; actualWait="
+ (System.currentTimeMillis() - begineWait));
}
} else {
logger.error("[" + winIndex + "/" + winSize
+ "] waiting[been timeout now]: " + window.getName());
listener.onWindowTimeout(window);
future.cancel(true);
}
} else {
if (debugEnabled) {
logger.debug("[" + winIndex + "/" + winSize + "] waiting[begin]: "
+ window.getName() + "; maxWait=(forever)");
}
future.get();
if (debugEnabled) {
logger.debug("[" + winIndex + "/" + winSize + "] waiting[done]: "
+ window.getName() + "; actualWait="
+ (System.currentTimeMillis() - begineWait));
}
}
} catch (InterruptedException e) {
logger.error("x[" + winIndex + "/" + winSize + "] waiting[interrupted]: "
+ window.getName());
} catch (ExecutionException e) {
logger.error("x[" + winIndex + "/" + winSize + "] waiting[error]: "
+ window.getName(), e);
window.setThrowable(e);
listener.onWindowError(window);
} catch (TimeoutException e) {
logger.error("x[" + winIndex + "/" + winSize + "] waiting[timeout]: "
+ window.getName(), e);
listener.onWindowTimeout(window);
future.cancel(true);
}
}
if (debugEnabled) {
logger.debug("[" + winIndex + "/" + winSize + "] size of simple windows = " + winIndex);