FileSystemSerializer.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.pdd.core;

  21. import java.io.File;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;

  24. import org.openspcoop2.core.commons.CoreException;
  25. import org.openspcoop2.monitor.engine.constants.Costanti;
  26. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  27. import org.openspcoop2.utils.UtilsException;
  28. import org.openspcoop2.utils.date.DateUtils;
  29. import org.openspcoop2.utils.id.IDUtilities;
  30. import org.openspcoop2.utils.resources.FileSystemUtilities;

  31. /**    
  32.  * FileSystemSerializer
  33.  *
  34.  * @author Poli Andrea (poli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public class FileSystemSerializer {

  39.     private static FileSystemSerializer staticInstance = null;
  40.     private static synchronized void initialize() throws CoreException{
  41.         if(staticInstance==null){
  42.             staticInstance = new FileSystemSerializer();
  43.         }
  44.     }
  45.     public static FileSystemSerializer getInstance() throws CoreException{
  46.         if(staticInstance==null){
  47.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  48.             synchronized (FileSystemSerializer.class) {
  49.                 initialize();
  50.             }
  51.         }
  52.         return staticInstance;
  53.     }
  54.    
  55.    
  56.     private File directory = null;
  57.    
  58.     private FileSystemSerializer() throws CoreException{
  59.        
  60.         this.directory = OpenSPCoop2Properties.getInstance().getFileSystemRecoveryRepository();
  61.         String prefix = "Directory ["+this.directory.getAbsolutePath()+"] ";
  62.         if(!this.directory.exists() &&
  63.             !this.directory.mkdir()){
  64.             throw new CoreException(prefix+"non esistente e creazione non riuscita");
  65.         }
  66.         if(!this.directory.canRead()){
  67.             throw new CoreException(prefix+"non accessibile in lettura");
  68.         }
  69.         if(!this.directory.canWrite()){
  70.             throw new CoreException(prefix+"non accessibile in scrittura");
  71.         }
  72.     }
  73.    
  74.     private static final String FORMAT_NEW = "yyyyMMdd_HHmmssSSS"; // compatibile con windows S.O.
  75.     @SuppressWarnings("unused")
  76.     private static final String FORMAT_OLD = "yyyy-MM-dd_HH:mm:ss.SSS";
  77.    
  78.     public void registraEvento(byte[] oggettSerializzato, Date date) throws CoreException, UtilsException{
  79.         this.registra(oggettSerializzato, Costanti.PREFIX_FILE_SYSTEM_REPOSITORY_EVENTI, date, getDirEventi());
  80.     }
  81.     public File getDirEventi(){
  82.         return new File(this.directory,Costanti.DIRECTORY_FILE_SYSTEM_REPOSITORY_EVENTI);
  83.     }
  84.        
  85.     public void registraTransazione(byte[] oggettSerializzato, Date date) throws CoreException, UtilsException{
  86.         this.registra(oggettSerializzato, Costanti.PREFIX_FILE_SYSTEM_REPOSITORY_TRANSAZIONE, date, getDirTransazioni());
  87.     }
  88.     public File getDirTransazioni(){
  89.         return new File(this.directory,Costanti.DIRECTORY_FILE_SYSTEM_REPOSITORY_TRANSAZIONE);
  90.     }
  91.    
  92.     public void registraDiagnostico(byte[] oggettSerializzato, Date date) throws CoreException, UtilsException{
  93.         this.registra(oggettSerializzato, Costanti.PREFIX_FILE_SYSTEM_REPOSITORY_DIAGNOSTICO, date,getDirDiagnostici());
  94.     }
  95.     public File getDirDiagnostici(){
  96.         return new File(this.directory,Costanti.DIRECTORY_FILE_SYSTEM_REPOSITORY_DIAGNOSTICO);
  97.     }
  98.    
  99.     public void registraTraccia(byte[] oggettSerializzato, Date date) throws CoreException, UtilsException{
  100.         this.registra(oggettSerializzato, Costanti.PREFIX_FILE_SYSTEM_REPOSITORY_TRACCIA, date,getDirTracce());
  101.     }
  102.     public File getDirTracce(){
  103.         return new File(this.directory,Costanti.DIRECTORY_FILE_SYSTEM_REPOSITORY_TRACCIA);
  104.     }
  105.    
  106.     public void registraDump(byte[] oggettSerializzato, Date date) throws CoreException, UtilsException{
  107.         this.registra(oggettSerializzato, Costanti.PREFIX_FILE_SYSTEM_REPOSITORY_DUMP, date,getDirDump());
  108.     }
  109.     public File getDirDump(){
  110.         return new File(this.directory,Costanti.DIRECTORY_FILE_SYSTEM_REPOSITORY_DUMP);
  111.     }
  112.    
  113.     public void registraTransazioneApplicativoServer(byte[] oggettSerializzato, Date date) throws CoreException, UtilsException{
  114.         this.registra(oggettSerializzato, Costanti.PREFIX_FILE_SYSTEM_REPOSITORY_TRANSAZIONE_APPLICATIVO_SERVER, date,getDirTransazioneApplicativoServer());
  115.     }
  116.     public File getDirTransazioneApplicativoServer(){
  117.         return new File(this.directory,Costanti.DIRECTORY_FILE_SYSTEM_REPOSITORY_TRANSAZIONE_APPLICATIVO_SERVER);
  118.     }
  119.    
  120.     public void registraTransazioneApplicativoServerConsegnaTerminata(byte[] oggettSerializzato, Date date) throws CoreException, UtilsException {
  121.         this.registra(oggettSerializzato, Costanti.PREFIX_FILE_SYSTEM_REPOSITORY_TRANSAZIONE_APPLICATIVO_SERVER_CONSEGNA_TERMINATA, date,getDirTransazioneApplicativoServerConsegnaTerminata());
  122.     }
  123.     public File getDirTransazioneApplicativoServerConsegnaTerminata(){
  124.         return new File(this.directory,Costanti.DIRECTORY_FILE_SYSTEM_REPOSITORY_TRANSAZIONE_APPLICATIVO_SERVER_CONSEGNA_TERMINATA);
  125.     }
  126.    
  127.     private void registra(byte[] oggettSerializzato, String prefix, Date date, File dir) throws CoreException, UtilsException{
  128.        
  129.         if(!dir.exists()){
  130.             this.mkdir(dir);
  131.         }
  132.        
  133.         SimpleDateFormat dateformat = DateUtils.getDefaultDateTimeFormatter(FORMAT_NEW);
  134.        
  135.         String nomeFile = prefix+"_"+dateformat.format(date)+"_"+IDUtilities.getUniqueSerialNumber("FileSystemSerializer")+".xml";
  136.         File f = new File(dir, nomeFile);
  137.         FileSystemUtilities.writeFile(f, oggettSerializzato);
  138.        
  139.     }
  140.    
  141.     private void mkdir(File dir) throws CoreException{
  142.         if(!dir.exists()){
  143.             mkdirEngine(dir);
  144.         }
  145.     }
  146.     private synchronized void mkdirEngine(File dir) throws CoreException{
  147.         if(!dir.exists() &&
  148.             !dir.mkdir()){
  149.             throw new CoreException("Directory ["+this.directory.getAbsolutePath()+"] non esistente e creazione non riuscita");
  150.         }
  151.     }
  152. }