SoggettiConfig.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.monitor.rs.server.config;

  21. import java.sql.Connection;
  22. import java.util.List;

  23. import org.apache.commons.lang.StringUtils;
  24. import org.openspcoop2.core.id.IDSoggetto;
  25. import org.openspcoop2.core.registry.Soggetto;
  26. import org.openspcoop2.core.registry.constants.PddTipologia;
  27. import org.openspcoop2.core.registry.driver.DriverRegistroServiziNotFound;
  28. import org.openspcoop2.core.registry.driver.FiltroRicerca;
  29. import org.openspcoop2.core.registry.driver.db.DriverRegistroServiziDB;
  30. import org.openspcoop2.utils.Semaphore;
  31. import org.openspcoop2.utils.SemaphoreLock;
  32. import org.openspcoop2.web.monitor.core.core.Utility;
  33. import org.slf4j.Logger;


  34. /**
  35.  * SoggettiConfig
  36.  *
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  *
  40.  */
  41. public class SoggettiConfig {

  42.     private static Semaphore semaphore = new Semaphore("ApiSoggettiConfig");
  43.    
  44.     public static boolean existsIdentificativoPorta(String tipoSoggetto, String nomeSoggetto) {
  45.        
  46.         if(Utility.existsIdentificativoPorta(tipoSoggetto, nomeSoggetto)) {
  47.             return true;
  48.         }
  49.        
  50.         // Verifico se il soggetto non fosse stato aggiunto in seguito al restart dell'API
  51.        
  52.         DBManager dbManager = DBManager.getInstance();
  53.         Connection connection = null;
  54.         Logger logSql = LoggerProperties.getLoggerDAO();
  55.         try {
  56.             connection = dbManager.getConnectionConfig();
  57.            
  58.             DriverRegistroServiziDB driverDB = new DriverRegistroServiziDB(connection, logSql, DatasourceProperties.getInstance().getConfigTipoDatabase());
  59.            
  60.             IDSoggetto idSoggetto = new IDSoggetto(tipoSoggetto, nomeSoggetto);
  61.            
  62.             if(driverDB.existsSoggetto(idSoggetto)==false) {
  63.                 return false;
  64.             }
  65.            
  66.             Soggetto soggetto = driverDB.getSoggetto(idSoggetto);
  67.             if(soggetto.getPortaDominio()==null || StringUtils.isEmpty(soggetto.getPortaDominio())) {
  68.                 return false; // esterno
  69.             }
  70.            
  71.             FiltroRicerca filtroRicercaPdd = new FiltroRicerca();
  72.             filtroRicercaPdd.setTipo(PddTipologia.OPERATIVO.toString());
  73.             List<String> idsPdd = null;
  74.             try {
  75.                 idsPdd = driverDB.getAllIdPorteDominio(filtroRicercaPdd);
  76.             }catch(DriverRegistroServiziNotFound notFound) {        
  77.             }
  78.             if(idsPdd==null || idsPdd.isEmpty() || !idsPdd.contains(soggetto.getPortaDominio())) {
  79.                 return false;
  80.             }
  81.            
  82.             SemaphoreLock lock = semaphore.acquire("existsIdentificativoPorta");
  83.             try {
  84.                 // controllo se non fosse gia' stato riaggiunto da un altro thread
  85.                
  86.                 if(Utility.existsIdentificativoPorta(tipoSoggetto, nomeSoggetto)) {
  87.                     return true;
  88.                 }
  89.                
  90.                 Utility.putIdentificativoPorta(tipoSoggetto, nomeSoggetto, soggetto.getIdentificativoPorta());
  91.                
  92.                 return true;
  93.             }finally {
  94.                 semaphore.release(lock, "existsIdentificativoPorta");
  95.             }
  96.            
  97.         }
  98.         catch(DriverRegistroServiziNotFound notFound) {
  99.             logSql.debug("Soggetto "+tipoSoggetto+"/"+nomeSoggetto+" non esistente: "+notFound.getMessage(),notFound);
  100.             return false;
  101.         }
  102.         catch(Exception e) {
  103.             logSql.error("Errore durante il controllo di esistenza del soggetto "+tipoSoggetto+"/"+nomeSoggetto+": "+e.getMessage(),e);
  104.             return false;
  105.         }
  106.         finally {
  107.             dbManager.releaseConnectionConfig(connection);
  108.         }
  109.     }
  110.    
  111. }