Package org.virtualbox_4_2

Examples of org.virtualbox_4_2.ISession


   }

   @Override
   public ISnapshot apply(IMachine machine) {
      // Snapshot a machine
      ISession session = null;
      ISnapshot snap = machine.getCurrentSnapshot();

      if (snap == null) {
         try {
            session = manager.get().openMachineSession(machine);
            int retries = 10;
            while (true) {
               try {

                  // running machines need to be pause before a snapshot can be taken
                  // due to a vbox bug see https://www.virtualbox.org/ticket/9255
                  boolean paused = false;
                  if (machine.getState() == MachineState.Running) {
                     session.getConsole().pause();
                     paused = true;
                  }

                  IProgress progress = session.getConsole().takeSnapshot(snapshotName, snapshotDesc);
                  progress.waitForCompletion(-1);

                  if (paused) {
                     session.getConsole().resume();
                  }

                  snap = machine.getCurrentSnapshot();
                  logger.debug("<< snapshot(%s) with description(%s) taken from master(%s)", snapshotName, snapshotDesc,
                           machine.getName());
View Full Code Here


   }

   @Override
   public void resumeNode(String vmName) {
      IMachine machine = manager.get().getVBox().findMachine(vmName);
      ISession machineSession;
      try {
         machineSession = manager.get().openMachineSession(machine);
         machineSession.getConsole().resume();
         machineSession.unlockMachine();
      } catch (Exception e) {
         throw Throwables.propagate(e);
      }
   }
View Full Code Here

   }

   @Override
   public void suspendNode(String vmName) {
      IMachine machine = manager.get().getVBox().findMachine(vmName);
      ISession machineSession;
      try {
         machineSession = manager.get().openMachineSession(machine);
         machineSession.getConsole().pause();
         machineSession.unlockMachine();
      } catch (Exception e) {
         throw Throwables.propagate(e);
      }
   }
View Full Code Here

      this.environment = environment;
   }

   @Override
   public ISession apply(IMachine machine) {
      ISession session = manager.getSessionObject();
      try {
         final IProgress progress = machine
                 .launchVMProcess(session, type.stringValue(), environment);
         progress.waitForCompletion(-1);
      } catch (VBoxException e) {
         ErrorCode errorCode = ErrorCode.valueOf(e);
         switch (errorCode) {
            case VBOX_E_INVALID_OBJECT_STATE:
               logger.warn(e, "Could not start machine. Got error code %s from launchMachine(). "
                       + "The machine might already be running.", errorCode);
               break;
            default:
               propagate(e);
         }
      } finally {
         if (session.getState() == SessionState.Locked) {
            // Remove session lock taken by launchVmProcess()
            session.unlockMachine();
            // TODO this unlock is not IMMEDIATELY visible outside (vbox doc)
         }
      }
      return session;
   }
View Full Code Here

               + nodeSpec.getTag() + VIRTUALBOX_NODE_NAME_SEPARATOR + nodeSpec.getName();
   }

   private void deleteExistingSnapshot(Master master) {
      if (master.getMachine().getCurrentSnapshot() != null) {
         ISession session;
         try {
            session = manager.get().getSessionObject();
            master.getMachine().lockMachine(session, LockType.Write);
            IProgress progress = session.getConsole().deleteSnapshot(master.getMachine().getCurrentSnapshot().getId());
            progress.waitForCompletion(-1);
            session.unlockMachine();
         } catch (Exception e) {
            throw new RuntimeException("error opening vbox machine session: " + e.getMessage(), e);
         }
         logger.debug("<< deleted an existing snapshot of vm(%s)", master.getMachine().getName());
      }
View Full Code Here

    *           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);
         }
      }
   }
View Full Code Here

   }

   private ISession lockSession(String machineId, LockType type, int retries) {
      int count = 0;
      IMachine immutableMachine = manager.get().getVBox().findMachine(machineId);
      ISession session;
      while (true) {
         try {
            session = manager.get().getSessionObject();
            immutableMachine.lockMachine(session, type);
            break;
         } catch (VBoxException e) {
            VBoxException vbex = Throwables2.getFirstThrowableOfType(e, VBoxException.class);
            if (vbex != null && machineNotFoundException(vbex)) {
               return null;
            }
            count++;
            logger.debug("Could not lock machine (try %d of %d). Error: %s", count, retries, e.getMessage());
            if (count == retries) {
               throw new RuntimeException(String.format("error locking %s with %s lock: %s", machineId, type,
                        e.getMessage()), e);
            }
            Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
         }
      }
      checkState(session.getState().equals(SessionState.Locked));
      return checkNotNull(session, "session");
   }
View Full Code Here

      clonedMachine = cloneFromMaster();
   }

   @Test
   public void testEnsureMachineisLaunchedAndSessionIsUnlocked() {
      ISession cloneMachineSession = machineController.ensureMachineIsLaunched(clonedMachine.getName());
      assertTrue(cloneMachineSession.getState() == SessionState.Unlocked);
      cloneMachineSession = machineController.ensureMachineHasPowerDown(clonedMachine.getName());
      SessionState state = cloneMachineSession.getState();
      assertEquals(SessionState.Unlocked, state);
   }
View Full Code Here

   }

   @Test(description = "write lock is acquired and released correctly")
   public void writeLockSessionOnMachine() {
      final IMachine clone = cloneFromMaster();
      ISession session = machineUtils.writeLockMachineAndApplyToSession(clone.getName(),
            new Function<ISession, ISession>() {
               @Override
               public ISession apply(ISession session) {
                  assertEquals(session.getMachine().getName(), clone.getName());
                  return session;
               }
            });
      checkState(session.getState().equals(SessionState.Unlocked));
      undoVm(clone.getName());
   }
View Full Code Here

   }

   @Test(dependsOnMethods = "writeLockSessionOnMachine", description = "shared lock is acquired and released correctly")
   public void sharedLockSessionOnMachine() {
      final IMachine clone = cloneFromMaster();
      ISession session = machineUtils.sharedLockMachineAndApplyToSession(clone.getName(),
            new Function<ISession, ISession>() {
               @Override
               public ISession apply(ISession session) {
                  assertEquals(session.getMachine().getName(), clone.getName());
                  return session;
               }
            });
      checkState(session.getState().equals(SessionState.Unlocked));
      undoVm(clone.getName());
   }
View Full Code Here

TOP

Related Classes of org.virtualbox_4_2.ISession

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.