DBManager.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.web.ctrlstat.core;

  21. import java.sql.Connection;
  22. import java.sql.SQLException;
  23. import java.util.Enumeration;

  24. import javax.naming.InitialContext;
  25. import javax.sql.DataSource;

  26. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  27. import org.slf4j.Logger;

  28. /**
  29.  * Contiene la gestione delle connessioni ad un Database. Il nome della risorsa
  30.  * JNDI da cui e' possibile attingere connessioni verso il Database, viene
  31.  * selezionato attraverso le impostazioni lette dal file
  32.  * 'console.datasource.properties'.
  33.  *
  34.  * @author Andrea Poli (apoli@link.it)
  35.  * @author Stefano Corallo (corallo@link.it)
  36.  * @author Sandra Giangrandi (sandra@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  *
  40.  */
  41. public class DBManager {

  42.     private static Logger checkLogger = null;
  43.     private static boolean checkIsClosed = true;
  44.     private static boolean checkAutocommit = true;
  45.     public static boolean isCheckIsClosed() {
  46.         return checkIsClosed;
  47.     }
  48.     public static void setCheckIsClosed(boolean checkIsClosed) {
  49.         DBManager.checkIsClosed = checkIsClosed;
  50.     }
  51.     public static boolean isCheckAutocommit() {
  52.         return checkAutocommit;
  53.     }
  54.     public static void setCheckAutocommit(boolean checkAutocommit) {
  55.         DBManager.checkAutocommit = checkAutocommit;
  56.     }
  57.     public static Logger getCheckLogger() {
  58.         return checkLogger;
  59.     }
  60.     public static void setCheckLogger(Logger checkLogger) {
  61.         DBManager.checkLogger = checkLogger;
  62.     }
  63.    
  64.    
  65.     /** Logger utilizzato per debug. */
  66.     private static Logger log = null;

  67.     /** DBManager */
  68.     private static DBManager manager = null;

  69.     private static boolean initialized = false;

  70.     /** DataSource dove attingere connessioni */
  71.     private DataSource dataSource = null;

  72.     private String dataSourceName = null;
  73.     private java.util.Properties dataSourceContext = null;
  74.     public String getDataSourceName() {
  75.         return this.dataSourceName;
  76.     }

  77.     public java.util.Properties getDataSourceContext() {
  78.         return this.dataSourceContext;
  79.     }
  80.    
  81.     /**
  82.      * Viene chiamato in causa per istanziare il datasource
  83.      *
  84.      * @param jndiName
  85.      *            Nome JNDI del Datasource
  86.      * @param context
  87.      *            Contesto JNDI da utilizzare
  88.      */
  89.     private DBManager(String jndiName, java.util.Properties context) throws Exception {
  90.         try {
  91.             DBManager.log = ControlStationLogger.getPddConsoleCoreLogger();
  92.             this.dataSourceName = jndiName;
  93.             this.dataSourceContext = context;
  94.            
  95.             if (context != null) {
  96.                 DBManager.log.info("Proprieta' di contesto:" + context.size());
  97.                 Enumeration<?> en = context.keys();
  98.                 while (en.hasMoreElements()) {
  99.                     String key = (String) en.nextElement();
  100.                     DBManager.log.info("\tNome[" + key + "] Valore[" + context.getProperty(key) + "]");
  101.                 }
  102.             } else {
  103.                 DBManager.log.info("Proprieta' di contesto non fornite");
  104.             }

  105.             DBManager.log.info("Nome dataSource:" + jndiName);

  106.             InitialContext initC = null;
  107.             if (context != null && context.size() > 0)
  108.                 initC = new InitialContext(context);
  109.             else
  110.                 initC = new InitialContext();
  111.             this.dataSource = (DataSource) initC.lookup(jndiName);
  112.             initC.close();

  113.         } catch (Exception e) {
  114.             DBManager.log.error("Lookup datasource non riuscita", e);
  115.             throw e;
  116.         }
  117.     }

  118.     /**
  119.      * Il Metodo si occupa di inizializzare il propertiesReader del QueueManager
  120.      *
  121.      * @param jndiName
  122.      *            Nome JNDI del Datasource
  123.      * @param context
  124.      *            Contesto JNDI da utilizzare
  125.      */
  126.     public static boolean initialize(String jndiName, java.util.Properties context) throws Exception {
  127.         if (DBManager.manager == null) {
  128.             initializeEngine(jndiName, context);
  129.         }
  130.         return DBManager.initialized;
  131.     }
  132.     private static synchronized void initializeEngine(String jndiName, java.util.Properties context) throws Exception {
  133.         try {
  134.             if (DBManager.manager == null) {
  135.                 DBManager.manager = new DBManager(jndiName, context);
  136.                 DBManager.setInitialized(true);
  137.             }
  138.         } catch (Exception e) {
  139.             DBManager.setInitialized(false);
  140.             DBManager.log.debug("Errore di inizializzazione del Database", e);
  141.             throw e;
  142.         }
  143.     }

  144.     /**
  145.      * Ritorna l'istanza di questo DBManager
  146.      *
  147.      * @return Istanza di DBManager
  148.      */
  149.     public static DBManager getInstance() {
  150.         if(DBManager.manager==null) {
  151.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  152.             synchronized (DBManager.class) {
  153.                 return DBManager.manager;
  154.             }
  155.         }
  156.         return DBManager.manager;
  157.     }

  158.     /**
  159.      * Viene chiamato in causa per ottenere una connessione al DB
  160.      */
  161.     public java.sql.Connection getConnection() {
  162.         if (this.dataSource == null) {
  163.             return null;
  164.         }

  165.         Connection connectionDB = null;
  166.         try {
  167.             connectionDB = this.dataSource.getConnection();
  168.         } catch (Exception e) {
  169.             DBManager.log.error("getConnection from db", e);
  170.             return null;
  171.         }

  172.         return connectionDB;
  173.     }

  174.     /**
  175.      * Viene chiamato in causa per rilasciare una connessione al DB, effettuando
  176.      * precedentemente un commit
  177.      *
  178.      * @param connectionDB
  179.      *            Connessione da rilasciare.
  180.      */
  181.     public void releaseConnection(java.sql.Connection connectionDB) {
  182.         try {
  183.             JDBCUtilities.closeConnection(checkLogger, connectionDB, checkAutocommit, checkIsClosed);
  184.         } catch (SQLException e) {
  185.             DBManager.log.error("closeConnection db", e);
  186.         }
  187.     }

  188.     public static boolean isInitialized() {
  189.         return DBManager.initialized;
  190.     }

  191.     private static void setInitialized(boolean initialized) {
  192.         DBManager.initialized = initialized;
  193.     }
  194. }