final Condition resultReceived = lock.newCondition();
final MessageListener listener = new MessageListener() {
@Override
public void handle(MessageEvent e) {
Message message = e.getMessage();
if (e.isIncoming() && filter.accept(message)) {
lock.lock();
try {
result[0] = message;
} finally {
resultReceived.signal();
lock.unlock();
}
}
}
};
lock.lock();
try {
addMessageListener(listener);
send(stanza);
// Wait for the stanza to arrive.
if (!resultReceived.await(configuration.getDefaultResponseTimeout(), TimeUnit.MILLISECONDS)) {
throw new NoResponseException("Timeout reached, while waiting on a response.");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
lock.unlock();
removeMessageListener(listener);
}
Message response = result[0];
if (response.getType() == Message.Type.ERROR) {
throw new StanzaException(response);
}
return response;
}