* the function to execute
* @return the result from applying the function to the session.
*/
protected <T> T lockSessionOnMachineAndApply(String machineId, LockType type, Function<ISession, T> function) {
int retries = 15;
ISession session = checkNotNull(lockSession(machineId, type, retries), "session");
try {
return function.apply(session);
} catch (VBoxException e) {
throw new RuntimeException(String.format("error applying %s to %s with %s lock: %s", function, machineId,
type, e.getMessage()), e);
} finally {
// this is a workaround for shared lock type, where session state is not updated immediately
if(type == LockType.Shared) {
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
}
if (session.getState().equals(SessionState.Locked)) {
session.unlockMachine();
}
if(!session.getState().equals(SessionState.Unlocked)) {
checkSessionIsUnlocked(session, 5, 3L, TimeUnit.SECONDS);
}
}
}