/**
   * Main Constructor Takes a ServiceInstance object created by the login splash
   *
   * @param ServiceInstance si
   * @see LoginSplash
   */
  public CommandEngine(ServiceInstance si, Main main) {
    super(si);
    this.main = main;
    vmTemplates = new ArrayList<Template>();
    vmRootEntities = new ArrayList<VirtualMachineExt>();
    vmFolders = new ArrayList<FolderExt>();
    networks = new ArrayList<Network>();

    try {
      Folder rootFolder = si.getRootFolder();
      ManagedEntity[] datacenters = rootFolder.getChildEntity();
      for (int i = 0; i < datacenters.length; i++) {
        if (datacenters[i] instanceof Datacenter
            && datacenters[i].getName().equalsIgnoreCase(DATACENTER)) {
          dc = (Datacenter) datacenters[i];
        }
      }

      // Set the template directory and the project directory
      for (ManagedEntity each : dc.getVmFolder().getChildEntity()) {
        if (each instanceof Folder && each.getName().equalsIgnoreCase(TEMPLATE_FOLDER)) {
          templateDir = (Folder) each;
        } else if (each instanceof Folder && each.getName().equalsIgnoreCase(ROOT_FOLDER)) {
          rootDir = (Folder) each;
        }
      }

      // Set the target datastore for new VM deployments
      for (ManagedEntity each : dc.getDatastoreFolder().getChildEntity()) {
        if (each instanceof Datastore && each.getName().equalsIgnoreCase(TARGET_DATASTORE)) {
          targetDatastore = (Datastore) each;
        }
      }

      // Get Networks - only happens once per load
      for (ManagedEntity each : dc.getNetworkFolder().getChildEntity()) {
        if (each instanceof Network) {
          networks.add(((Network) each));
        }
      }

      refresh();
    } catch (RemoteException e) {
      disconnect();
      LOG.printStackTrace(e);
    } catch (Exception e) {
      LOG.printStackTrace(e);
    }
  }
  @Override
  public void run() {
    try {
      ServiceDetails sd = ServiceDetails.getInstance();
      ServiceInstance si = sd.getSi();
      Folder rootFolder = si.getRootFolder();
      String name = rootFolder.getName();
      System.out.println("root:" + name);

      System.out.println("\n============ Data Centers ============");
      ManagedEntity[] dcs = new InventoryNavigator(rootFolder).searchManagedEntities("Datacenter");
      for (int i = 0; i < dcs.length; i++) {
        System.out.println("Datacenter[" + i + "]=" + dcs[i].getName());
        sd.setDc((Datacenter) dcs[i]);
      }

      System.out.println("\n\n============ Hosts ============");
      ManagedEntity[] hostsArray =
          new InventoryNavigator(rootFolder).searchManagedEntities("HostSystem");
      for (int i = 0; i < hostsArray.length; i++) {
        System.out.println("host[" + i + "]=" + hostsArray[i].getName());
      }

      System.out.println("\n\n============ Virtual Machines ============");
      ManagedEntity[] vms =
          new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine");
      VirtualMachine vm = null;

      System.out.println(
          " VM Name"
              + "\t\t Guest OS"
              + "\t\t VM Version"
              + "\t\t CPU \t"
              + "\t\t Memory "
              + "\t\t VMware Tools"
              + "\t\t IP Addresses"
              + "\tState");
      for (int i = 0; i < vms.length; i++) {

        vm = (VirtualMachine) vms[i];

        System.out.println(
            vms[i].getName()
                + "\t\t"
                + vm.getSummary().getConfig().guestFullName
                + "\t"
                + vm.getConfig().version
                + "\t\t\t"
                + vm.getConfig().getHardware().numCPU
                + "vCPU \t\t\t"
                + vm.getConfig().getHardware().memoryMB
                + " MB \t"
                + "\t"
                + vm.getGuest().toolsRunningStatus
                + "\t"
                + vm.getSummary().getGuest().getIpAddress()
                + "\t \t "
                + vm.getGuest().guestState
                + "\n");
        /*		        System.out.println("VM Version:"+vm.getConfig().version);
        System.out.println("CPU:"+vm.getConfig().getHardware().numCPU+" vCPU");
        System.out.println("Memory:"+vm.getConfig().getHardware().memoryMB+" MB");
        System.out.println("VMware Tools:"+vm.getGuest().toolsRunningStatus);
        System.out.println("IP Addresses:"+vm.getSummary().getGuest().getIpAddress());
        System.out.println("State:"+vm.getGuest().guestState);*/
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }