GestorePdDInitThread.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.web.ctrlstat.gestori;

  21. import java.sql.Connection;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.openspcoop2.core.registry.constants.PddTipologia;
  26. import org.openspcoop2.pdd.config.OpenSPCoop2ConfigurationException;
  27. import org.openspcoop2.utils.Utilities;
  28. import org.openspcoop2.web.ctrlstat.config.ConsoleProperties;
  29. import org.openspcoop2.web.ctrlstat.core.ControlStationLogger;
  30. import org.openspcoop2.web.ctrlstat.core.DBManager;
  31. import org.openspcoop2.web.ctrlstat.dao.PdDControlStation;
  32. import org.openspcoop2.web.ctrlstat.driver.DriverControlStationNotFound;
  33. import org.openspcoop2.web.ctrlstat.servlet.pdd.PddCore;
  34. import org.slf4j.Logger;

  35. /**
  36.  * GestorePdDInitThread
  37.  *
  38.  * @author Andrea Poli (apoli@link.it)
  39.  * @author Stefano Corallo (corallo@link.it)
  40.  * @author Sandra Giangrandi (sandra@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  *
  44.  */
  45. public class GestorePdDInitThread extends Thread {

  46.     private ConsoleProperties consoleProperties;
  47.    
  48.     /** Logger utilizzato per debug. */
  49.     private static Logger log = null;
  50.     /** run */
  51.     // public static boolean stop = false;
  52.     private static Map<String, IGestore> gestoriPdd = new HashMap<String, IGestore>();

  53.     private boolean singlePdD = false;
  54.     private boolean enginePDD = false;
  55.    
  56.     /** Costruttore
  57.      * @throws OpenSPCoop2ConfigurationException */
  58.     public GestorePdDInitThread() throws OpenSPCoop2ConfigurationException {
  59.        
  60.         GestorePdDInitThread.log = ControlStationLogger.getGestorePddLogger();

  61.         this.consoleProperties = ConsoleProperties.getInstance();
  62.        
  63.         // start();
  64.     }

  65.     /**
  66.      * Metodo che fa partire il Thread.
  67.      *
  68.      */
  69.     @Override
  70.     public void run() {

  71.         try {
  72.             this.initGestore();

  73.             if (this.singlePdD) {
  74.                 log.warn("GestorePdDInitThread non avviato: govwayConsole avviata in singlePdD mode.");
  75.                 return;
  76.             }

  77.         } catch (GestoreNonAttivoException e) {
  78.             log.warn("Inizializzazione GestorePdDInitThread non effettuata : " + e.getMessage());
  79.             return;
  80.         } catch (Exception e) {
  81.             log.error("Inizializzazione Gestore PdD Init Thread Fallita : " + e.getMessage(), e);
  82.             return;
  83.         }
  84.        
  85.         try {

  86.             // Controllo se dbmanager inizializzato
  87.             // Il DBManager viene inizializzato nell'InitListener
  88.             if (!DBManager.isInitialized()) {
  89.                 GestorePdDInitThread.log.info("Inizializzazione di " + this.getClass().getSimpleName() + " non riuscito perche' DBManager non INIZIALIZZATO");
  90.                 GestorePdDInitThread.log.info("Gestore Pdd non avviato!");
  91.                 return;
  92.             }

  93.             DBManager dbm = DBManager.getInstance();
  94.             Connection con = dbm.getConnection();

  95.             PddCore core = new PddCore();
  96.             if(core.isSinglePdD()==false){
  97.                 List<String> pddList = core.getAllIdPorteDominio(null);
  98.                 if(pddList!=null && pddList.size()>0)
  99.                     GestorePdDInitThread.log.info("Trovate "+pddList.size()+" pdd su cui avviare il thread di gestione");
  100.                 else
  101.                     GestorePdDInitThread.log.info("Non sono state trovate pdd da gestire");
  102.                 if(pddList!=null) {
  103.                     for (int i = 0; i < pddList.size(); i++) {
  104.                         PdDControlStation pdd = null;
  105.                         try{
  106.                             pdd = core.getPdDControlStation(pddList.get(i));
  107.                         }catch(DriverControlStationNotFound dNot){
  108.                             GestorePdDInitThread.log.error("Errore durante la lettura dei dati della pdd ["+pddList.get(i)+"]: "+dNot.getMessage());
  109.                         }
  110.                         if(pdd!=null) {
  111.                             addGestore(pdd);
  112.                         }
  113.                     }
  114.                 }
  115.             }

  116.             // Chiudo la connessione al DB
  117.             dbm.releaseConnection(con);
  118.         } catch (org.openspcoop2.web.ctrlstat.core.ControlStationCoreException csce) {
  119.             GestorePdDInitThread.log.error("ControlStationCoreException: " + csce.getMessage());
  120.         } catch (org.openspcoop2.web.ctrlstat.driver.DriverControlStationException dcse) {
  121.             GestorePdDInitThread.log.error("DriverControlStationException: " + dcse.getMessage());
  122.         } catch (Exception dcse) {
  123.             GestorePdDInitThread.log.error("DriverControlStationException: " + dcse.getMessage());
  124.         }
  125.     }

  126.     /**
  127.      * Aggiunge una porta di dominio alla lista di porte di dominio e avvia il
  128.      * thread di gestione della PdD
  129.      *
  130.      * @param nomeCoda
  131.      * @throws OpenSPCoop2ConfigurationException
  132.      */
  133.     public static void startPdD(String nomeCoda) throws OpenSPCoop2ConfigurationException {
  134.         GestorePdDThread pdd = new GestorePdDThread(nomeCoda);
  135.         GestorePdDInitThread.gestoriPdd.put(nomeCoda,pdd);
  136.         new Thread(pdd).start();
  137.     }

  138.     public void stopGestore() {
  139.         // Stoppo tutti i thread gestori delle pdd
  140.         if(gestoriPdd!=null && !gestoriPdd.isEmpty()) {
  141.             for (String pdd : gestoriPdd.keySet()) {
  142.                 deleteGestore(pdd);
  143.             }
  144.         }

  145.     }

  146.    
  147.     public void initGestore() throws Exception {

  148.         this.enginePDD = this.consoleProperties.isGestioneCentralizzataSincronizzazionePdd();
  149.         this.singlePdD = this.consoleProperties.isSinglePdD();

  150.         if (this.enginePDD == false) {
  151.             //this.log.info("Motore di sincronizzazione verso le Porte di Dominio non attivo.");
  152.             throw new GestoreNonAttivoException("Motore di sincronizzazione verso le Porte di Dominio non attivo.");
  153.         }

  154.     }
  155.    
  156.    
  157.     public static void addGestore(PdDControlStation pdd) throws Exception {
  158.         if(gestoriPdd.containsKey(pdd.getNome())==false){
  159.             try{
  160.                 String tipoPdd = pdd.getTipo();
  161.                 if (tipoPdd != null && PddTipologia.OPERATIVO.toString().equals(tipoPdd)) {
  162.                     GestorePdDInitThread.log.info("Avvio thread di gestione per la porta di dominio ["+pdd.getNome()+"] ...");
  163.                     String nomeCoda = pdd.getNome();
  164.                     GestorePdDInitThread.startPdD(nomeCoda);
  165.                     GestorePdDInitThread.log.info("Avviato thread di gestione per la porta di dominio ["+pdd.getNome()+"]");
  166.                 }else{
  167.                     GestorePdDInitThread.log.info("Thread di gestione per la porta di dominio ["+pdd.getNome()+"] non avviato poiche' la porta di dominio non possiede tipo "+PddTipologia.OPERATIVO.toString()+" (tipo:"+pdd.getTipo()+")");
  168.                 }
  169.             }catch(Exception e){
  170.                 GestorePdDInitThread.log.error("Thread di gestione per la porta di dominio ["+pdd.getNome()+"] non avviato: "+e.getMessage());
  171.             }
  172.         }
  173.         else{
  174.             GestorePdDInitThread.log.debug("Thread di gestione per la porta di dominio ["+pdd.getNome()+"] gia' in esecuzione");
  175.         }
  176.     }
  177.    
  178.     public static void deleteGestore(String pdd)  {
  179.         if(gestoriPdd.containsKey(pdd)==false){
  180.             GestorePdDInitThread.log.debug("Thread di gestione per la porta di dominio ["+pdd+"] non risulta in esecuzione");
  181.         }
  182.         else{
  183.             GestorePdDInitThread.log.debug("Fermo il thread di gestione per la porta di dominio ["+pdd+"] ...");
  184.             GestorePdDThread gestore = (GestorePdDThread) gestoriPdd.get(pdd);
  185.             gestore.stopGestore();
  186.             int timeout = 60;
  187.             for (int i = 0; i < timeout; i++) {
  188.                 if(gestore.isRunning()){
  189.                     Utilities.sleep(1000);
  190.                 }
  191.                 else{
  192.                     break;
  193.                 }
  194.             }
  195.             GestorePdDInitThread.log.debug("Thread di gestione per la porta di dominio ["+pdd+"] terminato");
  196.             gestoriPdd.remove(pdd);
  197.         }
  198.     }
  199. }