DriverRegistroServiziDB_connettoriDriver.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.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.util.List;

  26. import org.apache.commons.lang.StringUtils;
  27. import org.openspcoop2.core.byok.IDriverBYOK;
  28. import org.openspcoop2.core.commons.CoreException;
  29. import org.openspcoop2.core.commons.DBUtils;
  30. import org.openspcoop2.core.constants.CostantiConnettori;
  31. import org.openspcoop2.core.constants.CostantiDB;
  32. import org.openspcoop2.core.constants.TipiConnettore;
  33. import org.openspcoop2.core.registry.Connettore;
  34. import org.openspcoop2.core.registry.Property;
  35. import org.openspcoop2.core.registry.driver.ConnettorePropertiesUtilities;
  36. import org.openspcoop2.core.registry.driver.DriverRegistroServiziException;
  37. import org.openspcoop2.core.registry.driver.DriverRegistroServiziNotFound;
  38. import org.openspcoop2.utils.UtilsException;
  39. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  40. import org.openspcoop2.utils.sql.ISQLQueryObject;
  41. import org.openspcoop2.utils.sql.SQLObjectFactory;

  42. /**
  43.  * DriverRegistroServiziDB_connettoriDriver
  44.  *
  45.  *
  46.  * @author Sandra Giangrandi (sandra@link.it)
  47.  * @author Stefano Corallo (corallo@link.it)
  48.  * @author $Author$
  49.  * @version $Rev$, $Date$
  50.  */
  51. public class DriverRegistroServiziDB_connettoriDriver {

  52.     private DriverRegistroServiziDB driver = null;
  53.    
  54.     protected DriverRegistroServiziDB_connettoriDriver(DriverRegistroServiziDB driver) {
  55.         this.driver = driver;
  56.     }
  57.    
  58.     protected Connettore getConnettore(long idConnettore) throws DriverRegistroServiziException, DriverRegistroServiziNotFound {
  59.         String nomeMetodo = "getConnettore(id)";
  60.        
  61.         Connection con = null;
  62.         if (this.driver.atomica) {
  63.             try {
  64.                 con = this.driver.getConnectionFromDatasource(nomeMetodo);
  65.             } catch (Exception e) {
  66.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(),e);

  67.             }

  68.         } else
  69.             con = this.driver.globalConnection;

  70.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  71.         try {

  72.             Connettore connettore = getConnettore(idConnettore, con);
  73.             if(connettore==null) {
  74.                 throw new DriverRegistroServiziNotFound("Connettore con id '"+idConnettore+"' non esistente");
  75.             }
  76.             return connettore;
  77.            
  78.         } catch (Exception qe) {
  79.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  80.         } finally {

  81.             this.driver.closeConnection(con);
  82.         }
  83.     }
  84.     protected Connettore getConnettore(String nomeConnettore) throws DriverRegistroServiziException {
  85.         String nomeMetodo = "getConnettore(nome)";
  86.        
  87.         Connection con = null;
  88.         PreparedStatement stmt=null;
  89.         ResultSet risultato=null;
  90.        
  91.         if (this.driver.atomica) {
  92.             try {
  93.                 con = this.driver.getConnectionFromDatasource(nomeMetodo);
  94.             } catch (Exception e) {
  95.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(),e);

  96.             }

  97.         } else
  98.             con = this.driver.globalConnection;

  99.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  100.         try {

  101.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  102.             sqlQueryObject.addFromTable(CostantiDB.CONNETTORI);
  103.             sqlQueryObject.addSelectField("id");
  104.             sqlQueryObject.addWhereCondition("nome_connettore=?");
  105.             String queryString = sqlQueryObject.createSQLQuery();
  106.             stmt = con.prepareStatement(queryString);
  107.             stmt.setString(1, nomeConnettore);
  108.             risultato = stmt.executeQuery();

  109.             Long idConnettore = null;
  110.             if (risultato.next()) {
  111.                 idConnettore = risultato.getLong("id");
  112.             }
  113.             else {
  114.                 throw new DriverRegistroServiziNotFound("Connettore con nome '"+nomeConnettore+"' non esistente");
  115.             }
  116.                    
  117.             Connettore connettore = getConnettore(idConnettore, con);
  118.             if(connettore==null) {
  119.                 throw new DriverRegistroServiziNotFound("Connettore con id '"+idConnettore+"' non esistente");
  120.             }
  121.             return connettore;
  122.            
  123.         } catch (Exception qe) {
  124.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  125.         } finally {

  126.             //Chiudo statement and resultset
  127.             JDBCUtilities.closeResources(risultato, stmt);

  128.             this.driver.closeConnection(con);
  129.         }
  130.     }
  131.    
  132.     protected Connettore getConnettore(long idConnettore, Connection connection) throws DriverRegistroServiziException {

  133.         Connettore connettore = null;

  134.         // accedo alla tab regserv_connettori

  135.         PreparedStatement stm = null;
  136.         ResultSet rs = null;

  137.         try {
  138.             IDriverBYOK driverBYOK = this.driver.getDriverUnwrapBYOK();
  139.            
  140.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  141.             sqlQueryObject.addFromTable(CostantiDB.CONNETTORI);
  142.             sqlQueryObject.addSelectField("*");
  143.             sqlQueryObject.addWhereCondition("id = ?");
  144.             String sqlQuery = sqlQueryObject.createSQLQuery();

  145.             stm = connection.prepareStatement(sqlQuery);
  146.             stm.setLong(1, idConnettore);

  147.             this.driver.logDebug("eseguo query : " + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, idConnettore));

  148.             rs = stm.executeQuery();

  149.             if (rs.next()) {
  150.                 String endpoint = rs.getString(CostantiDB.CONNETTORI_COLUMN_ENDPOINT_TYPE);
  151.                 if (endpoint == null || endpoint.equals("") || endpoint.equals(TipiConnettore.DISABILITATO.getNome())) {
  152.                     connettore = new Connettore();
  153.                     connettore.setNome(rs.getString(CostantiDB.CONNETTORI_COLUMN_NOME));
  154.                     connettore.setTipo(TipiConnettore.DISABILITATO.getNome());
  155.                     connettore.setId(idConnettore);

  156.                 } else {
  157.                     Property prop = null;
  158.                     connettore = new Connettore();
  159.                     connettore.setNome(rs.getString(CostantiDB.CONNETTORI_COLUMN_NOME));
  160.                     connettore.setTipo(endpoint);
  161.                     //l'id del connettore e' quello passato come parametro
  162.                     connettore.setId(idConnettore);

  163.                     // Debug
  164.                     if(rs.getInt("debug")==1){
  165.                         prop = new Property();
  166.                         prop.setNome(CostantiDB.CONNETTORE_DEBUG);
  167.                         prop.setValore("true");
  168.                         connettore.addProperty(prop);
  169.                     }

  170.                     // Proxy
  171.                     readConnettoreProxy(rs, connettore, driverBYOK);
  172.                    
  173.                     // Tempi Risposta
  174.                     readConnettoreTempiRisposta(rs, connettore);
  175.                    
  176.                     // transfer_mode
  177.                     readConnettoreTransferMode(rs, connettore);
  178.                    
  179.                     // redirect_mode
  180.                     readConnettoreRedirectMode(rs, connettore);
  181.                                
  182.                     // token policy
  183.                     String tokenPolicy = rs.getString("token_policy");
  184.                     if(tokenPolicy!=null && !"".equals(tokenPolicy)){
  185.                        
  186.                         prop = new Property();
  187.                         prop.setNome(CostantiDB.CONNETTORE_TOKEN_POLICY);
  188.                         prop.setValore(tokenPolicy.trim());
  189.                         connettore.addProperty(prop);
  190.                        
  191.                     }
  192.                    
  193.                     // api key
  194.                     readAutenticazioneApiKey(rs, connettore, driverBYOK);
  195.                    
  196.                     if (endpoint.equals(CostantiDB.CONNETTORE_TIPO_HTTP)) {
  197.                         readConnettoreHttp(rs, connettore, driverBYOK);
  198.                     } else if (endpoint.equals(TipiConnettore.JMS.getNome())){
  199.                         readConnettoreJms(rs, connettore, driverBYOK);
  200.                     }else if(endpoint.equals(TipiConnettore.NULL.getNome())){
  201.                         //nessuna proprieta per connettore null
  202.                     }else if(endpoint.equals(TipiConnettore.NULLECHO.getNome())){
  203.                         //nessuna proprieta per connettore nullEcho
  204.                     }else if (!endpoint.equals(TipiConnettore.DISABILITATO.getNome())) {
  205.                         if(rs.getLong("custom")==1){
  206.                             // connettore custom
  207.                             readPropertiesConnettoreCustom(idConnettore,connettore,connection,driverBYOK);
  208.                             connettore.setCustom(true);
  209.                         }
  210.                         else{
  211.                             // legge da file properties
  212.                             connettore.setPropertyList(ConnettorePropertiesUtilities.getPropertiesConnettore(endpoint,connection,this.driver.tipoDB));
  213.                         }
  214.                     }

  215.                 }
  216.             }
  217.            
  218.             // Extended Info
  219.             this.readPropertiesConnettoreExtendedInfo(idConnettore,connettore,connection,driverBYOK);
  220.            
  221.             return connettore;
  222.         } catch (SQLException sqle) {
  223.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getConnettore] SQLException : " + sqle.getMessage(),sqle);
  224.         } catch (CoreException e) {
  225.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getConnettore] CoreException : " + e.getMessage(),e);
  226.         }catch (Exception sqle) {
  227.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getConnettore] Exception : " + sqle.getMessage(),sqle);
  228.         }finally {
  229.             // chiudo lo statement e resultset
  230.             JDBCUtilities.closeResources(rs, stm);
  231.         }
  232.     }
  233.    
  234.     private static void readConnettoreProxy(ResultSet rs, Connettore connettore, IDriverBYOK driverBYOK) throws SQLException, UtilsException {
  235.         if(rs.getInt("proxy")==1){
  236.            
  237.             String tmp = rs.getString("proxy_type");
  238.             if(tmp!=null && !"".equals(tmp)){
  239.                 Property prop = new Property();
  240.                 prop.setNome(CostantiDB.CONNETTORE_PROXY_TYPE);
  241.                 prop.setValore(tmp.trim());
  242.                 connettore.addProperty(prop);
  243.             }
  244.            
  245.             tmp = rs.getString("proxy_hostname");
  246.             if(tmp!=null && !"".equals(tmp)){
  247.                 Property prop = new Property();
  248.                 prop.setNome(CostantiDB.CONNETTORE_PROXY_HOSTNAME);
  249.                 prop.setValore(tmp.trim());
  250.                 connettore.addProperty(prop);
  251.             }
  252.            
  253.             tmp = rs.getString("proxy_port");
  254.             if(tmp!=null && !"".equals(tmp)){
  255.                 Property prop = new Property();
  256.                 prop.setNome(CostantiDB.CONNETTORE_PROXY_PORT);
  257.                 prop.setValore(tmp.trim());
  258.                 connettore.addProperty(prop);
  259.             }
  260.            
  261.             readConnettoreProxyCredentials(rs, connettore, driverBYOK);
  262.         }
  263.     }
  264.     private static void readConnettoreProxyCredentials(ResultSet rs, Connettore connettore, IDriverBYOK driverBYOK) throws SQLException, UtilsException {
  265.         String tmp = rs.getString("proxy_username");
  266.         if(tmp!=null && !"".equals(tmp)){
  267.             Property prop = new Property();
  268.             prop.setNome(CostantiDB.CONNETTORE_PROXY_USERNAME);
  269.             prop.setValore(tmp.trim());
  270.             connettore.addProperty(prop);
  271.         }
  272.        
  273.         tmp = rs.getString("proxy_password");
  274.         String encValue = rs.getString("enc_proxy_password");
  275.         if(tmp!=null && !"".equals(tmp)){
  276.             Property prop = new Property();
  277.             prop.setNome(CostantiDB.CONNETTORE_PROXY_PASSWORD);
  278.            
  279.             if(encValue!=null && StringUtils.isNotEmpty(encValue)) {
  280.                 if(driverBYOK!=null) {
  281.                     prop.setValore(driverBYOK.unwrapAsString(encValue));
  282.                 }
  283.                 else {
  284.                     prop.setValore(encValue);
  285.                 }
  286.             }
  287.             else {
  288.                 prop.setValore(tmp.trim());
  289.             }
  290.            
  291.             connettore.addProperty(prop);
  292.         }
  293.     }
  294.    
  295.     private static void readConnettoreTempiRisposta(ResultSet rs, Connettore connettore) throws SQLException {
  296.         int connectionTimeout = rs.getInt("connection_timeout");
  297.         if(connectionTimeout>0){
  298.            
  299.             Property prop = new Property();
  300.             prop.setNome(CostantiDB.CONNETTORE_CONNECTION_TIMEOUT);
  301.             prop.setValore(connectionTimeout+"");
  302.             connettore.addProperty(prop);
  303.            
  304.         }
  305.         int readTimeout = rs.getInt("read_timeout");
  306.         if(readTimeout>0){
  307.            
  308.             Property prop = new Property();
  309.             prop.setNome(CostantiDB.CONNETTORE_READ_CONNECTION_TIMEOUT);
  310.             prop.setValore(readTimeout+"");
  311.             connettore.addProperty(prop);
  312.            
  313.         }
  314.         int avgResponseTime = rs.getInt("avg_response_time");
  315.         if(avgResponseTime>0){
  316.            
  317.             Property prop = new Property();
  318.             prop.setNome(CostantiDB.CONNETTORE_TEMPO_MEDIO_RISPOSTA);
  319.             prop.setValore(avgResponseTime+"");
  320.             connettore.addProperty(prop);
  321.            
  322.         }
  323.     }

  324.     private static void readConnettoreTransferMode(ResultSet rs, Connettore connettore) throws SQLException {
  325.         String transferMode = rs.getString("transfer_mode");
  326.         if(transferMode!=null && !"".equals(transferMode)){
  327.            
  328.             Property prop = new Property();
  329.             prop.setNome(CostantiDB.CONNETTORE_HTTP_DATA_TRANSFER_MODE);
  330.             prop.setValore(transferMode.trim());
  331.             connettore.addProperty(prop);
  332.            
  333.             transferMode = rs.getString("transfer_mode_chunk_size");
  334.             if(transferMode!=null && !"".equals(transferMode)){
  335.                 prop = new Property();
  336.                 prop.setNome(CostantiDB.CONNETTORE_HTTP_DATA_TRANSFER_MODE_CHUNK_SIZE);
  337.                 prop.setValore(transferMode.trim());
  338.                 connettore.addProperty(prop);
  339.             }
  340.         }
  341.     }
  342.    
  343.     private static void readConnettoreRedirectMode(ResultSet rs, Connettore connettore) throws SQLException {
  344.         String redirectMode = rs.getString("redirect_mode");
  345.         if(redirectMode!=null && !"".equals(redirectMode)){
  346.            
  347.             Property prop = new Property();
  348.             prop.setNome(CostantiDB.CONNETTORE_HTTP_REDIRECT_FOLLOW);
  349.             prop.setValore(redirectMode.trim());
  350.             connettore.addProperty(prop);
  351.            
  352.             redirectMode = rs.getString("redirect_max_hop");
  353.             if(redirectMode!=null && !"".equals(redirectMode)){
  354.                 prop = new Property();
  355.                 prop.setNome(CostantiDB.CONNETTORE_HTTP_REDIRECT_MAX_HOP);
  356.                 prop.setValore(redirectMode.trim());
  357.                 connettore.addProperty(prop);
  358.             }
  359.         }
  360.     }
  361.    
  362.     private static void readAutenticazioneApiKey(ResultSet rs, Connettore connettore, IDriverBYOK driverBYOK) throws SQLException, UtilsException {
  363.         String apiKey = rs.getString("api_key");
  364.         if(apiKey!=null && !"".equals(apiKey)){
  365.            
  366.             Property prop = new Property();
  367.             prop.setNome(CostantiDB.CONNETTORE_APIKEY);
  368.             if(driverBYOK!=null) {
  369.                 prop.setValore(driverBYOK.unwrapAsString(apiKey));
  370.             }
  371.             else {
  372.                 prop.setValore(apiKey);
  373.             }
  374.             connettore.addProperty(prop);
  375.            
  376.             String apiKeyHeader = rs.getString("api_key_header");
  377.             if(apiKeyHeader!=null && !"".equals(apiKeyHeader)){
  378.                 prop = new Property();
  379.                 prop.setNome(CostantiDB.CONNETTORE_APIKEY_HEADER);
  380.                 prop.setValore(apiKeyHeader.trim());
  381.                 connettore.addProperty(prop);
  382.             }
  383.            
  384.            
  385.             String appId = rs.getString("app_id");
  386.             if(appId!=null && !"".equals(appId)){
  387.                
  388.                 prop = new Property();
  389.                 prop.setNome(CostantiDB.CONNETTORE_APIKEY_APPID);
  390.                 prop.setValore(appId);
  391.                 connettore.addProperty(prop);
  392.                
  393.                 String appIdHeader = rs.getString("app_id_header");
  394.                 if(appIdHeader!=null && !"".equals(appIdHeader)){
  395.                     prop = new Property();
  396.                     prop.setNome(CostantiDB.CONNETTORE_APIKEY_APPID_HEADER);
  397.                     prop.setValore(appIdHeader.trim());
  398.                     connettore.addProperty(prop);
  399.                 }
  400.             }
  401.         }
  402.     }
  403.    
  404.     private static void readConnettoreHttp(ResultSet rs, Connettore connettore, IDriverBYOK driverBYOK) throws DriverRegistroServiziException, SQLException, UtilsException {
  405.         // url
  406.         String value = rs.getString("url");
  407.         if(value!=null)
  408.             value = value.trim();
  409.         if(value == null || "".equals(value) || " ".equals(value)){
  410.             throw new DriverRegistroServiziException("Connettore di tipo http possiede una url non definita");
  411.         }
  412.         Property prop = new Property();
  413.         prop.setNome(CostantiDB.CONNETTORE_HTTP_LOCATION);
  414.         prop.setValore(value);
  415.         connettore.addProperty(prop);
  416.        
  417.         // user
  418.         String usr = rs.getString("utente");
  419.         if (usr != null && !usr.trim().equals("")) {
  420.             prop = new Property();
  421.             prop.setNome(CostantiDB.CONNETTORE_USER);
  422.             prop.setValore(usr);
  423.             connettore.addProperty(prop);
  424.         }
  425.         // password
  426.         String pwd = rs.getString("password");
  427.         String encValue = rs.getString("enc_password");
  428.         if (pwd != null && !pwd.trim().equals("")) {
  429.             prop = new Property();
  430.             prop.setNome(CostantiDB.CONNETTORE_PWD);
  431.            
  432.             if(encValue!=null && StringUtils.isNotEmpty(encValue)) {
  433.                 if(driverBYOK!=null) {
  434.                     prop.setValore(driverBYOK.unwrapAsString(encValue));
  435.                 }
  436.                 else {
  437.                     prop.setValore(encValue);
  438.                 }
  439.             }
  440.             else {
  441.                 prop.setValore(pwd);
  442.             }
  443.            
  444.             connettore.addProperty(prop);
  445.         }
  446.     }
  447.    
  448.     private static void readConnettoreJms(ResultSet rs, Connettore connettore, IDriverBYOK driverBYOK) throws DriverRegistroServiziException, SQLException, UtilsException {
  449.         // nome coda/topic
  450.         String value = rs.getString("nome");
  451.         if(value!=null)
  452.             value = value.trim();
  453.         if(value == null || "".equals(value) || " ".equals(value)){
  454.             throw new DriverRegistroServiziException("Connettore di tipo jms possiede il nome della coda/topic non definito");
  455.         }
  456.         Property prop = new Property();
  457.         prop.setNome(CostantiDB.CONNETTORE_JMS_NOME);
  458.         prop.setValore(value);
  459.         connettore.addProperty(prop);

  460.         // tipo
  461.         value = rs.getString("tipo");
  462.         if(value!=null)
  463.             value = value.trim();
  464.         if(value == null || "".equals(value) || " ".equals(value)){
  465.             throw new DriverRegistroServiziException("Connettore di tipo jms possiede il tipo della coda non definito");
  466.         }
  467.         prop = new Property();
  468.         prop.setNome(CostantiDB.CONNETTORE_JMS_TIPO);
  469.         prop.setValore(value);
  470.         connettore.addProperty(prop);

  471.         // connection-factory
  472.         value = rs.getString("connection_factory");
  473.         if(value!=null)
  474.             value = value.trim();
  475.         if(value == null || "".equals(value) || " ".equals(value)){
  476.             throw new DriverRegistroServiziException("Connettore di tipo jms non possiede la definizione di una Connection Factory");
  477.         }
  478.         prop = new Property();
  479.         prop.setNome(CostantiDB.CONNETTORE_JMS_CONNECTION_FACTORY);
  480.         prop.setValore(value);
  481.         connettore.addProperty(prop);

  482.         // send_as
  483.         value = rs.getString("send_as");
  484.         if(value!=null)
  485.             value = value.trim();
  486.         if(value == null || "".equals(value) || " ".equals(value)){
  487.             throw new DriverRegistroServiziException("Connettore di tipo jms possiede il tipo dell'oggetto JMS non definito");
  488.         }
  489.         prop = new Property();
  490.         prop.setNome(CostantiDB.CONNETTORE_JMS_SEND_AS);
  491.         prop.setValore(value);
  492.         connettore.addProperty(prop);

  493.         readConnettoreJmsCredentials(rs, connettore, driverBYOK);
  494.        
  495.         readConnettoreJmsContext(rs, connettore);
  496.     }
  497.     private static void readConnettoreJmsCredentials(ResultSet rs, Connettore connettore, IDriverBYOK driverBYOK) throws SQLException, UtilsException {
  498.         // user
  499.         String usr = rs.getString("utente");
  500.         if (usr != null && !usr.trim().equals("")) {
  501.             Property prop = new Property();
  502.             prop.setNome(CostantiDB.CONNETTORE_USER);
  503.             prop.setValore(usr);
  504.             connettore.addProperty(prop);
  505.         }
  506.         // password
  507.         String pwd = rs.getString("password");
  508.         String encValue = rs.getString("enc_password");
  509.         if (pwd != null && !pwd.trim().equals("")) {
  510.             Property prop = new Property();
  511.             prop.setNome(CostantiDB.CONNETTORE_PWD);
  512.            
  513.             if(encValue!=null && StringUtils.isNotEmpty(encValue)) {
  514.                 if(driverBYOK!=null) {
  515.                     prop.setValore(driverBYOK.unwrapAsString(encValue));
  516.                 }
  517.                 else {
  518.                     prop.setValore(encValue);
  519.                 }
  520.             }
  521.             else {
  522.                 prop.setValore(pwd);
  523.             }
  524.            
  525.             connettore.addProperty(prop);
  526.         }
  527.     }
  528.     private static void readConnettoreJmsContext(ResultSet rs, Connettore connettore) throws SQLException {
  529.         // context-java.naming.factory.initial
  530.         String initcont = rs.getString("initcont");
  531.         if (initcont != null && !initcont.trim().equals("")) {
  532.             Property prop = new Property();
  533.             prop.setNome(CostantiDB.CONNETTORE_JMS_CONTEXT_JAVA_NAMING_FACTORY_INITIAL);
  534.             prop.setValore(initcont);
  535.             connettore.addProperty(prop);
  536.         }
  537.         // context-java.naming.factory.url.pkgs
  538.         String urlpkg = rs.getString("urlpkg");
  539.         if (urlpkg != null && !urlpkg.trim().equals("")) {
  540.             Property prop = new Property();
  541.             prop.setNome(CostantiDB.CONNETTORE_JMS_CONTEXT_JAVA_NAMING_FACTORY_URL_PKG);
  542.             prop.setValore(urlpkg);
  543.             connettore.addProperty(prop);
  544.         }
  545.         // context-java.naming.provider.url
  546.         String provurl = rs.getString("provurl");
  547.         if (provurl != null && !provurl.trim().equals("")) {
  548.             Property prop = new Property();
  549.             prop.setNome(CostantiDB.CONNETTORE_JMS_CONTEXT_JAVA_NAMING_PROVIDER_URL);
  550.             prop.setValore(provurl);
  551.             connettore.addProperty(prop);
  552.         }
  553.     }
  554.    
  555.    
  556.     protected void readPropertiesConnettoreCustom(long idConnettore, Connettore connettore, Connection connection,
  557.             IDriverBYOK driverBYOK) throws DriverRegistroServiziException {

  558.         PreparedStatement stm = null;
  559.         ResultSet rs = null;

  560.         try {
  561.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  562.             sqlQueryObject.addFromTable(CostantiDB.CONNETTORI_CUSTOM);
  563.             sqlQueryObject.addSelectField("*");
  564.             sqlQueryObject.addWhereCondition("id_connettore = ?");
  565.             String sqlQuery = sqlQueryObject.createSQLQuery();

  566.             stm = connection.prepareStatement(sqlQuery);
  567.             stm.setLong(1, idConnettore);

  568.             this.driver.logDebug("eseguo query : " + DBUtils.formatSQLString(sqlQuery, idConnettore));

  569.             rs = stm.executeQuery();

  570.             while (rs.next()) {
  571.                 processPropertiesConnettoreCustom(rs, connettore, driverBYOK);
  572.             }

  573.             rs.close();
  574.             stm.close();

  575.         } catch (SQLException sqle) {

  576.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::readPropertiesConnettoreCustom] SQLException : " + sqle.getMessage(),sqle);
  577.         }catch (Exception sqle) {

  578.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::readPropertiesConnettoreCustom] Exception : " + sqle.getMessage(),sqle);
  579.         } finally {
  580.             //Chiudo statement and resultset
  581.             JDBCUtilities.closeResources(rs, stm);
  582.         }
  583.     }
  584.     private static void processPropertiesConnettoreCustom(ResultSet rs, Connettore connettore,
  585.             IDriverBYOK driverBYOK) throws SQLException, UtilsException {
  586.         String nome = rs.getString(CostantiDB.CONNETTORI_CUSTOM_COLUMN_NAME);
  587.         String valore = rs.getString(CostantiDB.CONNETTORI_CUSTOM_COLUMN_VALUE);
  588.         String encValue = rs.getString(CostantiDB.CONNETTORI_CUSTOM_COLUMN_ENC_VALUE);

  589.         if(CostantiDB.CONNETTORE_DEBUG.equals(nome)){ // lo posso aver aggiunto prima
  590.             boolean found = false;
  591.             for (int i = 0; i < connettore.sizePropertyList(); i++) {
  592.                 if(CostantiDB.CONNETTORE_DEBUG.equals(connettore.getProperty(i).getNome())){
  593.                     // already exists
  594.                     found = true;
  595.                     break;
  596.                 }
  597.             }
  598.             if(found){
  599.                 return; // è gia stato aggiunto.
  600.             }
  601.         }
  602.        
  603.         Property prop = new Property();
  604.         prop.setNome(nome);
  605.         if(encValue!=null && StringUtils.isNotEmpty(encValue)) {
  606.             if(driverBYOK!=null) {
  607.                 prop.setValore(driverBYOK.unwrapAsString(encValue));
  608.             }
  609.             else {
  610.                 prop.setValore(encValue);
  611.             }
  612.         }
  613.         else {
  614.             prop.setValore(valore);
  615.         }
  616.         connettore.addProperty(prop);
  617.     }
  618.    
  619.     protected void readPropertiesConnettoreExtendedInfo(long idConnettore, Connettore connettore, Connection connection,
  620.             IDriverBYOK driverBYOK) throws DriverRegistroServiziException {

  621.         PreparedStatement stm = null;
  622.         ResultSet rs = null;

  623.         try {
  624.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  625.             sqlQueryObject.addFromTable(CostantiDB.CONNETTORI_CUSTOM);
  626.             sqlQueryObject.addSelectField("*");
  627.             sqlQueryObject.addWhereCondition("id_connettore = ?");
  628.             sqlQueryObject.addWhereLikeCondition(CostantiDB.CONNETTORI_CUSTOM_COLUMN_NAME, CostantiConnettori.CONNETTORE_EXTENDED_PREFIX+"%");
  629.             String sqlQuery = sqlQueryObject.createSQLQuery();

  630.             stm = connection.prepareStatement(sqlQuery);
  631.             stm.setLong(1, idConnettore);

  632.             this.driver.logDebug("eseguo query : " + DBUtils.formatSQLString(sqlQuery, idConnettore));

  633.             rs = stm.executeQuery();

  634.             while (rs.next()) {
  635.                 processPropertiesConnettoreExtendedInfo(rs, connettore,
  636.                         driverBYOK);
  637.             }

  638.             rs.close();
  639.             stm.close();

  640.         } catch (SQLException sqle) {

  641.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::readPropertiesConnettoreExtendedInfo] SQLException : " + sqle.getMessage(),sqle);
  642.         }catch (Exception sqle) {

  643.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::readPropertiesConnettoreExtendedInfo] Exception : " + sqle.getMessage(),sqle);
  644.         } finally {
  645.             //Chiudo statement and resultset
  646.             JDBCUtilities.closeResources(rs, stm);
  647.         }
  648.     }
  649.     private static void processPropertiesConnettoreExtendedInfo(ResultSet rs, Connettore connettore,
  650.             IDriverBYOK driverBYOK) throws SQLException, UtilsException {
  651.         String nome = rs.getString(CostantiDB.CONNETTORI_CUSTOM_COLUMN_NAME);
  652.         String valore = rs.getString(CostantiDB.CONNETTORI_CUSTOM_COLUMN_VALUE);
  653.         String encValue = rs.getString(CostantiDB.CONNETTORI_CUSTOM_COLUMN_ENC_VALUE);

  654.         // Le proprietà sono già state inserite in caso di connettore custom
  655.         boolean found = false;
  656.         for (int i = 0; i < connettore.sizePropertyList(); i++) {
  657.             if(nome.equals(connettore.getProperty(i).getNome())){
  658.                 // already exists
  659.                 found = true;
  660.                 break;
  661.             }
  662.         }
  663.         if(found){
  664.             return; // è gia stato aggiunto.
  665.         }
  666.        
  667.         Property prop = new Property();
  668.         prop.setNome(nome);
  669.         if(encValue!=null && StringUtils.isNotEmpty(encValue)) {
  670.             if(driverBYOK!=null) {
  671.                 prop.setValore(driverBYOK.unwrapAsString(encValue));
  672.             }
  673.             else {
  674.                 prop.setValore(encValue);
  675.             }
  676.         }
  677.         else {
  678.             prop.setValore(valore);
  679.         }
  680.         connettore.addProperty(prop);
  681.     }

  682.     protected Property[] getPropertiesConnettore(String nomeConnettore) throws DriverRegistroServiziException {
  683.         Connection con = null;
  684.         if (this.driver.atomica) {
  685.             try {
  686.                 con = this.driver.getConnectionFromDatasource("getPropertiesConnettore");

  687.             } catch (Exception e) {
  688.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getPropertiesConnettore] Exception accedendo al datasource :" + e.getMessage(),e);

  689.             }

  690.         } else
  691.             con = this.driver.globalConnection;
  692.         return getPropertiesConnettore(nomeConnettore,con);
  693.     }
  694.     protected Property[] getPropertiesConnettore(String nomeConnettore, Connection connection) throws DriverRegistroServiziException {
  695.         try {
  696.             List<Property> l = ConnettorePropertiesUtilities.getPropertiesConnettore(nomeConnettore, connection,this.driver.tipoDB);
  697.             return l!=null && !l.isEmpty() ? l.toArray(new Property[1]) : null;
  698.         } catch (CoreException e) {
  699.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getPropertiesConnettore] CoreException : " + e.getMessage(),e);
  700.         }
  701.     }
  702.    
  703.     protected void createConnettore(Connettore connettore) throws DriverRegistroServiziException {
  704.         if (connettore == null)
  705.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::createConnettore] Parametro non valido.");

  706.         Connection con = null;
  707.         boolean error = false;

  708.         if (this.driver.atomica) {
  709.             try {
  710.                 con = this.driver.getConnectionFromDatasource("createConnettore");
  711.                 con.setAutoCommit(false);
  712.             } catch (Exception e) {
  713.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::createConnettore] Exception accedendo al datasource :" + e.getMessage(),e);

  714.             }

  715.         } else
  716.             con = this.driver.globalConnection;

  717.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  718.         try {
  719.             this.driver.logDebug("CRUDConnettore type = 1");
  720.             // creo connettore
  721.             DriverRegistroServiziDB_connettoriLIB.CRUDConnettore(1, connettore, con, this.driver.getDriverWrapBYOK());

  722.         } catch (Exception qe) {
  723.             error = true;
  724.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::createConnettore] Errore durante la creazione del connettore : " + qe.getMessage(),qe);
  725.         } finally {

  726.             this.driver.closeConnection(error,con);
  727.         }
  728.     }

  729.     protected void updateConnettore(Connettore connettore) throws DriverRegistroServiziException {
  730.         if (connettore == null)
  731.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updateConnettore] Parametro non valido.");

  732.         Connection con = null;
  733.         boolean error = false;

  734.         if (this.driver.atomica) {
  735.             try {
  736.                 con = this.driver.getConnectionFromDatasource("updateConnettore");
  737.                 con.setAutoCommit(false);
  738.             } catch (Exception e) {
  739.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updateConnettore] Exception accedendo al datasource :" + e.getMessage(),e);

  740.             }

  741.         } else
  742.             con = this.driver.globalConnection;

  743.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  744.         try {
  745.             this.driver.logDebug("CRUDConnettore type = 2");
  746.             // update connettore
  747.             DriverRegistroServiziDB_connettoriLIB.CRUDConnettore(2, connettore, con, this.driver.getDriverWrapBYOK());

  748.         } catch (Exception qe) {
  749.             error = true;
  750.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updateConnettore] Errore durante l'aggiornamento del connettore : " + qe.getMessage(),qe);
  751.         } finally {

  752.             this.driver.closeConnection(error,con);
  753.         }
  754.     }

  755.     protected void deleteConnettore(Connettore connettore) throws DriverRegistroServiziException {
  756.         if (connettore == null)
  757.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::deleteConnettore] Parametro non valido.");

  758.         Connection con = null;
  759.         boolean error = false;

  760.         if (this.driver.atomica) {
  761.             try {
  762.                 con = this.driver.getConnectionFromDatasource("deleteConnettore");
  763.                 con.setAutoCommit(false);
  764.             } catch (Exception e) {
  765.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::deleteConnettore] Exception accedendo al datasource :" + e.getMessage(),e);

  766.             }

  767.         } else
  768.             con = this.driver.globalConnection;

  769.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  770.         try {
  771.             this.driver.logDebug("CRUDConnettore type = 3");
  772.             // delete connettore
  773.             DriverRegistroServiziDB_connettoriLIB.CRUDConnettore(3, connettore, con, this.driver.getDriverWrapBYOK());

  774.         } catch (Exception qe) {
  775.             error = true;
  776.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::deleteConnettore] Errore durante la rimozione del connettore : " + qe.getMessage(),qe);
  777.         } finally {

  778.             this.driver.closeConnection(error,con);
  779.         }
  780.     }
  781. }