DatabaseConfig.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.logger.config;

  21. import java.sql.Connection;

  22. import javax.sql.DataSource;

  23. import org.openspcoop2.utils.TipiDatabase;
  24. import org.openspcoop2.utils.UtilsException;

  25. /**
  26.  * DatabaseConfig
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class DatabaseConfig {

  33.     private TipiDatabase databaseType;
  34.     private boolean logSql;
  35.    
  36.     private DataSource ds;
  37.    
  38.     private Connection connection;
  39.     private Boolean isAutocommit;
  40.    
  41.     private DatabaseConfigDatasource configDatasource;
  42.    
  43.     private DatabaseConfigConnection configConnection;
  44.    
  45.     public TipiDatabase getDatabaseType() {
  46.         return this.databaseType;
  47.     }
  48.     public void setDatabaseType(TipiDatabase databaseType) {
  49.         this.databaseType = databaseType;
  50.     }
  51.    
  52.     public boolean isLogSql() {
  53.         return this.logSql;
  54.     }
  55.     public void setLogSql(boolean logSql) {
  56.         this.logSql = logSql;
  57.     }
  58.    
  59.     public DataSource getDs() {
  60.         return this.ds;
  61.     }

  62.     public void setDs(DataSource ds) {
  63.         this.ds = ds;
  64.     }

  65.     public Connection getConnection() {
  66.         return this.connection;
  67.     }

  68.     public void setConnection(Connection connection) {
  69.         this.connection = connection;
  70.     }

  71.     public Boolean getIsAutocommit() {
  72.         return this.isAutocommit;
  73.     }

  74.     public void setIsAutocommit(Boolean isAutocommit) {
  75.         this.isAutocommit = isAutocommit;
  76.     }

  77.     public DatabaseConfigDatasource getConfigDatasource() {
  78.         return this.configDatasource;
  79.     }

  80.     public void setConfigDatasource(DatabaseConfigDatasource configDatasource) {
  81.         this.configDatasource = configDatasource;
  82.     }

  83.     public DatabaseConfigConnection getConfigConnection() {
  84.         return this.configConnection;
  85.     }

  86.     public void setConfigConnection(DatabaseConfigConnection configConnection) {
  87.         this.configConnection = configConnection;
  88.     }
  89.    
  90.     public static void validate(DatabaseConfig config) throws UtilsException{
  91.         if(config==null){
  92.             throw new UtilsException("Database configuration undefined (with enabled mode)");
  93.         }
  94.         if(config.getDatabaseType()==null){
  95.             throw new UtilsException("Database configuration undefined (databaseType undefined)");
  96.         }
  97.         if(TipiDatabase.DEFAULT.equals(config.getDatabaseType())){
  98.             throw new UtilsException("Database configuration undefined (databaseType DEFAULT unsupported)");
  99.         }
  100.         int dbConfigMode = 0;
  101.         if(config.getDs()!=null){
  102.             dbConfigMode++;
  103.         }
  104.         if(config.getConnection()!=null){
  105.             dbConfigMode++;
  106.         }
  107.         if(config.getConfigConnection()!=null){
  108.             dbConfigMode++;
  109.         }
  110.         if(config.getConfigDatasource()!=null){
  111.             dbConfigMode++;
  112.         }
  113.         if(dbConfigMode==0){
  114.             throw new UtilsException("Datbase configuration uncorrect: source db configuration undefined");
  115.         }
  116.         if(dbConfigMode>1){
  117.             throw new UtilsException("Datbase configuration uncorrect: found multiple source db configuration");
  118.         }
  119.          if(config.getConnection()!=null){
  120.              if(config.getIsAutocommit()==null){
  121.                  throw new UtilsException("Database configuration undefined (autocommit mode undefined, required with connection)");
  122.              }
  123.          }
  124.     }
  125. }