From 582423bd2d5486c621d05f70f820bbb128157072 Mon Sep 17 00:00:00 2001 From: David Ribeiro Alves Date: Thu, 22 Mar 2012 03:46:22 +0000 Subject: [PATCH] commented machine utils --- .../jclouds/virtualbox/util/MachineUtils.java | 97 +++++++++++++------ 1 file changed, 67 insertions(+), 30 deletions(-) diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java index 41962ce7b3..8c527d85ba 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java @@ -45,7 +45,7 @@ import com.google.inject.Inject; /** * Utilities for executing functions on a VirtualBox machine. * - * @author Adrian Cole, Mattias Holmqvist, Andrea Turli + * @author Adrian Cole, Mattias Holmqvist, Andrea Turli, David Alves */ @Singleton @@ -101,10 +101,34 @@ public class MachineUtils { }); } + /** + * Locks the machine and executes the given function using the machine matching the given id. + * The machine is write locked and modifications to the session that reflect on the machine can be done safely. + *

+ * Unlocks the machine before returning. + * + * @param machineId + * the id of the machine + * @param function + * the function to execute + * @return the result from applying the function to the machine. + */ public T writeLockMachineAndApplyToSession(final String machineId, final Function function) { return lockSessionOnMachineAndApply(machineId, LockType.Write, function); } + /** + * Locks the machine and executes the given function using the machine matching the given id. + * The machine is read locked, which means that settings can be read safely (but not changed) by function. + *

+ * Unlocks the machine before returning. + * + * @param machineId + * the id of the machine + * @param function + * the function to execute + * @return the result from applying the function to the machine. + */ public T readLockMachineAndApply(final String machineId, final Function function) { return lockSessionOnMachineAndApply(machineId, LockType.Shared, new Function() { @@ -121,6 +145,18 @@ public class MachineUtils { }); } + /** + * Locks the machine and executes the given function to the session using the machine matching the given id. + * The machine is read locked, which means that settings can be read safely (but not changed) by function. + *

+ * Unlocks the machine before returning. + * + * @param machineId + * the id of the machine + * @param function + * the function to execute + * @return the result from applying the function to the machine. + */ public T readLockMachineAndApplyToSession(final String machineId, final Function function) { return lockSessionOnMachineAndApply(machineId, LockType.Shared, function); } @@ -132,6 +168,9 @@ public class MachineUtils { *

* Unlocks the machine before returning. * + * Tries to obtain a lock 5 times before giving up waiting 1 sec between tries. When no machine + * is found null is returned. + * * @param type * the kind of lock to use when initially locking the machine. * @param machineId @@ -141,41 +180,39 @@ public class MachineUtils { * @return the result from applying the function to the session. */ private T lockSessionOnMachineAndApply(String machineId, LockType type, Function function) { - try { - int retries = 5; - int count = 0; - ISession session; - while (true) { + int retries = 5; + int count = 0; + ISession session; + while (true) { + try { + session = manager.get().getSessionObject(); + IMachine immutableMachine = manager.get().getVBox().findMachine(machineId); + immutableMachine.lockMachine(session, type); + break; + } catch (VBoxException e) { + VBoxException vbex = Throwables2.getFirstThrowableOfType(e, VBoxException.class); + if (vbex != null && machineNotFoundException(vbex)) { + return null; + } + count++; + logger.warn("Could not lock machine (try %i of %i). Error: %s", retries, count, e.getMessage()); + if (count == retries) { + throw new RuntimeException(String.format("error locking %s with %s lock: %s", machineId, type, + e.getMessage()), e); + } try { - session = manager.get().getSessionObject(); - IMachine immutableMachine = manager.get().getVBox().findMachine(machineId); - immutableMachine.lockMachine(session, type); - break; - } catch (VBoxException e) { - VBoxException vbex = Throwables2.getFirstThrowableOfType(e, VBoxException.class); - if (vbex != null && machineNotFoundException(vbex)){ - return null; - } - count++; - logger.warn("Could not lock machine (try %i of %i). Error: %s", retries, count, e.getMessage()); - if (count == retries){ - throw new RuntimeException(String.format("error locking %s with %s lock: %s", machineId, - type, e.getMessage()), e); - } - try { - Thread.sleep(1000L); - } catch (InterruptedException e1) { - } + Thread.sleep(1000L); + } catch (InterruptedException e1) { } } - try { - return function.apply(session); - } finally { - session.unlockMachine(); - } + } + 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 { + session.unlockMachine(); } }