DateManager.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.utils.date;

  21. import java.sql.Timestamp;
  22. import java.util.Calendar;
  23. import java.util.Date;
  24. import java.util.GregorianCalendar;
  25. import java.util.Properties;


  26. import org.slf4j.Logger;
  27. import org.openspcoop2.utils.LoggerWrapperFactory;
  28. import org.openspcoop2.utils.UtilsException;
  29. import org.openspcoop2.utils.resources.Loader;


  30. /**
  31.  * Classe per la produzione di date utilizzando
  32.  *
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public class DateManager {

  38.     /** Istanza */
  39.     private static IDate dataManager;
  40.     /** Logger */
  41.     private static Logger log;
  42.    
  43.     /**
  44.      * Inizializzazione DataManager
  45.      *
  46.      */
  47.     public static synchronized void initializeDataManager(String className,java.util.Properties properties,Logger log)throws UtilsException{
  48.        
  49.         if(DateManager.dataManager==null){
  50.             try{
  51.                 if(log!=null)
  52.                     DateManager.log = log;
  53.                 else
  54.                     DateManager.log = LoggerWrapperFactory.getLogger(DateManager.class);
  55.                 DateManager.dataManager = (IDate) Loader.getInstance().newInstance(className);
  56.                 DateManager.dataManager.init(properties);
  57.             }catch(Exception e){
  58.                 throw new UtilsException("Riscontrato errore durante il caricamento del data manager specificato [class:"+className+"]: "+e.getMessage(),e);
  59.             }
  60.         }
  61.     }
  62.    
  63.     /**
  64.      * Close il DataManager
  65.      *
  66.      * @throws UtilsException
  67.      */
  68.     public static void close() {
  69.         if(DateManager.dataManager!=null){
  70.             try{
  71.                 DateManager.dataManager.close();
  72.             }catch(Exception e){
  73.                 DateManager.log.error("Riscontrato errore durante la chiusura del data manager: "+e.getMessage(),e);
  74.             }
  75.         }
  76.     }
  77.    
  78.    
  79.    
  80.     /**
  81.      * Ritorna la data corrente.
  82.      *
  83.      * @return Data corrente
  84.      */
  85.     public static Date getDate() {
  86.         try{
  87.             if(DateManager.dataManager==null){
  88.                 Logger log = LoggerWrapperFactory.getLogger(DateManager.class);
  89.                 log.warn("DateManager non inizializzato");
  90.                 DateManager.initializeDataManager("org.openspcoop2.utils.date.SystemDate", new Properties(), log);
  91.             }
  92.             return DateManager.dataManager.getDate();
  93.         }catch(Exception e){
  94.             DateManager.log.error("DateManager.getDate() non riuscita",e);
  95.             return new java.util.Date();
  96.         }
  97.     }
  98.    
  99.     /**
  100.      * Ritorna la data corrente in millisecondi da Gennaio 1.4970.
  101.      *
  102.      * @return Data corrente
  103.      */
  104.     public static long getTimeMillis() {
  105.         try{
  106.             if(DateManager.dataManager==null){
  107.                 Logger log = LoggerWrapperFactory.getLogger(DateManager.class);
  108.                 log.warn("DateManager non inizializzato");
  109.                 DateManager.initializeDataManager("org.openspcoop2.utils.date.SystemDate", new Properties(), log);
  110.             }
  111.             return DateManager.dataManager.getTimeMillis();
  112.         }catch(Exception e){
  113.             DateManager.log.error("DateManager.getTimeMillis() non riuscita",e);
  114.             return new java.util.Date().getTime();
  115.         }
  116.     }
  117.    
  118.     /**
  119.      * Ritorna la data corrente come timestamp SQL.
  120.      *
  121.      * @return Data corrente
  122.      */
  123.     public static Timestamp getTimestamp() {
  124.         try{
  125.             if(DateManager.dataManager==null){
  126.                 Logger log = LoggerWrapperFactory.getLogger(DateManager.class);
  127.                 log.warn("DateManager non inizializzato");
  128.                 DateManager.initializeDataManager("org.openspcoop2.utils.date.SystemDate", new Properties(), log);
  129.             }
  130.             return DateManager.dataManager.getTimestamp();
  131.         }catch(Exception e){
  132.             DateManager.log.error("DateManager.getTimestamp() non riuscita",e);
  133.             return new Timestamp(new java.util.Date().getTime());
  134.         }
  135.     }
  136.    
  137.     /**
  138.      * Ritorna la data corrente come Calendar
  139.      *
  140.      * @return Data corrente
  141.      */
  142.     public static Calendar getCalendar() throws UtilsException{
  143.         try{
  144.             if(DateManager.dataManager==null){
  145.                 Logger log = LoggerWrapperFactory.getLogger(DateManager.class);
  146.                 log.warn("DateManager non inizializzato");
  147.                 DateManager.initializeDataManager("org.openspcoop2.utils.date.SystemDate", new Properties(), log);
  148.             }
  149.             return DateManager.dataManager.getCalendar();
  150.         }catch(Exception e){
  151.             DateManager.log.error("DateManager.getCalendar() non riuscita",e);
  152.             Calendar c = new GregorianCalendar();
  153.             c.setTime(new java.util.Date());
  154.             return c;
  155.         }
  156.     }
  157. }