Getting rolled over log4j files from FTP

Scenario

  • Log4j configured to roll over log files by size
  • Local FTP process to get all the files relating one fixed date

Error example

  1. Server returns files log.1 and log.2 as modified on target date
  2. Local FTP process starts to download log.1
  3. Server perform a roll over log files renaming log.1 to log.2 and log.2 to log.3
  4. Local FTP process ends log.1 downloading
  5. Local FTP process starts to download log.2 (!!!)

Code pattern for expected result

  [...]
    // Downloaded files
    boolean fileDownloaded;
    ArrayList downloadedFiles = new ArrayList();

    do {

      fileDownloaded = false;

      // Log directory
      client.cd(logDir);
      List ls = client.ls();
      ArrayList filesToDownload = new ArrayList();
      for (Object file : ls) {
        SftpFile sftpFile = (SftpFile) file;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(sftpFile.getAttributes()
            .getModifiedTime().longValue() * 1000L));
        // Target date
        if (calendar.get(Calendar.DAY_OF_MONTH) == dayOfMonth
            && calendar.get(Calendar.MONTH) == (month - 1)
            && calendar.get(Calendar.YEAR) == year) {
          // Mark to download
          filesToDownload.add(sftpFile.getFilename());
        }
      }

      // Update list
      downloadedFiles = updateDownloadedList(downloadedFiles, filesToDownload);

      // Download file
      for (String fileName : filesToDownload) {
        if (!downloadedFiles.contains(fileName)) {
          client.get(fileName, currentDir + "/" + fileName);
          downloadedFiles.add(fileName);
          fileDownloaded = true;
          break;
        }
      }

    } while (fileDownloaded);

[...]

/**
 * Update downloaded log4j files renaming numeric log extension
 * @param downloadedFiles currently downloaded files (local machine)
 * @param availableFiles currently listed files (log4j server)
 * @return
 */
private ArrayList updateDownloadedList(ArrayList downloadedFiles, ArrayList availableFiles) {
 
  ArrayList updatedDownloadedFiles = new ArrayList();
 
  for (String downloadedFile : downloadedFiles) {
   
    if (!availableFiles.contains(downloadedFile)) {
     
      do {
          if (downloadedFile.endsWith(".log")) {
        
            downloadedFile = downloadedFile + ".1";
      
          } else {
        
            int lastDotPosition = downloadedFile.lastIndexOf(".") + 1;
            int nextRollOverNum = Integer.parseInt(downloadedFile.substring(lastDotPosition, downloadedFile.length())) + 1;
            downloadedFile = downloadedFile.substring(0, lastDotPosition) + nextRollOverNum;
      
          }
      } while (!availableFiles.contains(downloadedFile));
     
      updatedDownloadedFiles.add(downloadedFile);
     
    } else {
     
      updatedDownloadedFiles.add(downloadedFile);
     
    }
  }
 
  return updatedDownloadedFiles;
}

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s