BasicProducer.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.protocol.basic;

  21. import java.sql.Connection;
  22. import java.sql.DriverManager;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.HashMap;
  26. import java.util.Properties;

  27. import javax.sql.DataSource;

  28. import org.openspcoop2.core.commons.CoreException;
  29. import org.openspcoop2.core.commons.IMonitoraggioRisorsa;
  30. import org.openspcoop2.core.config.OpenspcoopAppender;
  31. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  32. import org.openspcoop2.protocol.sdk.ProtocolException;
  33. import org.openspcoop2.protocol.sdk.config.IProtocolConfiguration;
  34. import org.openspcoop2.protocol.sdk.diagnostica.MsgDiagnosticoException;
  35. import org.openspcoop2.utils.TipiDatabase;
  36. import org.openspcoop2.utils.Utilities;
  37. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  38. import org.openspcoop2.utils.resources.GestoreJNDI;


  39. /**
  40.  * Contiene l'implementazione di un appender personalizzato,
  41.  * per la registrazione su database.
  42.  *
  43.  * @author Poli Andrea (apoli@link.it)
  44.  * @author $Author$
  45.  * @version $Rev$, $Date$
  46.  */
  47. public class BasicProducer extends BasicComponentFactory implements IMonitoraggioRisorsa {
  48.    
  49.     /** Properties */
  50.     protected Properties appenderProperties;
  51.        
  52.     /** DataSource dove attingere connessioni */
  53.     protected DataSource ds = null;
  54.     protected String datasource = null;
  55.    
  56.     /** Connessione diretta via JDBC */
  57.     protected String connectionViaJDBCUrl = null;
  58.     protected String connectionViaJDBCDriverJDBC = null;
  59.     protected String connectionViaJDBCUsername = null;
  60.     protected String connectionViaJDBCPassword = null;

  61.     /** SingleConnection */
  62.     protected boolean singleConnection = false;
  63.     protected HashMap<BasicProducerType, Connection> singleConnectionConnectionMap = new HashMap<>();
  64.     protected HashMap<BasicProducerType, String> singleConnectionSourceMap = new HashMap<>();
  65.    
  66.     /** TipoDatabase */
  67.     protected String tipoDatabase = null;

  68.     /** OpenSPCoop Connection */
  69.     protected boolean openspcoopConnection = false;
  70.    
  71.     /** Emit debug info */
  72.     protected boolean debug = false;

  73.     /** forceIndex */
  74.     protected boolean forceIndex = false;
  75.     public void setForceIndex(boolean forceIndex) {
  76.         this.forceIndex = forceIndex;
  77.     }
  78.    
  79.     /** IProtocolConfiguration */
  80.     protected IProtocolConfiguration protocolConfiguration;
  81.    
  82.     /** ISAlive */
  83.     protected boolean isAlive = true;
  84.    
  85.     private BasicProducerType producerType;
  86.    
  87.     public BasicProducer(IProtocolFactory<?> factory, BasicProducerType producerType) throws ProtocolException{
  88.         super(factory);
  89.         this.producerType = producerType;
  90.     }
  91.    
  92.    

  93.     protected synchronized void initConnection(DataSource dsA,String dataSourceS)throws ProtocolException{
  94.         Connection singleConnectionConnection = this.singleConnectionConnectionMap.get(this.producerType);
  95.         if(singleConnectionConnection==null){
  96.             try{
  97.                 String singleConnectionSource = dataSourceS;
  98.                 singleConnectionConnection = dsA.getConnection();
  99.                 this.singleConnectionConnectionMap.put(this.producerType, singleConnectionConnection);
  100.                 this.singleConnectionSourceMap.put(this.producerType, singleConnectionSource);
  101.             }catch(Exception e){
  102.                  throw new ProtocolException("Inizializzazione single connection (via datasource) non riuscita",e);
  103.             }
  104.         }
  105.     }
  106.     protected synchronized void initConnection(String jdbcUrl,String jdbcDriver,String username, String password)throws ProtocolException{
  107.         Connection singleConnectionConnection = this.singleConnectionConnectionMap.get(this.producerType);
  108.         if(singleConnectionConnection==null){
  109.             try{
  110.                 String singleConnectionSource = "url:"+jdbcUrl+" driver:"+jdbcDriver+" username:"+username+" password:"+password;
  111.                 if(username==null){
  112.                     singleConnectionConnection = DriverManager.getConnection(jdbcUrl);
  113.                 }
  114.                 else{
  115.                     singleConnectionConnection = DriverManager.getConnection(jdbcUrl,username,password);
  116.                 }
  117.                 this.singleConnectionConnectionMap.put(this.producerType, singleConnectionConnection);
  118.                 this.singleConnectionSourceMap.put(this.producerType, singleConnectionSource);
  119.             }catch(Exception e){
  120.                  throw new ProtocolException("Inizializzazione single connection (via jdbc connection) non riuscita",e);
  121.             }
  122.         }
  123.     }
  124.     protected Connection getConnectionViaJDBC() throws SQLException{
  125.         if(this.connectionViaJDBCUsername==null){
  126.             return DriverManager.getConnection(this.connectionViaJDBCUrl);
  127.         }
  128.         else{
  129.             return DriverManager.getConnection(this.connectionViaJDBCUrl,
  130.                     this.connectionViaJDBCUsername,this.connectionViaJDBCPassword);
  131.         }
  132.     }
  133.    
  134.     protected BasicConnectionResult getConnection(Connection conOpenSPCoopPdD,String methodName) throws ProtocolException{
  135.         Connection con = null;
  136.         BasicConnectionResult cr = new BasicConnectionResult();
  137.         cr.setReleaseConnection(false);
  138.         if(this.debug){
  139.             this.logInfo("@@ ["+methodName+"] SINGLE CONNECTION["+this.singleConnection+"] OPEN["+this.openspcoopConnection+"]");
  140.         }
  141.         if(!this.singleConnection){
  142.             if(this.openspcoopConnection && conOpenSPCoopPdD!=null){
  143.                 /**System.out.println("["+methodName+"]@GET_CONNECTION@ USE CONNECTION OPENSPCOOP");*/
  144.                 if(this.debug){
  145.                     this.logInfo("@@ ["+methodName+"] GET_CONNECTION, USE CONNECTION OPENSPCOOP");
  146.                 }
  147.                 con = conOpenSPCoopPdD;
  148.             }else{
  149.                 if(this.ds!=null){
  150.                     try{
  151.                         con = this.ds.getConnection();
  152.                         /**if(con == null)
  153.                             throw new Exception("Connessione non fornita");*/
  154.                         cr.setReleaseConnection(true);
  155.                         /**System.out.println("["+methodName+"]@GET_CONNECTION@ USE CONNECTION FROM DATASOURCE");*/
  156.                         if(this.debug){
  157.                             this.logInfo("@@ ["+methodName+"] GET_CONNECTION, USE CONNECTION FROM DATASOURCE");
  158.                         }
  159.                     }catch(Exception e){
  160.                         throw new ProtocolException("Errore durante il recupero di una connessione dal datasource ["+this.datasource+"]: "+e.getMessage());
  161.                     }
  162.                 }
  163.                 else{
  164.                     try{
  165.                         con = this.getConnectionViaJDBC();
  166.                         if(con == null)
  167.                             throw new ProtocolException("Connessione non fornita");
  168.                         cr.setReleaseConnection(true);
  169.                         /**System.out.println("["+methodName+"]@GET_CONNECTION@ USE CONNECTION VIA JDBC");*/
  170.                         if(this.debug){
  171.                             this.logInfo("@@ ["+methodName+"] GET_CONNECTION, USE CONNECTION VIA JDBC");
  172.                         }
  173.                     }catch(Exception e){
  174.                         throw new ProtocolException("Errore durante il recupero di una connessione via jdbc url["+this.connectionViaJDBCUrl+"] driver["+
  175.                                 this.connectionViaJDBCDriverJDBC+"] username["+this.connectionViaJDBCUsername+"] password["+this.connectionViaJDBCPassword
  176.                                 +"]: "+e.getMessage());
  177.                     }
  178.                 }
  179.             }
  180.         }else{
  181.             con = this.singleConnectionConnectionMap.get(this.producerType);
  182.             if(con == null){
  183.                 throw new ProtocolException("Connessione (singleConnection enabled) non fornita dalla sorgente ["+this.singleConnectionSourceMap.get(this.producerType)+"]");
  184.             }
  185.             /**System.out.println("["+methodName+"]@GET_CONNECTION@ SINGLE CONNECTION");*/
  186.             if(this.debug){
  187.                 this.logInfo("@@ ["+methodName+"] GET_CONNECTION, SINGLE CONNECTION");
  188.             }
  189.         }
  190.         cr.setConnection(con);
  191.         return cr;
  192.     }
  193.     protected void releaseConnection(BasicConnectionResult connectionResult,String methodName) throws SQLException{
  194.         if(this.debug){
  195.             this.logInfo("@@ ["+methodName+"] RELEASE_CONNECTION ["+connectionResult.isReleaseConnection()+"]?");
  196.         }
  197.         if(connectionResult.isReleaseConnection()){
  198.             /**System.out.println("["+methodName+"]@RELEASE_CONNECTION@");*/
  199.             if(this.debug){
  200.                 this.logInfo("@@ ["+methodName+"] RELEASE_CONNECTION effettuato");
  201.             }
  202.             JDBCUtilities.closeConnection(BasicComponentFactory.getCheckLogger(), connectionResult.getConnection(), BasicComponentFactory.isCheckAutocommit(), BasicComponentFactory.isCheckIsClosed());
  203.         }
  204.     }
  205.    

  206.    
  207.    

  208.    
  209.     /**
  210.      * Inizializza l'engine di un appender per la registrazione
  211.      *
  212.      * @param appenderProperties Proprieta' dell'appender
  213.      * @throws MsgDiagnosticoException
  214.      */
  215.     public void initializeAppender(OpenspcoopAppender appenderProperties, boolean tipoDatabaseRequired) throws ProtocolException{
  216.        
  217.         try{
  218.        
  219.             // Lettura modalita' di gestione
  220.             this.appenderProperties = new Properties();
  221.             if(appenderProperties.sizePropertyList()>0){
  222.                
  223.                 for(int i=0; i<appenderProperties.sizePropertyList(); i++){
  224.                     this.appenderProperties.put(appenderProperties.getProperty(i).getNome(),
  225.                             appenderProperties.getProperty(i).getValore());
  226.                 }
  227.                
  228.                 this.datasource = this.appenderProperties.getProperty("datasource");
  229.                 if(this.datasource==null){
  230.                     this.connectionViaJDBCUrl = this.appenderProperties.getProperty("connectionUrl");
  231.                     if(this.connectionViaJDBCUrl==null){
  232.                         String tmp = this.appenderProperties.getProperty("checkProperties");
  233.                         if(tmp == null || Boolean.valueOf(tmp.trim())) {
  234.                             throw new ProtocolException("Proprietà 'datasource' e 'connectionUrl' non definite (almeno una delle due è obbligatoria)");
  235.                         }
  236.                         else {
  237.                             this.isAlive = false;
  238.                         }
  239.                     }
  240.                 }
  241.                
  242.                 this.tipoDatabase=this.appenderProperties.getProperty("tipoDatabase");
  243.                 // non obbligatorio in msg diagnostico per retrocompatibilità
  244.                 if(this.tipoDatabase==null && tipoDatabaseRequired){
  245.                     throw new ProtocolException("Proprieta' 'tipoDatabase' non definita");
  246.                 }
  247.                 if(this.tipoDatabase!=null &&
  248.                     !TipiDatabase.isAMember(this.tipoDatabase)){
  249.                     throw new ProtocolException("Proprieta' 'tipoDatabase' presenta un tipo ["+this.tipoDatabase+"] non supportato");
  250.                 }
  251.                
  252.                 String singleConnectionString = this.appenderProperties.getProperty("singleConnection");
  253.                 if(singleConnectionString!=null){
  254.                     singleConnectionString = singleConnectionString.trim();
  255.                     if("true".equals(singleConnectionString)){
  256.                         this.singleConnection = true;
  257.                     }
  258.                 }
  259.                
  260.                 String openspcoopConnectionString = this.appenderProperties.getProperty("usePdDConnection");
  261.                 if(openspcoopConnectionString!=null){
  262.                     openspcoopConnectionString = openspcoopConnectionString.trim();
  263.                     if("true".equals(openspcoopConnectionString)){
  264.                         this.openspcoopConnection = true;
  265.                     }
  266.                 }
  267.                
  268.             }else{
  269.                 throw new ProtocolException("Proprietà 'datasource' e 'connectionUrl' non definite (almeno una delle due è obbligatoria)");
  270.             }
  271.            
  272.             // Datasource
  273.             if(this.datasource!=null){
  274.                
  275.                 java.util.Properties ctx = Utilities.readProperties("context-", this.appenderProperties);
  276.            
  277.                 GestoreJNDI jndi = new GestoreJNDI(ctx);
  278.                 if(this.singleConnection){
  279.                     if(this.singleConnectionConnectionMap.get(this.producerType)==null){
  280.                         DataSource dsL = (DataSource) jndi.lookup(this.datasource);
  281.                         initConnection(dsL,this.datasource);
  282.                     }
  283.                 }else{
  284.                     this.ds = (DataSource) jndi.lookup(this.datasource);
  285.                 }
  286.                
  287.             }
  288.            
  289.            
  290.             // connectionViaJDBC
  291.             if(this.connectionViaJDBCUrl!=null){
  292.                
  293.                 this.connectionViaJDBCDriverJDBC = this.appenderProperties.getProperty("connectionDriver");
  294.                 if(this.connectionViaJDBCDriverJDBC==null){
  295.                     throw new ProtocolException("Proprietà 'connectionDriver' non definita (obbligatoria nella modalita' 'connection via jdbc')");
  296.                 }
  297.                
  298.                 this.connectionViaJDBCUsername = this.appenderProperties.getProperty("connectionUsername");
  299.                 if(this.connectionViaJDBCUsername!=null){
  300.                     this.connectionViaJDBCPassword = this.appenderProperties.getProperty("connectionPassword");
  301.                     if(this.connectionViaJDBCPassword==null){
  302.                         throw new ProtocolException("Proprietà 'connectionPassword' non definita (obbligatoria nella modalita' 'connection via jdbc' se viene definita la proprieta' 'connectionUsername')");
  303.                     }
  304.                 }
  305.                
  306.                 Class.forName(this.connectionViaJDBCDriverJDBC);
  307.                
  308.                 if(this.singleConnection &&
  309.                     this.singleConnectionConnectionMap.get(this.producerType)==null){
  310.                     initConnection(this.connectionViaJDBCUrl,this.connectionViaJDBCDriverJDBC,this.connectionViaJDBCUsername,this.connectionViaJDBCPassword);
  311.                 }
  312.             }
  313.            
  314.             // debug info
  315.             String debugS = this.appenderProperties.getProperty("debug");
  316.             if(debugS!=null){
  317.                 debugS = debugS.trim();
  318.                 if("true".equals(debugS)){
  319.                     this.debug = true;
  320.                 }
  321.             }
  322.            
  323.            
  324.             // Protocol Configuration
  325.             this.protocolConfiguration = this.protocolFactory.createProtocolConfiguration();
  326.            
  327.         }catch(Exception e){
  328.             throw new ProtocolException("Errore durante l'inizializzazione dell'appender: "+e.getMessage(),e);
  329.         }
  330.     }

  331.     protected String getSQLStringValue(String value){
  332.         if(value!=null && (!"".equals(value))){
  333.             return value;
  334.         }
  335.         else{
  336.             return null;
  337.         }
  338.     }
  339.    
  340.    
  341.    
  342.     public String getTipoDatabase() {
  343.         return this.tipoDatabase;
  344.     }

  345.     public void setTipoDatabase(String tipoDatabase) {
  346.         this.tipoDatabase = tipoDatabase;
  347.     }
  348.    

  349.     /**
  350.      * Metodo che verica la connessione ad una risorsa.
  351.      * Se la connessione non e' presente, viene lanciata una eccezione che contiene il motivo della mancata connessione
  352.      *
  353.      * @throws DriverException eccezione che contiene il motivo della mancata connessione
  354.      */
  355.     @Override
  356.     public void isAlive() throws CoreException{
  357.         if(!this.isAlive) {
  358.             return;
  359.         }
  360.         // Verifico la connessione
  361.         Connection con = null;
  362.         Statement stmtTest = null;
  363.         BasicConnectionResult cr = null;
  364.         try {
  365.             // Connessione al DB
  366.             cr = this.getConnection(null,"isAlive");
  367.             con = cr.getConnection();
  368.            
  369.             // test:
  370.             stmtTest = con.createStatement();
  371.             stmtTest.execute("SELECT * from db_info");
  372.         } catch (Exception e) {
  373.             throw new CoreException("Connessione al database '"+this.producerType+"' non disponibile: "+e.getMessage(),e);

  374.         }finally{
  375.             try{
  376.                 if(stmtTest!=null)
  377.                     stmtTest.close();
  378.             }catch(Exception e){
  379.                 // close
  380.             }
  381.             try{
  382.                 this.releaseConnection(cr, "isAlive");
  383.             }catch(Exception e){
  384.                 // close
  385.             }
  386.         }
  387.     }

  388. }