ConnettorePropertiesUtilities.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;

  21. import java.io.FileNotFoundException;
  22. import java.io.InputStream;
  23. import java.sql.Connection;
  24. import java.sql.PreparedStatement;
  25. import java.sql.ResultSet;
  26. import java.util.ArrayList;
  27. import java.util.Collection;
  28. import java.util.Enumeration;
  29. import java.util.List;
  30. import java.util.Properties;

  31. import org.openspcoop2.core.commons.CoreException;
  32. import org.openspcoop2.core.commons.DBUtils;
  33. import org.openspcoop2.core.config.Property;
  34. import org.openspcoop2.core.constants.CostantiDB;
  35. import org.openspcoop2.utils.sql.ISQLQueryObject;
  36. import org.openspcoop2.utils.sql.SQLObjectFactory;

  37. /**
  38.  * ConnettorePropertiesUtilities
  39.  *  
  40.  * @author Poli Andrea (apoli@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */
  44. public class ConnettorePropertiesUtilities {

  45.     private ConnettorePropertiesUtilities() {}
  46.    
  47.     public static List<Property> fromPropertiesToCollectionConfig(Properties props) {
  48.         ArrayList<Property> lista = new ArrayList<>();

  49.         Property tmp = null;
  50.         Enumeration<?> en = props.keys();
  51.         while (en.hasMoreElements()) {
  52.             String key = (String) en.nextElement();
  53.             String value = (String) props.get(key);
  54.             /** log.info("loading property "+key+" - "+value);*/
  55.             tmp = new Property();
  56.             tmp.setNome(key);
  57.             tmp.setValore(value);

  58.             lista.add(tmp);
  59.         }

  60.         return lista;
  61.     }
  62.    
  63.     public static List<Property> getPropertiesConnettore(String nomeConnettore,Connection con, String tipoDB) throws CoreException {

  64.         PreparedStatement stmt = null;
  65.         ResultSet risultato = null;
  66.         String path = null;
  67.         Properties prop = new Properties();

  68.         try {

  69.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  70.             sqlQueryObject.addFromTable(CostantiDB.CONNETTORI_PROPERTIES);
  71.             sqlQueryObject.addSelectField("*");
  72.             sqlQueryObject.addWhereCondition("nome_connettore = ?");
  73.             String queryString = sqlQueryObject.createSQLQuery();

  74.             stmt = con.prepareStatement(queryString);
  75.             stmt.setString(1, nomeConnettore);

  76.             risultato = stmt.executeQuery();

  77.             if (risultato.next())
  78.                 path = risultato.getString("path");

  79.             // controllo il path
  80.             if (path == null || path.equals(""))
  81.                 throw new CoreException("getPropertiesConnettore : nessun path impostato per il connettore : " + nomeConnettore);

  82.             // Accedo al file
  83.             InputStream ins = DBUtils.class.getResourceAsStream(path);

  84.             prop.load(ins);

  85.             /** log.info("caricato il file :"+path+" con "+prop.size()+" properties.");*/

  86.             Collection<Property> collection = ConnettorePropertiesUtilities.fromPropertiesToCollectionConfig(prop);

  87.             List<Property> list = new ArrayList<>();
  88.             list.addAll(collection);

  89.             return list;

  90.         } catch (FileNotFoundException fe) {
  91.             throw new CoreException("Impossibile aprire il file :" + path + " Errore: " + fe.getMessage(),fe);
  92.         } catch (Exception e) {
  93.             throw new CoreException("Errore durante la lettura delle properties dal file [" + path + "]: " + e.getMessage(),e);
  94.         } finally {
  95.             //Chiudo statement and resultset
  96.             try{
  97.                 if(risultato!=null)
  98.                     risultato.close();
  99.             }catch (Exception e) {
  100.                 //ignore
  101.             }
  102.             try{
  103.                 if(stmt!=null)
  104.                     stmt.close();
  105.             }catch (Exception e) {
  106.                 //ignore
  107.             }
  108.         }
  109.     }
  110. }