FSRecoveryFileUtils.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.monitor.engine.fs_recovery;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.PrintWriter;

  25. import org.openspcoop2.utils.UtilsException;
  26. import org.slf4j.Logger;

  27. /**
  28.  * FSRecoveryFileUtils
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class FSRecoveryFileUtils {
  35.    
  36.     private FSRecoveryFileUtils() {}

  37.     public static final String ERROR_SUFFIX = ".error";
  38.    
  39.     public static String getDateFromFilename(String fileName) throws UtilsException {
  40.         String[] split = fileName.split("_");
  41.         if(split.length < 2) {
  42.             throw new UtilsException("Impossibile ricavare la data dal nome file ["+fileName+"]");
  43.         }
  44.         return split[1];
  45.     }
  46.    
  47.     public static String getNewFileNameFromFilename(String fileName) throws UtilsException {
  48.        
  49.         int tentativi = getTentativiFromFilename(fileName);
  50.        
  51.         if(tentativi == 0) {
  52.             return fileName + "_1"+ERROR_SUFFIX;
  53.         } else {
  54.             String substring = fileName.substring(0, fileName.indexOf(ERROR_SUFFIX));
  55.            
  56.             String[] split = substring.split("_");
  57.             if(split==null || split.length <= 0) {
  58.                 throw new UtilsException("Impossibile ricavare il nuovo nome file dal nome file ["+fileName+"]");
  59.             }
  60.             StringBuilder sb  = new StringBuilder();
  61.             for(int i =0; i < split.length -1; i++) {
  62.                 sb.append(split[i]).append("_");
  63.             }
  64.             int newErrorIndex = tentativi+1;
  65.             sb.append(newErrorIndex).append(ERROR_SUFFIX);
  66.             return sb.toString();
  67.         }
  68.     }
  69.    
  70.     public static int getTentativiFromFilename(String fileName) throws UtilsException {
  71.         if(fileName.endsWith(".xml")) {
  72.             return 0;
  73.         } else {
  74.             if(!fileName.endsWith(ERROR_SUFFIX)){
  75.                 throw new UtilsException("Impossibile ricavare il numero di tentativi dal nome file ["+fileName+"]");  
  76.             }
  77.            
  78.             String substring = fileName.substring(0, fileName.indexOf(ERROR_SUFFIX));
  79.            
  80.             String[] split = substring.split("_");
  81.             if(split==null || split.length <= 0) {
  82.                 throw new UtilsException("Impossibile ricavare il numero di tentativi dal nome file ["+fileName+"]");
  83.             }
  84.             return Integer.parseInt(split[split.length -1].trim());
  85.         }
  86.     }
  87.    
  88.     public static String renameToDLQ(File directoryDLQ, File file, Throwable e, Logger log) throws UtilsException {
  89.         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
  90.                 PrintWriter pw = new PrintWriter(baos);){
  91.             e.printStackTrace(pw);
  92.             pw.flush();
  93.             baos.flush();
  94.             return renameToDLQ(directoryDLQ, file, baos.toString(), log);
  95.         } catch(Exception er) {
  96.             throw new UtilsException(e.getMessage(),e);
  97.         }
  98.        
  99.     }
  100.    
  101.     public static String renameToDLQ(File directoryDLQ, File file, String error, Logger log) throws UtilsException {
  102.        
  103.         if(log!=null) {
  104.             // nop
  105.         }
  106.        
  107.         // Se per un file si raggiunge il massimo numero di tentativi, effettuare il rename del file file.renameTo(file2)
  108.         // Spostandolo nella directory DLQ.

  109.         // NOTA: in DLQ estrapolare la data (solo anno mese e giorno) dal nome del file e usarla come nome della directory all'interno di DLQ.
  110.         String data = FSRecoveryFileUtils.getDateFromFilename(file.getName());
  111.        
  112.         File dir = new File(directoryDLQ, data + File.separator);
  113.        
  114.         File newFile = new File(dir, file.getName());
  115.        
  116.         dir.mkdirs();
  117.         if(!file.renameTo(newFile)) {
  118.             // ignore
  119.         }
  120.        
  121.         File readme = new File(dir, file.getName() + ".README");
  122.         try (FileOutputStream fos = new FileOutputStream(readme);){
  123.             fos.write(error.getBytes());
  124.             fos.flush();
  125.         }
  126.         catch(Exception e) {
  127.             throw new UtilsException(e.getMessage(),e);
  128.         }
  129.            
  130.         return newFile.getAbsolutePath();

  131.     }
  132.    
  133.    
  134.    
  135. }