/**
  * Create a new IVMInstall of DefaultVM install type.
  *
  * @return
  */
 public static IVMInstall createVM(File location) {
   @SuppressWarnings("restriction")
   IVMInstallType vmType = JavaRuntime.getVMInstallType(ID_STANDARD_VM_TYPE);
   IVMInstall vm = vmType.createVMInstall(createVMId(vmType));
   vm.setInstallLocation(location);
   return vm;
 }
 public static List<IVMInstall> getAllVMs() {
   List<IVMInstall> result = new ArrayList<IVMInstall>();
   IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
   for (IVMInstallType type : types) {
     for (IVMInstall vm : type.getVMInstalls()) {
       result.add(vm);
     }
   }
   return result;
 }
 /** Populates the JRE table with existing JREs defined in the workspace. */
 protected void fillWithWorkspaceJREs() {
   // fill with JREs
   List<VMStandin> standins = new ArrayList<VMStandin>();
   IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
   for (int i = 0; i < types.length; i++) {
     IVMInstallType type = types[i];
     IVMInstall[] installs = type.getVMInstalls();
     for (int j = 0; j < installs.length; j++) {
       IVMInstall install = installs[j];
       standins.add(new VMStandin(install));
     }
   }
   setJREs(standins.toArray(new IVMInstall[standins.size()]));
 }
 /**
  * Find a unique VM id. Check existing 'real' VMs, as well as the last id used for a VMStandin.
  */
 private String createUniqueId(IVMInstallType vmType) {
   String id = null;
   do {
     id = String.valueOf(System.currentTimeMillis());
   } while (vmType.findVMInstall(id) != null || id.equals(fgLastUsedID));
   fgLastUsedID = id;
   return id;
 }
  public static String createVMId(IVMInstallType type) {
    // This code based on code copied from
    // org.eclipse.jdt.launching.JavaRuntime.detectEclipseRuntime()

    // Make sure the VM id is unique
    long unique = System.currentTimeMillis();
    while (type.findVMInstall(String.valueOf(unique)) != null) {
      unique++;
    }
    return String.valueOf(unique);
  }
  /**
   * Searches the specified directory recursively for installed VMs, adding each detected VM to the
   * <code>found</code> list. Any directories specified in the <code>ignore</code> are not
   * traversed.
   *
   * @param directory
   * @param found
   * @param types
   * @param ignore
   */
  protected void search(
      File directory,
      List<File> found,
      List<IVMInstallType> types,
      Set<File> ignore,
      IProgressMonitor monitor) {
    if (monitor.isCanceled()) {
      return;
    }

    String[] names = directory.list();
    if (names == null) {
      return;
    }
    List<File> subDirs = new ArrayList<File>();
    for (int i = 0; i < names.length; i++) {
      if (monitor.isCanceled()) {
        return;
      }
      File file = new File(directory, names[i]);
      try {
        monitor.subTask(
            NLS.bind(
                JREMessages.InstalledJREsBlock_14,
                new String[] {
                  Integer.toString(found.size()), file.getCanonicalPath().replaceAll("&", "&&")
                })); // @see bug 29855 //$NON-NLS-1$ //$NON-NLS-2$
      } catch (IOException e) {
      }
      IVMInstallType[] vmTypes = JavaRuntime.getVMInstallTypes();
      if (file.isDirectory()) {
        if (!ignore.contains(file)) {
          boolean validLocation = false;

          // Take the first VM install type that claims the location as a
          // valid VM install.  VM install types should be smart enough to not
          // claim another type's VM, but just in case...
          for (int j = 0; j < vmTypes.length; j++) {
            if (monitor.isCanceled()) {
              return;
            }
            IVMInstallType type = vmTypes[j];
            IStatus status = type.validateInstallLocation(file);
            if (status.isOK()) {
              found.add(file);
              types.add(type);
              validLocation = true;
              break;
            }
          }
          if (!validLocation) {
            subDirs.add(file);
          }
        }
      }
    }
    while (!subDirs.isEmpty()) {
      File subDir = subDirs.remove(0);
      search(subDir, found, types, ignore, monitor);
      if (monitor.isCanceled()) {
        return;
      }
    }
  }