@Override protected void perform() { // Generate the ticket if needed (in some situations the client will not send // a ticket): if (StringUtils.isEmpty(ticket)) { ticket = Ticketing.generateOTP(); } // Update the dynamic information of the virtual machine in memory (we need it // to update the database later): final VM vm = getVm(); final DbUser user = getCurrentUser(); vm.setConsoleUserId(user.getId()); vm.setConsoleCurrentUserName(getConsoleUserName()); // If the virtual machine has the allow reconnect flag or the user // needed additional permissions to connect to the console then we just // have to save the user id to the database, regardless of what was // there before and without locking. // // Note that the fact that the user needed permissions actually means // that it has them, otherwise we will not be here, performing the // operation. // // In any other situation we try to save the new user to the database // and proceed only if the previous user in the database is null. This // is needed to prevent races between different users trying to access // the console of the same virtual machine simultaneously. final VmDynamicDao dao = DbFacade.getInstance().getVmDynamicDao(); if (vm.getAllowConsoleReconnect() || neededPermissions) { dao.update(vm.getDynamicData()); sendTicket(); } else { final boolean saved = dao.updateConsoleUserWithOptimisticLocking(vm.getDynamicData()); if (saved) { sendTicket(); } else { dontSendTicket(); } } }
private boolean isVmRunningOnHost(Guid hostId) { return !vmDynamicDao.getAllRunningForVds(hostId).isEmpty(); }