public static <MESSAGE, RES, EX extends Exception> RES callV8Sync(
V8CommandSender<MESSAGE, EX> commandSender,
MESSAGE message, final V8BlockingCallback<RES> callback, long timeoutMs)
throws EX, MethodIsBlockingException {
CallbackSemaphore syncCallback = new CallbackSemaphore();
final Exception [] exBuff = { null };
// A long way of creating buffer for generic type without warnings.
final List<RES> resBuff = new ArrayList<RES>(Collections.nCopies(1, (RES)null));
V8CommandProcessor.V8HandlerCallback callbackWrapper =
new V8CommandProcessor.V8HandlerCallback() {
@Override
public void failure(String message) {
exBuff[0] = new Exception("Failure: " + message);
}
@Override
public void messageReceived(CommandResponse response) {
RES result = callback.messageReceived(response);
resBuff.set(0, result);
}
};
commandSender.sendV8CommandAsync(message, true, callbackWrapper, syncCallback);
boolean waitRes;
try {
waitRes = syncCallback.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS);
} catch (RuntimeException e) {
throw new CallbackException(e);
}
if (!waitRes) {