ServerProperties.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.util.Enumeration;
  22. import java.util.Properties;

  23. import org.slf4j.Logger;
  24. import org.openspcoop2.utils.BooleanNullable;
  25. import org.openspcoop2.utils.LoggerWrapperFactory;
  26. import org.openspcoop2.utils.UtilsException;
  27. import org.openspcoop2.utils.UtilsRuntimeException;
  28. import org.openspcoop2.utils.crypt.CryptConfig;
  29. import org.openspcoop2.utils.service.authorization.AuthorizationConfig;
  30. import org.openspcoop2.web.monitor.core.config.ApplicationProperties;
  31. import org.openspcoop2.web.monitor.core.core.Utility;

  32. /**    
  33.  * ServerProperties
  34.  *
  35.  * @author Poli Andrea (poli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class ServerProperties  {

  40.     /** Logger utilizzato per errori eventuali. */
  41.     private Logger log = null;
  42.    
  43.     /** Reader delle proprieta' impostate nel file 'rs-api-monitor.properties' */
  44.     private ServerInstanceProperties reader;
  45.    
  46.     /** Copia Statica */
  47.     private static ServerProperties serverProperties = null;
  48.    
  49.    
  50.     private ServerProperties(String confDir,Logger log) throws UtilsException {

  51.         if(log!=null)
  52.             this.log = log;
  53.         else
  54.             this.log = LoggerWrapperFactory.getLogger(ServerProperties.class);
  55.        
  56.         /* ---- Lettura del cammino del file di configurazione ---- */
  57.         Properties propertiesReader = new Properties();
  58.         try (java.io.InputStream properties = DatasourceProperties.class.getResourceAsStream("/rs-api-monitor.properties")){  
  59.             if(properties==null){
  60.                 throw new UtilsException("File '/rs-api-monitor.properties' not found");
  61.             }
  62.             propertiesReader.load(properties);
  63.         }catch(Exception e) {
  64.             String error="Riscontrato errore durante la lettura del file 'rs-api-monitor.properties': "+e.getMessage();
  65.             this.log.error(error);
  66.             throw new UtilsException("RS Api MonitorProperties initialize error: "+e.getMessage());
  67.         }

  68.         this.reader = new ServerInstanceProperties(propertiesReader, this.log, confDir);
  69.        
  70.         this.log.info("Inizializzazione ApplicationProperties in corso...");
  71.         try{
  72.             ApplicationProperties.initialize(log, "/rs-api-monitor.properties", ConstantsEnv.OPENSPCOOP2_RS_API_MONITOR_PROPERTIES, ConstantsEnv.OPENSPCOOP2_RS_API_MONITOR_LOCAL_PATH);
  73.         }catch(Exception e){
  74.             throw new UtilsRuntimeException(e.getMessage(),e);
  75.         }
  76.         this.log.info("Inizializzazione ApplicationProperties effettuata con successo");
  77.     }


  78.     /**
  79.      * Il Metodo si occupa di inizializzare il propertiesReader
  80.      *
  81.      *
  82.      */
  83.     public static boolean initialize(String confDir,Logger log){

  84.         try {
  85.             ServerProperties.serverProperties = new ServerProperties(confDir,log);  
  86.             return true;
  87.         }
  88.         catch(Exception e) {
  89.             log.error("Errore durante l'inizializzazione del ServerProperties: "+e.getMessage(),e);        
  90.             return false;
  91.         }
  92.     }
  93.    
  94.     /**
  95.      * Ritorna l'istanza di questa classe
  96.      *
  97.      * @return Istanza di Properties
  98.      *
  99.      */
  100.     public static ServerProperties getInstance() throws UtilsException{
  101.         if(ServerProperties.serverProperties==null){
  102.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  103.             synchronized (ServerProperties.class) {
  104.                 throw new UtilsException("ServerProperties non inizializzato");
  105.             }
  106.         }
  107.         return ServerProperties.serverProperties;
  108.     }
  109.    
  110.     public static void updateLocalImplementation(Properties prop){
  111.         ServerProperties.serverProperties.reader.setLocalObjectImplementation(prop);
  112.     }








  113.     /* ********  M E T O D I  ******** */

  114.     private boolean parse(BooleanNullable b, boolean defaultValue) {
  115.         return (b!=null && b.getValue()!=null) ? b.getValue() : defaultValue;
  116.     }
  117.    
  118.     public String readProperty(boolean required,String property) throws UtilsException{
  119.         String tmp = this.reader.getValueConvertEnvProperties(property);
  120.         if(tmp==null){
  121.             if(required){
  122.                 throw new UtilsException("Property ["+property+"] not found");
  123.             }
  124.             else{
  125.                 return null;
  126.             }
  127.         }else{
  128.             return tmp.trim();
  129.         }
  130.     }
  131.    
  132.     private BooleanNullable readBooleanProperty(boolean required,String property) throws UtilsException{
  133.         String tmp = this.readProperty(required, property);
  134.         if(tmp==null && !required) {
  135.             return BooleanNullable.NULL(); // se e' required viene sollevata una eccezione dal metodo readProperty
  136.         }
  137.         if(!"true".equalsIgnoreCase(tmp) && !"false".equalsIgnoreCase(tmp)){
  138.             throw new UtilsException("Property ["+property+"] with uncorrect value ["+tmp+"] (true/value expected)");
  139.         }
  140.         return Boolean.parseBoolean(tmp) ? BooleanNullable.TRUE() : BooleanNullable.FALSE();
  141.     }
  142.    
  143.     public Properties getProperties() throws UtilsException{
  144.         Properties p = new Properties();
  145.         Enumeration<?> names = this.reader.propertyNames();
  146.         while (names.hasMoreElements()) {
  147.             String name = (String) names.nextElement();
  148.             p.put(name, this.reader.getValueConvertEnvProperties(name));
  149.         }
  150.         return p;
  151.     }
  152.    
  153.     private AuthorizationConfig authConfig = null;
  154.     private synchronized void initAuthorizationConfig() throws UtilsException {
  155.         if(this.authConfig==null) {
  156.             this.authConfig = new AuthorizationConfig(getProperties());
  157.         }
  158.     }
  159.     public AuthorizationConfig getAuthorizationConfig() throws UtilsException {
  160.         if(this.authConfig==null) {
  161.             this.initAuthorizationConfig();
  162.         }
  163.         return this.authConfig;
  164.     }

  165.    
  166.     public String getConfDirectory() throws UtilsException {
  167.         return this.readProperty(false, "confDirectory");
  168.     }
  169.     public String getProtocolloDefault() throws UtilsException {
  170.         return this.readProperty(true, "protocolloDefault");
  171.     }
  172.    
  173.     public boolean isJdbcCloseConnectionCheckIsClosed() throws UtilsException{
  174.         BooleanNullable b = this.readBooleanProperty(true, "jdbc.closeConnection.checkIsClosed");
  175.         return this.parse(b, true);
  176.     }
  177.     public boolean isJdbcCloseConnectionCheckAutocommit() throws UtilsException{
  178.         BooleanNullable b = this.readBooleanProperty(true, "jdbc.closeConnection.checkAutocommit");
  179.         return this.parse(b, true);
  180.     }
  181.    
  182.     public boolean isFindall404() throws UtilsException {
  183.         return Boolean.parseBoolean(this.readProperty(true, "findall_404"));
  184.     }
  185.    
  186.    
  187.     public boolean useSoggettoDefault() throws UtilsException {
  188.         if(Utility.isMultitenantAbilitato()) {
  189.             return Boolean.parseBoolean(this.readProperty(true, "multitenant.forzaSoggettoDefault"));
  190.         }
  191.         else {
  192.             return true; // in caso di multitenant disabilitato, il soggetto di default viene sempre impostato
  193.         }
  194.     }
  195.            
  196.     public String getSoggettoDefaultIfEnabled(String protocollo) throws UtilsException {
  197.         if(this.useSoggettoDefault()) {
  198.             String p = this.readProperty(false, protocollo+".soggetto");
  199.             if(p!=null) {
  200.                 return p;
  201.             }
  202.             return this.readProperty(true, "soggetto");
  203.         }
  204.         else {
  205.             throw new UtilsException("Utilizzo del soggetto di default non abilitato");
  206.         }
  207.     }


  208.     public org.openspcoop2.utils.service.context.ContextConfig getContextConfig() throws UtilsException {
  209.         org.openspcoop2.utils.service.context.ContextConfig config = new org.openspcoop2.utils.service.context.ContextConfig();
  210.         config.setClusterId(this.readProperty(false, "clusterId"));
  211.         config.setDump(Boolean.parseBoolean(this.readProperty(true, "dump")));
  212.         config.setEmitTransaction(Boolean.parseBoolean(this.readProperty(true, "transaction")));
  213.         config.setServiceType(this.readProperty(false, "service.type"));
  214.         config.setServiceName(this.readProperty(false, "service.name"));
  215.         config.setServiceVersion(Integer.parseInt(this.readProperty(false, "service.version")));
  216.         return config;
  217.     }
  218.    
  219.     public String getUtenzePassword() throws UtilsException{
  220.         return this.readProperty(true, "utenti.password");
  221.     }
  222.     private static CryptConfig utenzeCryptConfig = null;
  223.     private static synchronized void initUtenzeCryptConfig(String p) throws UtilsException {
  224.         if(utenzeCryptConfig==null) {
  225.             utenzeCryptConfig = new CryptConfig(p);
  226.         }
  227.     }
  228.     public CryptConfig getUtenzeCryptConfig() throws UtilsException {
  229.         if(utenzeCryptConfig==null) {
  230.             initUtenzeCryptConfig(getUtenzePassword());
  231.         }
  232.         return utenzeCryptConfig;
  233.     }
  234.    
  235.    
  236.     public int getTransazioniDettaglioVisualizzazioneMessaggiThreshold() throws NumberFormatException, UtilsException {
  237.         return Integer.valueOf(this.readProperty(true, "transazioni.dettaglio.visualizzazioneMessaggi.threshold"));
  238.     }
  239.    
  240.    
  241.    
  242.     public boolean isSecurityLoadBouncyCastleProvider() throws UtilsException{
  243.         BooleanNullable b = this.readBooleanProperty(false, "security.addBouncyCastleProvider");
  244.         return parse(b, false);
  245.     }
  246.    
  247.    
  248.     public String getEnvMapConfig() throws UtilsException{
  249.         return this.readProperty(false, "env.map.config");
  250.     }
  251.     public boolean isEnvMapConfigRequired() throws UtilsException{
  252.         BooleanNullable b = this.readBooleanProperty(false, "env.map.required");
  253.         return this.parse(b, false);
  254.     }
  255.    
  256.    
  257.     public String getHSMConfigurazione() throws UtilsException {
  258.         return this.readProperty(false, "hsm.config");
  259.     }
  260.     public boolean isHSMRequired() throws UtilsException {
  261.         BooleanNullable b = this.readBooleanProperty(false, "hsm.required");
  262.         return parse(b, false);
  263.     }
  264.     public boolean isHSMKeyPasswordConfigurable() throws UtilsException{
  265.         BooleanNullable b = this.readBooleanProperty(false, "hsm.keyPassword");
  266.         return this.parse(b, false);
  267.     }
  268.    
  269.    
  270.    
  271.     public String getBYOKConfigurazione() throws UtilsException{
  272.         return this.readProperty(false, "byok.config");
  273.     }
  274.     public boolean isBYOKRequired() throws UtilsException{
  275.         BooleanNullable b = this.readBooleanProperty(false, "byok.required");
  276.         return parse(b, false);
  277.     }
  278.     public String getBYOKEnvSecretsConfig() throws UtilsException{
  279.         return this.readProperty(false, "byok.env.secrets.config");
  280.     }
  281.     public boolean isBYOKEnvSecretsConfigRequired() throws UtilsException{
  282.         BooleanNullable b = this.readBooleanProperty(false, "byok.env.secrets.required");
  283.         return this.parse(b, false);
  284.     }
  285.    
  286.    
  287.     public String getConfigurazioneNodiRuntime() throws UtilsException{
  288.         return this.readProperty(false, "configurazioni.configurazioneNodiRun");
  289.     }
  290. }