MySQLThreshold.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.pdd.core.threshold;

  21. import java.sql.Connection;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.Properties;


  26. import org.openspcoop2.pdd.config.DBManager;
  27. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  28. import org.openspcoop2.pdd.config.Resource;


  29. //import org.openspcoop.pdd.logger.MsgDiagnostico;

  30. /**
  31.  * Implementazione che definisce un meccanismo di Soglia sullo spazio libero rimasto
  32.  * nelle risorse utilizzate dalla PdD, per un database MySQL.
  33.  *  
  34.  * @author Poli Andrea (apoli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public class MySQLThreshold implements IThreshold {

  39.     public static OpenSPCoop2Properties properties = OpenSPCoop2Properties.getInstance();
  40.     public static String ID_MODULO = "MySQLThreshold";
  41.     private static final String Query="SHOW TABLE STATUS LIKE 'MESSAGGI'";
  42.    
  43.     /**
  44.      * Controlla lo spazio libero sul tablespace di mysql
  45.      *  
  46.      * @param parametri Parametri
  47.      * @return true se vi e' ancora spazio libero che rispetti la soglia
  48.      */
  49.     @Override
  50.     public boolean check(Properties parametri) throws ThresholdException{
  51.         DBManager dbManager = DBManager.getInstance();
  52.         Resource resource = null;
  53.         long Threshold,FreeTableSpace;
  54.         boolean result=false;
  55.         Statement s=null;
  56.         ResultSet rs=null;
  57.    
  58.         //MsgDiagnostico msgDiag = new MsgDiagnostico(ID_MODULO);
  59.         try{
  60.             try{
  61.                 resource = dbManager.getResource(MySQLThreshold.properties.getIdentitaPortaDefaultWithoutProtocol(), MySQLThreshold.ID_MODULO,null);
  62.             }catch(Exception e){
  63.                 throw new Exception("Impossibile ottenere una Risorsa dal DBManager",e);
  64.             }
  65.             if(resource==null)
  66.                 throw new Exception("Risorsa is null");
  67.             if(resource.getResource() == null)
  68.                 throw new Exception("Connessione is null");
  69.             Connection connection = (Connection) resource.getResource();
  70.            
  71.             // controllo validita parametro
  72.             String valore = parametri.getProperty("valore");
  73.             if(valore==null){
  74.                 throw new ThresholdException("Parametro ["+valore+"] non presente");
  75.             }
  76.             else{
  77.                 valore = valore.trim();
  78.             }
  79.             Threshold=Long.parseLong(valore);
  80.             if(Threshold<0L)
  81.                 throw new Exception("Valore di soglia negativo");
  82.            
  83.             //Interrogazione del database
  84.             s=connection.createStatement();
  85.             if(!s.execute(MySQLThreshold.Query))
  86.                 throw new Exception("Impossibile verficare lo spazio rimanente sul tablespace");
  87.            
  88.             rs=s.getResultSet();
  89.            
  90.             if(rs==null)
  91.                 throw new Exception("Nessun risultato disponibile per la verifica di soglia");
  92.             if(!rs.next())
  93.                 throw new Exception("Nessun risultato disponibile per la verifica di soglia");
  94.            
  95.             String Comment = rs.getString("Comment");
  96.             if(Comment==null)
  97.                 throw new Exception("La quantita' di spazio disponibile sul DB e' NULL");

  98.             // Parse della stringa ricevuta da MySql
  99.             // Il contentuo di questa colonna tipicamente e'
  100.             //  InnoDB free: <spazio rimanente nel tablespace> kB
  101.             // vanno eliminati i primi 13 caratteri e gli ultimi 3
  102.        
  103.             FreeTableSpace=Long.parseLong(Comment.substring(13, Comment.length()-3 ));
  104.            
  105.             result=(FreeTableSpace>Threshold);
  106.            
  107.         }catch(Exception e){
  108.             throw new ThresholdException("MySQLThreshold error: "+e.getMessage(),e);
  109.         }finally{
  110.             try {
  111.                 if(rs!=null)
  112.                     rs.close();
  113.             }catch(SQLException ex){
  114.                 // ignore
  115.             }
  116.             try {
  117.                 if(s!=null)
  118.                     s.close();
  119.             }catch(SQLException ex){
  120.                 // ignore
  121.             }
  122.             rs=null;s=null;
  123.             dbManager.releaseResource(MySQLThreshold.properties.getIdentitaPortaDefaultWithoutProtocol(), MySQLThreshold.ID_MODULO, resource);
  124.         }
  125.         //msgDiag.highDebug("Spazio libero sul TableSpace: "+FreeTableSpace+" Spazio entro la soglia: "+result);
  126.         return result;
  127.     }
  128.    
  129. }