protected String generateNewVmName() {
   int nextIdx = 0;
   try {
     nextIdx = Integer.parseInt(FileUtil.readText(myIdxFile));
     FileUtil.writeFileAndReportErrors(myIdxFile, String.valueOf(nextIdx + 1));
   } catch (Exception e) {
     LOG.warn(
         "Will generate random clone index. Reason: unable to read idx file: " + e.toString());
     Random r = new Random();
     nextIdx = 100000 + r.nextInt(100000);
   }
   return String.format("%s-%d", getId(), nextIdx);
 }
 public void check_clone_name_generation() {
   for (int i = 0; i < 10; i++) {
     assertEquals(
         String.format("%s-%d", myImageDetails.getNickname(), i + 1), myImage.generateNewVmName());
   }
   FileUtil.delete(myIdxStorage);
   final String newName = myImage.generateNewVmName();
   assertTrue(newName.startsWith(myImage.getName()));
   final int i = Integer.parseInt(newName.substring(myImage.getName().length() + 1));
   assertTrue(i > 100000);
 }
  public VmwareCloudImage(
      @NotNull final VMWareApiConnector apiConnector,
      @NotNull final VmwareCloudImageDetails imageDetails,
      @NotNull final CloudAsyncTaskExecutor asyncTaskExecutor,
      @NotNull final File idxStorage) {
    super(imageDetails.getNickname(), imageDetails.getNickname());
    myImageDetails = imageDetails;
    myApiConnector = apiConnector;
    myAsyncTaskExecutor = asyncTaskExecutor;
    myInstances.clear();
    myIdxFile = new File(idxStorage, imageDetails.getNickname() + ".idx");
    if (!myIdxFile.exists()) {
      try {
        FileUtil.writeFileAndReportErrors(myIdxFile, "1");
      } catch (IOException e) {
        LOG.warn(
            String.format(
                "Unable to write idx file '%s': %s", myIdxFile.getAbsolutePath(), e.toString()));
      }
    }

    Map<String, VmwareInstance> realInstances = null;
    try {
      realInstances = myApiConnector.listImageInstances(this);
    } catch (VmwareCheckedCloudException e) {
      updateErrors(TypedCloudErrorInfo.fromException(e));
      return;
    }
    if (imageDetails.getBehaviour().isUseOriginal()) {
      final VmwareCloudInstance imageInstance =
          new VmwareCloudInstance(
              this, imageDetails.getSourceName(), VmwareConstants.CURRENT_STATE);
      myInstances.put(myImageDetails.getSourceName(), imageInstance);

      final VmwareInstance vmwareInstance = realInstances.get(imageDetails.getSourceName());
      if (vmwareInstance != null) {
        imageInstance.setStatus(vmwareInstance.getInstanceStatus());
      } else {
        imageInstance.setStatus(InstanceStatus.UNKNOWN);
        imageInstance.updateErrors(
            new TypedCloudErrorInfo("NoVM", "VM doesn't exist: " + imageDetails.getSourceName()));
      }
    } else {
      for (String instanceName : realInstances.keySet()) {
        final VmwareInstance instance = realInstances.get(instanceName);
        final String snapshotName = instance.getSnapshotName();
        VmwareCloudInstance cloudInstance =
            new VmwareCloudInstance(this, instanceName, snapshotName);
        cloudInstance.setStatus(instance.getInstanceStatus());
        myInstances.put(instanceName, cloudInstance);
      }
    }
  }
  @Override
  protected ModelAndView doHandle(
      @NotNull HttpServletRequest request, @NotNull HttpServletResponse response) throws Exception {
    String buildIdParam = request.getParameter("buildId");
    String path = request.getParameter("file");
    String torrentPath = path + TorrentUtil.TORRENT_FILE_SUFFIX;

    File torrentFile = null;
    long buildId = Long.parseLong(buildIdParam);
    SBuild build = myBuildsManager.findBuildInstanceById(buildId);
    if (build != null) {
      torrentFile = myTorrentsManager.getTorrentFile(build, torrentPath);
      if (!torrentFile.isFile()) {
        torrentFile = null;
      }
    }

    if (torrentFile == null) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
    } else {
      response.setContentType(WebUtil.getMimeType(request, torrentFile.getName()));
      // force set content-disposition to attachment
      WebUtil.setContentDisposition(request, response, torrentFile.getName(), false);
      ServletOutputStream output = response.getOutputStream();
      FileInputStream fis = null;
      try {
        fis = new FileInputStream(torrentFile);
        StreamUtil.copyStreamContent(fis, output);
      } finally {
        FileUtil.close(fis);
        output.close();
      }
    }

    return null;
  }