Skip to content

Instantly share code, notes, and snippets.

@ZuluagaSD
Created October 5, 2012 17:10
Show Gist options
  • Save ZuluagaSD/3841056 to your computer and use it in GitHub Desktop.
Save ZuluagaSD/3841056 to your computer and use it in GitHub Desktop.
public class VirtualMachineDAO {
public ArrayList<String[]> getInfo(ServiceInstance si, Folder rootFolder) {
String[] vmArray = new String[10];
ArrayList<String[]> restulArr = new ArrayList<String[]>();
//result += "Searching VirtualMachines...." + "\n";
try {
ManagedEntity[] mes = new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine");
if (mes == null || mes.length == 0) {
//result += "There aren´t VirtualMachines" + "\n";
} else {
//result += "Virtual Machines\n==============================" + "\n";
//result += "I´ve found " + mes.length + " Virtual Machines" + "\n";
int x = 0;
for (int i = 0; i < mes.length; i++) {
VirtualMachine vm = (VirtualMachine) mes[i];
vmArray[x] = vm.getName();
vmArray[x + 1] = vm.getRuntime().getPowerState().toString();
vmArray[x + 2] = vm.getGuest().getGuestState();
vmArray[x + 3] = vm.getConfig().getGuestFullName();
vmArray[x + 4] = vm.getGuest().getIpAddress();
vmArray[x + 5] = vm.getGuest().getHostName();
VirtualHardware vmHrd = vm.getConfig().getHardware();
vmArray[x + 6] = String.valueOf(vmHrd.getMemoryMB() / 1024);
vmArray[x + 7] = String.valueOf(vmHrd.getNumCPU());
Datastore[] vmDataStores = vm.getDatastores();
vmArray[x + 8] = String.valueOf(vmDataStores.length);
for (int j = 0; j < vmDataStores.length; j++) {
vmArray[x + 9] = "#" + j + ": " + vmDataStores[j].getName();
}
restulArr.add(vmArray);
// result += "==============================" + "\n";
}
}
} catch (InvalidProperty e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RuntimeFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return restulArr;
}
}
@jvelezpo
Copy link

jvelezpo commented Oct 5, 2012

May be trying this

for (int i = 0; i < mes.length; i++) {
VirtualMachine vm = (VirtualMachine) mes[i];
vmArray = new String[10];
vmArray[x] = vm.getName();
.
.
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment