FactoryDriverRegistroServiziDBCreator.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.registry.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 FactoryDriverRegistroServiziDBCreator 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.     public FactoryDriverRegistroServiziDBCreator(String fileProperties) {
  42.         super(fileProperties);

  43.     }

  44.     /**
  45.      * Ritorna una nuova istanza del {@link DriverRegistroServiziDB}
  46.      */
  47.     @Override
  48.     public IDriverWS getDriver() throws Exception {

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

  51.         Enumeration<?> en = prop.propertyNames();
  52.         while (en.hasMoreElements()) {
  53.             String property = (String) en.nextElement();
  54.             if (property.equals("dataSource")) {
  55.                 String value = prop.getProperty(property);
  56.                 if (value!=null) {
  57.                     value = value.trim();
  58.                     this.jndiName = value;
  59.                 }
  60.             }
  61.             if (property.startsWith("dataSource.property.")) {
  62.                 String key = (property.substring("dataSource.property.".length()));
  63.                 if (key != null)
  64.                     key = key.trim();
  65.                 String value = prop.getProperty(property);
  66.                 if (value!=null)
  67.                     value = value.trim();
  68.                 if (key!=null && value!=null)
  69.                     this.jndiProp.setProperty(key,value);
  70.             }
  71.         }
  72.        
  73.         // leggo tipoDatabase
  74.         if(this.jndiName.indexOf("@")!=-1){
  75.             // estrazione tipo database
  76.             try{
  77.                 this.tipoDatabase = DBUtils.estraiTipoDatabaseFromLocation(this.jndiName);
  78.                 this.jndiName = this.jndiName.substring(this.jndiName.indexOf("@")+1);
  79.             }catch(Exception e){
  80.                 throw new Exception("Analisi del tipo di database (tipoDatabase@datasource) non riuscita: "+e.getMessage());
  81.             }
  82.         }else{
  83.             // Leggo come proprieta'
  84.             this.tipoDatabase = prop.getProperty("tipoDatabase");
  85.             if(this.tipoDatabase==null){
  86.                 throw new Exception("Il registro dei servizi di tipo ["+CostantiConfigurazione.REGISTRO_DB+"] richiede la definizione del tipo di database indicato o come prefisso della location (tipoDB@datasource) o attraverso la proprieta' 'tipoDatabase'");
  87.             }else{
  88.                 this.tipoDatabase = this.tipoDatabase.trim();
  89.             }
  90.         }

  91.         return new DriverRegistroServiziDB(this.jndiName,this.jndiProp,this.tipoDatabase);

  92.     }

  93. }