FactoryDriverConfigurazioneDBCreator.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.core.config.driver.db;

  21. import java.util.Enumeration;
  22. import java.util.Properties;

  23. import org.openspcoop2.core.commons.DBUtils;
  24. import org.openspcoop2.core.commons.FactoryDriverCreator;
  25. import org.openspcoop2.core.commons.IDriverWS;
  26. import org.openspcoop2.core.config.constants.CostantiConfigurazione;

  27. /**
  28.  *
  29.  *
  30.  * @author Stefano Corallo (corallo@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class FactoryDriverConfigurazioneDBCreator extends FactoryDriverCreator {

  35.     private String jndiName;
  36.     private String tipoDatabase;
  37.     private Properties jndiProp;

  38.     public void setJndiProp(Properties jndiProp) {
  39.         this.jndiProp = jndiProp;
  40.     }


  41.     /**
  42.      * Ritorna una nuova istanza del {@link DriverConfigurazioneDB}
  43.      */
  44.     @Override
  45.     public IDriverWS getDriver() throws Exception {

  46.         //Leggo le informazioni dal file properties
  47.         Properties prop = readProperties(super.getFilePropertiesName());

  48.         Enumeration<?> en = prop.propertyNames();
  49.         while (en.hasMoreElements()) {
  50.             String property = (String) en.nextElement();
  51.             if (property.equals("dataSource")) {
  52.                 String value = prop.getProperty(property);
  53.                 if (value!=null) {
  54.                     value = value.trim();
  55.                     this.jndiName = value;
  56.                 }
  57.             }
  58.             if (property.startsWith("dataSource.property.")) {
  59.                 String key = (property.substring("dataSource.property.".length()));
  60.                 if (key != null)
  61.                     key = key.trim();
  62.                 String value = prop.getProperty(property);
  63.                 if (value!=null)
  64.                     value = value.trim();
  65.                 if (key!=null && value!=null)
  66.                     this.jndiProp.setProperty(key,value);
  67.             }
  68.         }

  69.         // leggo tipoDatabase
  70.         if(this.jndiName.indexOf("@")!=-1){
  71.             // estrazione tipo database
  72.             try{
  73.                 this.tipoDatabase = DBUtils.estraiTipoDatabaseFromLocation(this.jndiName);
  74.                 this.jndiName = this.jndiName.substring(this.jndiName.indexOf("@")+1);
  75.             }catch(Exception e){
  76.                 throw new Exception("Analisi del tipo di database (tipoDatabase@datasource) non riuscita: "+e.getMessage());
  77.             }
  78.         }else{
  79.             // Leggo come proprieta'
  80.             this.tipoDatabase = prop.getProperty("tipoDatabase");
  81.             if(this.tipoDatabase==null){
  82.                 throw new Exception("La configurazione di tipo ["+CostantiConfigurazione.CONFIGURAZIONE_DB+"] richiede la definizione del tipo di database indicato o come prefisso della location (tipoDB@datasource) o attraverso la proprieta' 'tipoDatabase'");
  83.             }else{
  84.                 this.tipoDatabase = this.tipoDatabase.trim();
  85.             }
  86.         }
  87.        
  88.         return new DriverConfigurazioneDB(this.jndiName,this.jndiProp,this.tipoDatabase);

  89.     }


  90.     public FactoryDriverConfigurazioneDBCreator(String fileProperties)
  91.     {
  92.         super(fileProperties);
  93.     }

  94. }