ConnettorePropertiesUtilities.java
/*
* GovWay - A customizable API Gateway
* https://govway.org
*
* Copyright (c) 2005-2024 Link.it srl (https://link.it).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.openspcoop2.core.config.driver;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import org.openspcoop2.core.commons.CoreException;
import org.openspcoop2.core.commons.DBUtils;
import org.openspcoop2.core.config.Property;
import org.openspcoop2.core.constants.CostantiDB;
import org.openspcoop2.utils.sql.ISQLQueryObject;
import org.openspcoop2.utils.sql.SQLObjectFactory;
/**
* ConnettorePropertiesUtilities
*
* @author Poli Andrea (apoli@link.it)
* @author $Author$
* @version $Rev$, $Date$
*/
public class ConnettorePropertiesUtilities {
private ConnettorePropertiesUtilities() {}
public static List<Property> fromPropertiesToCollectionConfig(Properties props) {
ArrayList<Property> lista = new ArrayList<>();
Property tmp = null;
Enumeration<?> en = props.keys();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String value = (String) props.get(key);
/** log.info("loading property "+key+" - "+value);*/
tmp = new Property();
tmp.setNome(key);
tmp.setValore(value);
lista.add(tmp);
}
return lista;
}
public static List<Property> getPropertiesConnettore(String nomeConnettore,Connection con, String tipoDB) throws CoreException {
PreparedStatement stmt = null;
ResultSet risultato = null;
String path = null;
Properties prop = new Properties();
try {
ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
sqlQueryObject.addFromTable(CostantiDB.CONNETTORI_PROPERTIES);
sqlQueryObject.addSelectField("*");
sqlQueryObject.addWhereCondition("nome_connettore = ?");
String queryString = sqlQueryObject.createSQLQuery();
stmt = con.prepareStatement(queryString);
stmt.setString(1, nomeConnettore);
risultato = stmt.executeQuery();
if (risultato.next())
path = risultato.getString("path");
// controllo il path
if (path == null || path.equals(""))
throw new CoreException("getPropertiesConnettore : nessun path impostato per il connettore : " + nomeConnettore);
// Accedo al file
InputStream ins = DBUtils.class.getResourceAsStream(path);
prop.load(ins);
/** log.info("caricato il file :"+path+" con "+prop.size()+" properties.");*/
Collection<Property> collection = ConnettorePropertiesUtilities.fromPropertiesToCollectionConfig(prop);
List<Property> list = new ArrayList<>();
list.addAll(collection);
return list;
} catch (FileNotFoundException fe) {
throw new CoreException("Impossibile aprire il file :" + path + " Errore: " + fe.getMessage(),fe);
} catch (Exception e) {
throw new CoreException("Errore durante la lettura delle properties dal file [" + path + "]: " + e.getMessage(),e);
} finally {
//Chiudo statement and resultset
try{
if(risultato!=null)
risultato.close();
}catch (Exception e) {
//ignore
}
try{
if(stmt!=null)
stmt.close();
}catch (Exception e) {
//ignore
}
}
}
}