Example #1
0
  @Override
  public Boolean execute() throws JobException {

    String basdir = Silica.getResourceDirectory();
    File root = new File(basdir).getParentFile();
    LOG.info("cleaning old modules in {}", basdir);

    if (!root.isDirectory()) {
      throw new JobException(MessageFormat.format("Could not found base directory {0}", basdir));
    }

    String[] sortedFileNames = root.list();
    Arrays.sort(sortedFileNames);
    boolean cleaned = true;
    for (int i = sortedFileNames.length - 1 - Silica.getNumOfKeepDeployed(); i >= 0; i--) {
      File f = new File(root, sortedFileNames[i]);
      if (!f.getAbsolutePath().contains("silica")) { // Just in case.
        throw new JobException(
            "Do not remove the folder "
                + f.getAbsolutePath()
                + ". File path must contains 'silica'.");
      }
      if (LOG.isDebugEnabled()) {
        LOG.debug(MessageFormat.format("Cleaning file: {0}", f.getAbsolutePath()));
      }
      if (!deleteChildren(f)) {
        LOG.error(MessageFormat.format("Could not clean file: {0}", f.getAbsolutePath()));
        cleaned = false;
      }
    }
    return Boolean.valueOf(cleaned);
  }