ValidazioneSemanticaMainProcessor.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.registry.driver.utils;

  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.net.URL;
  24. import java.sql.Connection;
  25. import java.sql.DriverManager;

  26. import org.apache.logging.log4j.Level;
  27. import org.openspcoop2.core.registry.driver.BeanUtilities;
  28. import org.openspcoop2.core.registry.driver.ValidazioneSemantica;
  29. import org.openspcoop2.core.registry.driver.db.DriverRegistroServiziDB;
  30. import org.openspcoop2.core.registry.driver.uddi.DriverRegistroServiziUDDI;
  31. import org.openspcoop2.core.registry.driver.web.DriverRegistroServiziWEB;
  32. import org.openspcoop2.core.registry.driver.xml.DriverRegistroServiziXML;
  33. import org.openspcoop2.utils.LoggerWrapperFactory;
  34. import org.openspcoop2.utils.Utilities;
  35. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  36. import org.openspcoop2.utils.resources.FileSystemUtilities;
  37. import org.openspcoop2.utils.resources.Loader;
  38. import org.slf4j.Logger;

  39. /**
  40.  * Validazione Semantica
  41.  *
  42.  * @author Sandra Giangrandi (sandra@link.it)
  43.  * @author $Author$
  44.  * @version $Rev$, $Date$
  45.  */
  46. public class ValidazioneSemanticaMainProcessor {

  47.         /**
  48.      * @param args
  49.      */
  50.     public static void main(String[] args) throws Exception  {
  51.         // TODO Auto-generated method stub
  52.        
  53.         if (args.length  < 4) {
  54.             String errorMsg = "ERROR, Usage:  java ValidazioneSemanticaMainProcessor commonProperties tipoRegistro proprietaRegistro checkURI";
  55.             System.err.println(errorMsg);
  56.             throw new Exception(errorMsg);
  57.         }

  58.         // Inizializzo logger
  59. //      System.out.println("LUNGHEZZA: "+args.length);
  60. //      for (int i = 0; i < args.length; i++) {
  61. //          System.out.println("ARG["+i+"]=["+args[i]+"]");
  62. //      }
  63.         try{
  64.             if(args.length==5){
  65.                 LoggerWrapperFactory.setLogConfiguration(args[4]);
  66.             }else{
  67.                 URL url = ValidazioneSemanticaMainProcessor.class.getResource("/validator.log4j2.properties");
  68.                 if(url!=null){
  69.                     LoggerWrapperFactory.setLogConfiguration(url);
  70.                 }
  71.                 else{
  72.                     File logFile = FileSystemUtilities.createTempFile("testValidazioneSemanticaRegistro_", ".log");
  73.                     System.out.println("LogMessages write in "+logFile.getAbsolutePath());
  74.                     LoggerWrapperFactory.setDefaultLogConfiguration(Level.ALL, false, null, logFile, "%p <%d{dd-MM-yyyy HH:mm:ss}> %C.%M(%L): %m %n %n");
  75.                 }
  76.             }
  77.         }catch(Exception e) {
  78.             String errorMsg = "Errore durante il caricamento del file di log args4["+args[4]+"] : "+e.getMessage();
  79.             System.err.println(errorMsg);
  80.             System.out.println("Args.length: "+args.length);
  81.             for (int i = 0; i < args.length; i++) {
  82.                 System.out.println("Arg["+i+"]=["+args[i]+"]");
  83.             }
  84.             throw new Exception(errorMsg,e);
  85.         }  
  86.         Logger log = LoggerWrapperFactory.getLogger("validatoreDatiRegistro");
  87.        
  88.        
  89.         java.util.Properties commonProperties = new java.util.Properties();
  90.         try{
  91.             try(FileInputStream fin = new FileInputStream(args[0])){
  92.                 commonProperties.load(fin);
  93.             }
  94.         }catch(java.io.IOException e) {
  95.             String errorMsg = "Errore durante il caricamento del file di properties ["+args[0]+"] : "+e.getMessage();
  96.             log.error(errorMsg,e);
  97.             throw new Exception(errorMsg,e);
  98.         }
  99.        
  100.        
  101.         java.util.Properties reader = new java.util.Properties();
  102.         try{
  103.             try(FileInputStream fin = new FileInputStream(args[2])){
  104.                 reader.load(fin);
  105.             }
  106.         }catch(java.io.IOException e) {
  107.             String errorMsg = "Errore durante il caricamento del file di properties ["+args[2]+"] : "+e.getMessage();
  108.             log.error(errorMsg,e);
  109.             throw new Exception(errorMsg,e);
  110.         }
  111.        
  112.                
  113.         // Lettura registro
  114.         org.openspcoop2.core.registry.RegistroServizi registro = null;
  115.         Connection connectionDB = null;
  116.         String tipoRegistro = args[1];
  117.         boolean checkURI = Boolean.parseBoolean(args[3]);
  118.        
  119.         if("xml".equals(tipoRegistro)){
  120.            
  121.             // RegistroServizi XML
  122.             String path = reader.getProperty("openspcoop2.registroServizi.xml");
  123.             if(path==null){
  124.                 throw new Exception("Non e' stato definito il path dove localizzare il registro servizi xml");
  125.             }else{
  126.                 path = path.trim();
  127.             }
  128.            
  129.             DriverRegistroServiziXML driver = new DriverRegistroServiziXML(path,log);
  130.             if(driver.create)
  131.                 log.info("Inizializzato Registro dei Servizi XML");
  132.             else
  133.                 throw new Exception("RegistroServizi XML non inizializzato");
  134.            
  135.             registro = driver.getImmagineCompletaRegistroServizi();

  136.         }
  137.         else if("db".equals(tipoRegistro)){
  138.            
  139.             // RegistroServizi DB
  140.            
  141.             // Database
  142.             String tipoDatabase = reader.getProperty("openspcoop2.registroServizi.db.tipo");
  143.             if(tipoDatabase==null){
  144.                 throw new Exception("Non e' stato definito il tipo di database");
  145.             }else{
  146.                 tipoDatabase = tipoDatabase.trim();
  147.             }
  148.            
  149.             String dataSource = reader.getProperty("openspcoop2.registroServizi.db.dataSource");
  150.             if(dataSource!=null){
  151.                 dataSource = dataSource.trim();
  152.                 java.util.Properties context = Utilities.readProperties("openspcoop2.registroServizi.db.context.",reader);
  153.                
  154.                 DriverRegistroServiziDB driver = new DriverRegistroServiziDB(dataSource,context,log,tipoDatabase);
  155.                 if(driver.create)
  156.                     log.info("Inizializzato Registro dei Servizi DB");
  157.                 else
  158.                     throw new Exception("RegistroServizi DB non inizializzato");
  159.                
  160.                 registro = driver.getImmagineCompletaRegistroServizi();
  161.                
  162.             }else{
  163.                 String connection = reader.getProperty("openspcoop2.registroServizi.db.url");
  164.                 if(connection==null){
  165.                     throw new Exception("Non e' stata definita una destinazione ne attraverso un datasource, ne attraverso una connessione diretta");
  166.                 }
  167.                 connection = connection.trim();
  168.                 String driverJDBC = reader.getProperty("openspcoop2.registroServizi.db.driver");
  169.                 if(driverJDBC==null){
  170.                     throw new Exception("Connessione diretta: non e' stato definito il Driver");
  171.                 }
  172.                 driverJDBC =driverJDBC.trim();
  173.                 String username = reader.getProperty("openspcoop2.registroServizi.db.user");
  174.                 String password = reader.getProperty("openspcoop2.registroServizi.db.password");
  175.                 if(username!=null){
  176.                     username = username.trim();
  177.                 }
  178.                 if(password!=null){
  179.                     password = password.trim();
  180.                 }
  181.                
  182.                 // Carico driver JDBC
  183.                 Loader.getInstance().newInstance(driverJDBC);
  184.                
  185.                 try{
  186.                     if(username!=null){
  187.                         connectionDB = DriverManager.getConnection(connection, username, password);
  188.                     }else{
  189.                         connectionDB = DriverManager.getConnection(connection);
  190.                     }
  191.                    
  192.                     DriverRegistroServiziDB driver = new DriverRegistroServiziDB(connectionDB,log,tipoDatabase);
  193.                     if(driver.create)
  194.                         log.info("Inizializzato Registro dei Servizi DB");
  195.                     else
  196.                         throw new Exception("RegistroServizi DB non inizializzato");
  197.                    
  198.                     registro = driver.getImmagineCompletaRegistroServizi();
  199.                    
  200.                 }finally{
  201.                     try{
  202.                         JDBCUtilities.closeConnection(DriverRegistroServiziDB.getCheckLogger(), connectionDB, DriverRegistroServiziDB.isCheckAutocommit(), DriverRegistroServiziDB.isCheckIsClosed());
  203.                     }catch(Exception e){
  204.                         // close
  205.                     }
  206.                 }
  207.             }  
  208.            
  209.         }
  210.         else if("web".equals(tipoRegistro)){
  211.            
  212.             // RegistroServizi WEB
  213.             String urlPrefix = reader.getProperty("openspcoop2.registroServizi.web.urlPrefix");
  214.             if(urlPrefix==null){
  215.                 throw new Exception("Non e' stato definito la url dove localizzare il registro servizi web");
  216.             }else{
  217.                 urlPrefix = urlPrefix.trim();
  218.             }
  219.             String pathPrefix = reader.getProperty("openspcoop2.registroServizi.web.pathPrefix");
  220.             if(pathPrefix==null){
  221.                 throw new Exception("Non e' stato definito il path dove localizzare il registro servizi web");
  222.             }else{
  223.                 pathPrefix = pathPrefix.trim();
  224.             }
  225.            
  226.             DriverRegistroServiziWEB driver = new DriverRegistroServiziWEB(urlPrefix,pathPrefix,log);
  227.             if(driver.create)
  228.                 log.info("Inizializzato Registro dei Servizi WEB");
  229.             else
  230.                 throw new Exception("RegistroServizi WEB non inizializzato");
  231.            
  232.             registro = driver.getImmagineCompletaRegistroServizi();
  233.            
  234.         }
  235.         else if("uddi".equals(tipoRegistro)){
  236.            
  237.             // RegistroServizi UDDI
  238.            
  239.             String inquiryUrl = reader.getProperty("openspcoop2.registroServizi.uddi.inquiryUrl");
  240.             if(inquiryUrl==null){
  241.                 throw new Exception("Non e' stato definito la url dove localizzare il registro servizi uddi");
  242.             }else{
  243.                 inquiryUrl = inquiryUrl.trim();
  244.             }
  245.             String urlPrefix = reader.getProperty("openspcoop2.registroServizi.uddi.urlPrefix");
  246.             if(urlPrefix==null){
  247.                 throw new Exception("Non e' stato definito la url prefix dove localizzare il registro servizi uddi (repository http)");
  248.             }else{
  249.                 urlPrefix = urlPrefix.trim();
  250.             }
  251.             String usernameUDDI = reader.getProperty("openspcoop2.registroServizi.uddi.username");
  252.             if(usernameUDDI!=null)
  253.                 usernameUDDI = usernameUDDI.trim();
  254.             String passwordUDDI = reader.getProperty("openspcoop2.registroServizi.uddi.password");
  255.             if(passwordUDDI!=null)
  256.                 passwordUDDI = passwordUDDI.trim();
  257.                        
  258.             DriverRegistroServiziUDDI driver = new DriverRegistroServiziUDDI(inquiryUrl,null,usernameUDDI,passwordUDDI,urlPrefix,null,log);
  259.             if(driver.create)
  260.                 log.info("Inizializzato Registro dei Servizi UDDI");
  261.             else
  262.                 throw new Exception("RegistroServizi UDDI non inizializzato");
  263.            
  264.             registro = driver.getImmagineCompletaRegistroServizi();
  265.         }
  266.         else if("ws".equals(tipoRegistro)){
  267.            
  268.             // RegistroServizi WS
  269.             String url = reader.getProperty("openspcoop2.registroServizi.ws.url");
  270.             if(url==null){
  271.                 throw new Exception("Non e' stato definito la url dove localizzare il registro servizi ws");
  272.             }else{
  273.                 url = url.trim();
  274.             }
  275.             String username = reader.getProperty("openspcoop2.registroServizi.ws.username");
  276.             if(username!=null)
  277.                 username = username.trim();
  278.             String password = reader.getProperty("openspcoop2.registroServizi.ws.password");
  279.             if(password!=null)
  280.                 password = password.trim();
  281.            
  282.             BeanUtilities driver = DriverRegistroServiziWSInitUtilities.newInstance(url, username, password, log);
  283.            
  284.             registro = driver.getImmagineCompletaRegistroServizi();
  285.         }
  286.         else{
  287.             throw new Exception("Tipo di registro servizi ["+tipoRegistro+"] non gestito");
  288.         }
  289.        

  290.         // VALIDAZIONE SEMANTICA
  291.         try{
  292.             String tipiConnettori = commonProperties.getProperty("openspcoop2.tipiConnettori");
  293.             if(tipiConnettori==null){
  294.                 throw new Exception("Non sono stati definiti i tipi di connettori supportati");
  295.             }else{
  296.                 tipiConnettori = tipiConnettori.trim();
  297.             }
  298.             String [] tipoConnettoriArray = tipiConnettori.split(",");
  299.             for (int i = 0; i < tipoConnettoriArray.length; i++) {
  300.                 tipoConnettoriArray[i]=tipoConnettoriArray[i].trim();
  301.             }
  302.            
  303.             String tipiSoggetti = commonProperties.getProperty("openspcoop2.tipiSoggetti");
  304.             if(tipiSoggetti==null){
  305.                 throw new Exception("Non sono stati definiti i tipi di soggetti supportati");
  306.             }else{
  307.                 tipiSoggetti = tipiSoggetti.trim();
  308.             }
  309.             String [] tipoSoggettiArray = tipiSoggetti.split(",");
  310.             for (int i = 0; i < tipoSoggettiArray.length; i++) {
  311.                 tipoSoggettiArray[i]=tipoSoggettiArray[i].trim();
  312.             }
  313.            
  314.             String tipiServiziSoap = commonProperties.getProperty("openspcoop2.tipiServizi.soap");
  315.             if(tipiServiziSoap==null){
  316.                 throw new Exception("Non sono stati definiti i tipi di Servizi supportati");
  317.             }else{
  318.                 tipiServiziSoap = tipiServiziSoap.trim();
  319.             }
  320.             String [] tipoServiziArraySoap = tipiServiziSoap.split(",");
  321.             for (int i = 0; i < tipoServiziArraySoap.length; i++) {
  322.                 tipoServiziArraySoap[i]=tipoServiziArraySoap[i].trim();
  323.             }
  324.            
  325.             String tipiServiziRest = commonProperties.getProperty("openspcoop2.tipiServizi.rest");
  326.             if(tipiServiziRest==null){
  327.                 throw new Exception("Non sono stati definiti i tipi di Servizi supportati");
  328.             }else{
  329.                 tipiServiziRest = tipiServiziRest.trim();
  330.             }
  331.             String [] tipoServiziArrayRest = tipiServiziRest.split(",");
  332.             for (int i = 0; i < tipoServiziArrayRest.length; i++) {
  333.                 tipoServiziArrayRest[i]=tipoServiziArrayRest[i].trim();
  334.             }
  335.            
  336.             ValidazioneSemantica validatore = new ValidazioneSemantica(registro,checkURI,tipoConnettoriArray,tipoSoggettiArray,tipoServiziArraySoap,tipoServiziArrayRest,log);
  337.            
  338.             validatore.validazioneSemantica(true);
  339.         }catch(Exception e){
  340.             log.error("Errore: "+e.getMessage());
  341.             throw e;
  342.             //e.printStackTrace(System.out);
  343.         }
  344.        
  345.     }

  346.    
  347. }