ConfigurazionePdD_plugins.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.config;

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

  23. import org.openspcoop2.core.commons.Filtri;
  24. import org.openspcoop2.core.commons.dao.DAOFactory;
  25. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  26. import org.openspcoop2.core.config.driver.DriverConfigurazioneNotFound;
  27. import org.openspcoop2.core.config.driver.db.DriverConfigurazioneDB;
  28. import org.openspcoop2.generic_project.exception.NotFoundException;
  29. import org.openspcoop2.generic_project.expression.IExpression;
  30. import org.openspcoop2.generic_project.expression.IPaginatedExpression;
  31. import org.openspcoop2.generic_project.utils.ServiceManagerProperties;
  32. import org.openspcoop2.core.plugins.IdPlugin;
  33. import org.openspcoop2.core.plugins.Plugin;
  34. import org.openspcoop2.core.plugins.PluginProprietaCompatibilita;
  35. import org.openspcoop2.core.plugins.dao.IPluginServiceSearch;
  36. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  37. import org.openspcoop2.utils.NameValue;

  38. /**    
  39.  * ConfigurazionePdD_plugins
  40.  *
  41.  * @author Poli Andrea (poli@link.it)
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  */
  45. public class ConfigurazionePdD_plugins extends AbstractConfigurazionePdDConnectionResourceManager {

  46.     private ServiceManagerProperties smp;
  47.    
  48.    
  49.     public ConfigurazionePdD_plugins(OpenSPCoop2Properties openspcoopProperties, DriverConfigurazioneDB driver, boolean useConnectionPdD) {
  50.         super(openspcoopProperties, driver, useConnectionPdD, OpenSPCoop2Logger.getLoggerOpenSPCoopPluginsSql(openspcoopProperties.isConfigurazionePluginsDebug()));
  51.        
  52.         this.smp = new ServiceManagerProperties();
  53.         this.smp.setShowSql(this.openspcoopProperties.isConfigurazionePluginsDebug());
  54.         this.smp.setDatabaseType(this.driver.getTipoDB());
  55.     }
  56.    
  57.    
  58.    
  59.    
  60.     public int countPlugins(Connection connectionPdD) throws DriverConfigurazioneException{
  61.        
  62.         ConfigurazionePdDConnectionResource cr = null;
  63.         try{
  64.             cr = this.getConnection(connectionPdD, "Plugins.countPlugins");
  65.             org.openspcoop2.core.plugins.dao.IServiceManager sm =
  66.                     (org.openspcoop2.core.plugins.dao.IServiceManager) DAOFactory.getInstance(this.log).
  67.                     getServiceManager(org.openspcoop2.core.plugins.utils.ProjectInfo.getInstance(),
  68.                             cr.connectionDB,this.smp,this.log);
  69.            
  70.             IPluginServiceSearch search =  sm.getPluginServiceSearch();
  71.             IExpression expr = search.newExpression();
  72.             return (int) search.count(expr).longValue();
  73.         }
  74.         catch(Exception e){
  75.             String errorMsg = "Errore durante la conta dei plugins registrati: "+e.getMessage();
  76.             this.log.error(errorMsg,e);
  77.             throw new DriverConfigurazioneException(errorMsg,e);
  78.         }
  79.         finally {
  80.             this.releaseConnection(cr);
  81.         }

  82.     }
  83.    
  84.     public List<IdPlugin> findAllPluginIds(Connection connectionPdD, int offset, int limit) throws DriverConfigurazioneException{
  85.        
  86.         ConfigurazionePdDConnectionResource cr = null;
  87.         try{
  88.             cr = this.getConnection(connectionPdD, "Plugins.findAllPluginIds_"+offset+"_"+limit);
  89.             org.openspcoop2.core.plugins.dao.IServiceManager sm =
  90.                     (org.openspcoop2.core.plugins.dao.IServiceManager) DAOFactory.getInstance(this.log).
  91.                     getServiceManager(org.openspcoop2.core.plugins.utils.ProjectInfo.getInstance(),
  92.                             cr.connectionDB,this.smp,this.log);
  93.            
  94.             IPluginServiceSearch search =  sm.getPluginServiceSearch();
  95.             IPaginatedExpression pagExpr = search.newPaginatedExpression();
  96.             pagExpr.offset(offset);
  97.             pagExpr.limit(limit);
  98.             pagExpr.addOrder(Plugin.model().TIPO_PLUGIN);
  99.             pagExpr.addOrder(Plugin.model().TIPO);
  100.             return search.findAllIds(pagExpr);
  101.         }
  102.         catch(Exception e){
  103.             String errorMsg = "Errore durante la lettura degli idPlugin (offset:"+offset+" limit:"+limit+"): "+e.getMessage();
  104.             this.log.error(errorMsg,e);
  105.             throw new DriverConfigurazioneException(errorMsg,e);
  106.         }
  107.         finally {
  108.             this.releaseConnection(cr);
  109.         }

  110.     }
  111.    
  112.    
  113.    
  114.     public String getPluginClassName(Connection connectionPdD, String tipoPlugin, String tipo) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  115.         return getPluginClassNameByFilter(connectionPdD, tipoPlugin, tipo);
  116.     }
  117.     public String getPluginClassNameByFilter(Connection connectionPdD, String tipoPlugin, String tipo, NameValue ... filtri) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  118.        
  119.         if(tipoPlugin==null || "".equals(tipoPlugin)) {
  120.             throw new DriverConfigurazioneException("tipo plugin non fornito");
  121.         }
  122.         if(tipo==null || "".equals(tipo)) {
  123.             throw new DriverConfigurazioneException("tipo non fornito");
  124.         }
  125.        
  126.         ConfigurazionePdDConnectionResource cr = null;
  127.         try{
  128.             cr = this.getConnection(connectionPdD, "Plugins.getPluginClassName_"+tipoPlugin+"#"+tipo);
  129.             org.openspcoop2.core.plugins.dao.IServiceManager sm =
  130.                     (org.openspcoop2.core.plugins.dao.IServiceManager) DAOFactory.getInstance(this.log).
  131.                     getServiceManager(org.openspcoop2.core.plugins.utils.ProjectInfo.getInstance(),
  132.                             cr.connectionDB,this.smp,this.log);
  133.            
  134.             IPluginServiceSearch search =  sm.getPluginServiceSearch();
  135.             IdPlugin idPlugin = new IdPlugin();
  136.             idPlugin.setTipoPlugin(tipoPlugin);
  137.             idPlugin.setTipo(tipo);
  138.            
  139.             Plugin plugin = search.get(idPlugin);
  140.            
  141.             if(filtri!=null && filtri.length>0) {
  142.                 for (int i = 0; i < filtri.length; i++) {
  143.                     NameValue filtro = filtri[i];
  144.                    
  145.                     if(plugin.sizePluginProprietaCompatibilitaList()>0) {
  146.                         for (int j = 0; j < plugin.sizePluginProprietaCompatibilitaList(); j++) {
  147.                             PluginProprietaCompatibilita ppc = plugin.getPluginProprietaCompatibilita(j);
  148.                             if(ppc.getNome().equals(filtro.getName())) {
  149.                                 if(!ppc.getValore().equals(filtro.getValue())) {
  150.                                    
  151.                                     // gestisco caso speciale
  152.                                     boolean isCasoSpecialeQualsiasi = false;
  153.                                    
  154.                                     if(Filtri.FILTRO_RUOLO_NOME.equals(ppc.getNome()) && Filtri.FILTRO_RUOLO_VALORE_ENTRAMBI.equals(ppc.getValore())) {
  155.                                         isCasoSpecialeQualsiasi = true;
  156.                                     }
  157.                                    
  158.                                     if(Filtri.FILTRO_APPLICABILITA_NOME.equals(ppc.getNome())) {
  159.                                         if(Filtri.FILTRO_APPLICABILITA_VALORE_QUALSIASI.equals(ppc.getValore())) {
  160.                                             isCasoSpecialeQualsiasi = true;
  161.                                         }
  162.                                         else if(Filtri.FILTRO_APPLICABILITA_VALORE_FRUIZIONE.equals(filtro.getValue()) && Filtri.FILTRO_APPLICABILITA_VALORE_IMPLEMENTAZIONE_API.equals(ppc.getValore())) {
  163.                                             isCasoSpecialeQualsiasi = true;
  164.                                         }
  165.                                         else if(Filtri.FILTRO_APPLICABILITA_VALORE_EROGAZIONE.equals(filtro.getValue()) && Filtri.FILTRO_APPLICABILITA_VALORE_IMPLEMENTAZIONE_API.equals(ppc.getValore())) {
  166.                                             isCasoSpecialeQualsiasi = true;
  167.                                         }
  168.                                     }
  169.                                    
  170.                                     if(!isCasoSpecialeQualsiasi) {                                  
  171.                                         throw new NotFoundException("Filtro '"+ppc.getNome()+"' non soddisfatto (atteso:"+filtro.getValue()+" trovato:"+ppc.getValore()+")");
  172.                                     }
  173.                                 }
  174.                             }
  175.                         }
  176.                     }
  177.                    
  178.                 }
  179.             }
  180.            
  181.             return plugin.getClassName();

  182.         }
  183.         catch(NotFoundException e) {
  184.             String errorMsg = "Plugin (tipologia:"+tipoPlugin+" tipo:"+tipo+") non trovato: "+e.getMessage();
  185.             this.log.debug(errorMsg,e);
  186.             throw new DriverConfigurazioneNotFound(errorMsg,e);
  187.         }
  188.         catch(Exception e){
  189.             String errorMsg = "Errore durante la lettura del Plugin (tipologia:"+tipoPlugin+" tipo:"+tipo+"): "+e.getMessage();
  190.             this.log.error(errorMsg,e);
  191.             throw new DriverConfigurazioneException(errorMsg,e);
  192.         }
  193.         finally {
  194.             this.releaseConnection(cr);
  195.         }

  196.     }
  197.    
  198.    
  199.     public String getPluginTipo(Connection connectionPdD, String tipoPlugin, String className) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  200.         return getPluginTipoByFilter(connectionPdD, tipoPlugin, className);
  201.     }
  202.     public String getPluginTipoByFilter(Connection connectionPdD, String tipoPlugin, String className, NameValue ... filtri) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  203.        
  204.         if(tipoPlugin==null || "".equals(tipoPlugin)) {
  205.             throw new DriverConfigurazioneException("tipo plugin non fornito");
  206.         }
  207.         if(className==null || "".equals(className)) {
  208.             throw new DriverConfigurazioneException("Classname non fornito");
  209.         }
  210.        
  211.         ConfigurazionePdDConnectionResource cr = null;
  212.         try{
  213.             cr = this.getConnection(connectionPdD, "Plugins.getPluginTipo_"+tipoPlugin+"#"+className);
  214.             org.openspcoop2.core.plugins.dao.IServiceManager sm =
  215.                     (org.openspcoop2.core.plugins.dao.IServiceManager) DAOFactory.getInstance(this.log).
  216.                     getServiceManager(org.openspcoop2.core.plugins.utils.ProjectInfo.getInstance(),
  217.                             cr.connectionDB,this.smp,this.log);
  218.            
  219.             IPluginServiceSearch search =  sm.getPluginServiceSearch();
  220.            
  221.             IExpression expr = search.newExpression();
  222.             expr.equals(Plugin.model().TIPO_PLUGIN, tipoPlugin);
  223.             expr.equals(Plugin.model().CLASS_NAME, className);
  224.            
  225.             Plugin plugin = search.find(expr);
  226.            
  227.             if(filtri!=null && filtri.length>0) {
  228.                 for (int i = 0; i < filtri.length; i++) {
  229.                     NameValue filtro = filtri[i];
  230.                    
  231.                     if(plugin.sizePluginProprietaCompatibilitaList()>0) {
  232.                         for (int j = 0; j < plugin.sizePluginProprietaCompatibilitaList(); j++) {
  233.                             PluginProprietaCompatibilita ppc = plugin.getPluginProprietaCompatibilita(j);
  234.                             if(ppc.getNome().equals(filtro.getName())) {
  235.                                 if(!ppc.getValore().equals(filtro.getValue())) {
  236.                                    
  237.                                     // gestisco caso speciale
  238.                                     boolean isCasoSpecialeQualsiasi = false;
  239.                                    
  240.                                     if(Filtri.FILTRO_RUOLO_NOME.equals(ppc.getNome()) && Filtri.FILTRO_RUOLO_VALORE_ENTRAMBI.equals(ppc.getValore())) {
  241.                                         isCasoSpecialeQualsiasi = true;
  242.                                     }
  243.                                    
  244.                                     if(Filtri.FILTRO_APPLICABILITA_NOME.equals(ppc.getNome())) {
  245.                                         if(Filtri.FILTRO_APPLICABILITA_VALORE_QUALSIASI.equals(ppc.getValore())) {
  246.                                             isCasoSpecialeQualsiasi = true;
  247.                                         }
  248.                                         else if(Filtri.FILTRO_APPLICABILITA_VALORE_FRUIZIONE.equals(filtro.getValue()) && Filtri.FILTRO_APPLICABILITA_VALORE_IMPLEMENTAZIONE_API.equals(ppc.getValore())) {
  249.                                             isCasoSpecialeQualsiasi = true;
  250.                                         }
  251.                                         else if(Filtri.FILTRO_APPLICABILITA_VALORE_EROGAZIONE.equals(filtro.getValue()) && Filtri.FILTRO_APPLICABILITA_VALORE_IMPLEMENTAZIONE_API.equals(ppc.getValore())) {
  252.                                             isCasoSpecialeQualsiasi = true;
  253.                                         }
  254.                                     }
  255.                                    
  256.                                     if(!isCasoSpecialeQualsiasi) {                                  
  257.                                         throw new NotFoundException("Filtro '"+ppc.getNome()+"' non soddisfatto (atteso:"+filtro.getValue()+" trovato:"+ppc.getValore()+")");
  258.                                     }
  259.                                 }
  260.                             }
  261.                         }
  262.                     }
  263.                    
  264.                 }
  265.             }
  266.            
  267.             return plugin.getTipo();

  268.         }
  269.         catch(NotFoundException e) {
  270.             String errorMsg = "Plugin (tipologia:"+tipoPlugin+" className:"+className+") non trovato: "+e.getMessage();
  271.             this.log.debug(errorMsg,e);
  272.             throw new DriverConfigurazioneNotFound(errorMsg,e);
  273.         }
  274.         catch(Exception e){
  275.             String errorMsg = "Errore durante la lettura del Plugin (tipologia:"+tipoPlugin+" className:"+className+"): "+e.getMessage();
  276.             this.log.error(errorMsg,e);
  277.             throw new DriverConfigurazioneException(errorMsg,e);
  278.         }
  279.         finally {
  280.             this.releaseConnection(cr);
  281.         }

  282.     }
  283.    
  284.    
  285. }