in case vm doesn't exist on kvm host, getconnectionbyname will throw exception, need to catch the exception, and return the default connection

This commit is contained in:
Edison Su 2013-04-24 10:38:14 -07:00
parent cdd459bb0b
commit 435f4bceb2
1 changed files with 10 additions and 4 deletions

View File

@ -61,13 +61,19 @@ public class LibvirtConnection {
static public Connect getConnectionByVmName(String vmName) throws LibvirtException {
HypervisorType[] hypervisors = new HypervisorType[] {HypervisorType.KVM, Hypervisor.HypervisorType.LXC};
for (HypervisorType hypervisor : hypervisors) {
Connect conn = LibvirtConnection.getConnectionByType(hypervisor.toString());
if (conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes())) != null) {
return conn;
}
try {
Connect conn = LibvirtConnection.getConnectionByType(hypervisor.toString());
if (conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes())) != null) {
return conn;
}
} catch (Exception e) {
s_logger.debug("can't find connection: " + hypervisor.toString() + ", for vm: " + vmName + ", continue");
}
}
s_logger.debug("can't find which hypervisor the vm used , then use the default hypervisor");
// return the default connection
return getConnection();
}