org.jclouds.virtualbox.util
Class MachineUtils

java.lang.Object
  extended by org.jclouds.virtualbox.util.MachineUtils

@Singleton
public class MachineUtils
extends Object

Utilities for executing functions on a VirtualBox machine.

Author:
Adrian Cole, Mattias Holmqvist, Andrea Turli, David Alves

Field Summary
 String IP_V4_ADDRESS_PATTERN
           
protected  Logger logger
           
 
Constructor Summary
MachineUtils(com.google.common.base.Supplier<org.virtualbox_4_1.VirtualBoxManager> manager, RunScriptOnNode.Factory scriptRunner)
           
 
Method Summary
<T> T
applyForMachine(String machineId, com.google.common.base.Function<org.virtualbox_4_1.IMachine,T> function)
           
 String getIpAddressFromBridgedNIC(String machineName)
           
 String getIpAddressFromHostOnlyNIC(String machineName)
           
protected
<T> T
lockSessionOnMachineAndApply(String machineId, org.virtualbox_4_1.LockType type, com.google.common.base.Function<org.virtualbox_4_1.ISession,T> function)
          Locks the machine and executes the given function using the current session.
static boolean machineNotFoundException(org.virtualbox_4_1.VBoxException e)
           
 com.google.common.util.concurrent.ListenableFuture<ExecResponse> runScriptOnNode(NodeMetadata metadata, Statement statement, RunScriptOptions options)
           
<T> T
sharedLockMachineAndApply(String machineId, com.google.common.base.Function<org.virtualbox_4_1.IMachine,T> function)
          Locks the machine and executes the given function using the machine matching the given id.
<T> T
sharedLockMachineAndApplyToSession(String machineId, com.google.common.base.Function<org.virtualbox_4_1.ISession,T> function)
          Locks the machine and executes the given function to the session using the machine matching the given id.
<T> T
writeLockMachineAndApply(String machineId, com.google.common.base.Function<org.virtualbox_4_1.IMachine,T> function)
          Locks the machine and executes the given function using the machine matching the given id.
<T> T
writeLockMachineAndApplyToSession(String machineId, com.google.common.base.Function<org.virtualbox_4_1.ISession,T> function)
          Locks the machine and executes the given function using the machine matching the given id.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IP_V4_ADDRESS_PATTERN

public final String IP_V4_ADDRESS_PATTERN
See Also:
Constant Field Values

logger

@Named(value="jclouds.compute")
protected Logger logger
Constructor Detail

MachineUtils

@Inject
public MachineUtils(com.google.common.base.Supplier<org.virtualbox_4_1.VirtualBoxManager> manager,
                           RunScriptOnNode.Factory scriptRunner)
Method Detail

runScriptOnNode

public com.google.common.util.concurrent.ListenableFuture<ExecResponse> runScriptOnNode(NodeMetadata metadata,
                                                                                        Statement statement,
                                                                                        RunScriptOptions options)

writeLockMachineAndApply

public <T> T writeLockMachineAndApply(String machineId,
                                      com.google.common.base.Function<org.virtualbox_4_1.IMachine,T> function)
Locks the machine and executes the given function using the machine matching the given id. Since the machine is locked it is possible to perform some modifications to the IMachine.

Unlocks the machine before returning.

Parameters:
machineId - the id of the machine
function - the function to execute
Returns:
the result from applying the function to the machine.

writeLockMachineAndApplyToSession

public <T> T writeLockMachineAndApplyToSession(String machineId,
                                               com.google.common.base.Function<org.virtualbox_4_1.ISession,T> function)
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.

Parameters:
machineId - the id of the machine
function - the function to execute
Returns:
the result from applying the function to the machine.

sharedLockMachineAndApply

public <T> T sharedLockMachineAndApply(String machineId,
                                       com.google.common.base.Function<org.virtualbox_4_1.IMachine,T> 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.

Parameters:
machineId - the id of the machine
function - the function to execute
Returns:
the result from applying the function to the machine.

sharedLockMachineAndApplyToSession

public <T> T sharedLockMachineAndApplyToSession(String machineId,
                                                com.google.common.base.Function<org.virtualbox_4_1.ISession,T> function)
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.

Parameters:
machineId - the id of the machine
function - the function to execute
Returns:
the result from applying the function to the machine.

lockSessionOnMachineAndApply

protected <T> T lockSessionOnMachineAndApply(String machineId,
                                             org.virtualbox_4_1.LockType type,
                                             com.google.common.base.Function<org.virtualbox_4_1.ISession,T> function)
Locks the machine and executes the given function using the current session. Since the machine is locked it is possible to perform some modifications to the IMachine.

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.

Parameters:
type - the kind of lock to use when initially locking the machine.
machineId - the id of the machine
function - the function to execute
Returns:
the result from applying the function to the session.

applyForMachine

public <T> T applyForMachine(String machineId,
                             com.google.common.base.Function<org.virtualbox_4_1.IMachine,T> function)
Parameters:
machineId -
function -
Returns:

machineNotFoundException

public static boolean machineNotFoundException(org.virtualbox_4_1.VBoxException e)

getIpAddressFromBridgedNIC

public String getIpAddressFromBridgedNIC(String machineName)

getIpAddressFromHostOnlyNIC

public String getIpAddressFromHostOnlyNIC(String machineName)


Copyright © 2009-2012 jclouds. All Rights Reserved.