/**
   * Recursively scans given directory and load all found files by loader.
   *
   * @param clsLdr Loader that could load class from given file.
   * @param dir Directory which should be scanned.
   * @param rsrcs Set which will be filled in.
   */
  @SuppressWarnings({"UnusedCatchParameter"})
  private static void findResourcesInDirectory(
      GridUriDeploymentFileResourceLoader clsLdr,
      File dir,
      Set<Class<? extends GridTask<?, ?>>> rsrcs) {
    assert dir.isDirectory() == true;

    for (File file : dir.listFiles()) {
      if (file.isDirectory()) {
        // Recurse down into directories.
        findResourcesInDirectory(clsLdr, file, rsrcs);
      } else {
        Class<? extends GridTask<?, ?>> rsrc = null;

        try {
          rsrc = clsLdr.createResource(file.getAbsolutePath(), true);
        } catch (GridSpiException e) {
          // Must never happen because we use 'ignoreUnknownRsrc=true'.
          assert false;
        }

        if (rsrc != null) {
          rsrcs.add(rsrc);
        }
      }
    }
  }
  /**
   * Load classes from given file. File could be either directory or JAR file.
   *
   * @param clsLdr Class loader to load files.
   * @param file Either directory or JAR file which contains classes or references to them.
   * @return Set of found and loaded classes or empty set if file does not exist.
   * @throws GridSpiException Thrown if given JAR file references to none existed class or
   *     IOException occurred during processing.
   */
  static Set<Class<? extends GridTask<?, ?>>> getClasses(ClassLoader clsLdr, File file)
      throws GridSpiException {
    Set<Class<? extends GridTask<?, ?>>> rsrcs = new HashSet<Class<? extends GridTask<?, ?>>>();

    if (file.exists() == false) {
      return rsrcs;
    }

    GridUriDeploymentFileResourceLoader fileRsrcLdr =
        new GridUriDeploymentFileResourceLoader(clsLdr, file);

    if (file.isDirectory()) {
      findResourcesInDirectory(fileRsrcLdr, file, rsrcs);
    } else {
      try {
        for (JarEntry entry : U.asIterable(new JarFile(file.getAbsolutePath()).entries())) {
          Class<? extends GridTask<?, ?>> rsrc = fileRsrcLdr.createResource(entry.getName(), false);

          if (rsrc != null) {
            rsrcs.add(rsrc);
          }
        }
      } catch (IOException e) {
        throw new GridSpiException(
            "Failed to discover classes in file: " + file.getAbsolutePath(), e);
      }
    }

    return rsrcs;
  }