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

  24. import org.slf4j.Logger;
  25. import org.openspcoop2.core.config.SystemProperties;
  26. import org.openspcoop2.core.config.Property;
  27. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;

  28. /**
  29.  * SystemPropertiesManager
  30.  *
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class SystemPropertiesManager {

  37.     public static final String SYSTEM_PROPERTIES = "OpenSPCoop2SystemProperties";
  38.    
  39.     private ConfigurazionePdDManager configPdDManager;
  40.     private Logger log;
  41.    
  42.     private void logInfo(String msg) {
  43.         if(this.log!=null) {
  44.             this.log.info(msg);
  45.         }
  46.     }
  47.    
  48.     public SystemPropertiesManager(ConfigurazionePdDManager configPdDManager,Logger log){
  49.         this.configPdDManager = configPdDManager;
  50.         this.log = log;
  51.     }

  52.     private String getPrefixKey(String key) {
  53.         return "Property["+key+"] ";
  54.     }
  55.    
  56.     public void updateSystemProperties() throws DriverConfigurazioneException{
  57.        
  58.         // remove old properties
  59.         String oldProperties = System.getProperty(SYSTEM_PROPERTIES);
  60.         if(oldProperties!=null && oldProperties.length()>0){
  61.             this.logInfo("Remove old properties: ["+oldProperties+"]");
  62.             String [] names = oldProperties.split(",");
  63.             for (int i = 0; i < names.length; i++) {
  64.                 this.logInfo("RemoveProperty ["+names[i]+"]");
  65.                 System.clearProperty(names[i]);
  66.             }
  67.         }
  68.         System.clearProperty(SYSTEM_PROPERTIES);
  69.        
  70.         SystemProperties sps = this.configPdDManager.getSystemPropertiesPdD();
  71.         if(sps!=null && sps.sizeSystemPropertyList()>0){
  72.                        
  73.             // set new propertis
  74.             StringBuilder bf = new StringBuilder();
  75.             for (int i = 0; i < sps.sizeSystemPropertyList(); i++) {
  76.                 Property sp = sps.getSystemProperty(i);
  77.                 String nome = sp.getNome();
  78.                 String valore = sp.getValore();
  79.                
  80.                 if(bf.length()>0){
  81.                     bf.append(",");
  82.                 }
  83.                 bf.append(nome);
  84.                 this.logInfo("SetProperty ["+nome+"]=["+valore+"]");
  85.                 System.setProperty(nome, valore);
  86.             }
  87.            
  88.             // Storico
  89.             this.logInfo("SetStoricoProperty ["+SYSTEM_PROPERTIES+"]=["+bf.toString()+"]");
  90.             System.setProperty(SYSTEM_PROPERTIES, bf.toString());
  91.            
  92.         }
  93.         else{
  94.             this.logInfo("Non sono state rilevate proprietà di sistema da impostare");
  95.         }
  96.        
  97.     }
  98.    
  99.    
  100.     public String getPropertyValue(String key){
  101.         return System.getProperty(key);
  102.     }
  103.    
  104.     public String readAllSystemProperties(String separator){
  105.         return this.readAllSystemProperties(separator, false);
  106.     }
  107.     private String readAllSystemProperties(String separator,boolean onlyOpenSPCoop2){
  108.         Properties p = System.getProperties();
  109.         if(p!=null){
  110.            
  111.             StringBuilder bf = new StringBuilder();
  112.            
  113.             Enumeration<Object> keys = p.keys();
  114.             java.util.ArrayList<String> listKeys = new ArrayList<>();
  115.             while (keys.hasMoreElements()) {
  116.                 Object object = keys.nextElement();
  117.                 if(object instanceof String){
  118.                     listKeys.add((String)object);
  119.                 }
  120.             }
  121.             java.util.Collections.sort(listKeys);
  122.            
  123.             addSystemProperties(p, listKeys, separator, onlyOpenSPCoop2, bf);
  124.            
  125.             return bf.toString();
  126.            
  127.         }else{
  128.             return null;
  129.         }
  130.     }
  131.     private void addSystemProperties(Properties p,java.util.ArrayList<String> listKeys,String separator,boolean onlyOpenSPCoop2,StringBuilder bf) {
  132.         for (int i = 0; i < listKeys.size(); i++) {
  133.             String key = listKeys.get(i);
  134.            
  135.             boolean isOpenSPCoop2SystemProperty = isOpenSPCoop2SystemProperty(key);
  136.             if(!isOpenSPCoop2SystemProperty &&
  137.                 onlyOpenSPCoop2){
  138.                 continue;
  139.             }
  140.            
  141.             if(bf.length()>0){
  142.                 bf.append(separator);
  143.             }
  144.             bf.append("["+key+"]=["+p.getProperty(key)+"]");
  145.         }
  146.     }
  147.     private boolean isOpenSPCoop2SystemProperty(String key) {
  148.         boolean isOpenSPCoop2SystemProperty = false;
  149.         String oldProperties = System.getProperty(SYSTEM_PROPERTIES);
  150.         if(oldProperties!=null && oldProperties.length()>0){
  151.             String [] names = oldProperties.split(",");
  152.             for (int j = 0; j < names.length; j++) {
  153.                 if(key.equals(names[j])){
  154.                     isOpenSPCoop2SystemProperty = true;
  155.                     break;
  156.                 }
  157.             }
  158.         }
  159.         return isOpenSPCoop2SystemProperty;
  160.     }
  161.    
  162.     public String readOpenSPCoop2SystemProperties(String separator){
  163.         return this.readAllSystemProperties(separator, true);
  164.     }

  165.     public void removeProperty(String key) throws OpenSPCoop2ConfigurationException{
  166.         if(!System.getProperties().containsKey(key)){
  167.             throw new OpenSPCoop2ConfigurationException(getPrefixKey(key)+"not found");
  168.         }
  169.         System.clearProperty(key);
  170.         String oldProperties = System.getProperty(SYSTEM_PROPERTIES);
  171.         StringBuilder bf = new StringBuilder();
  172.         if(oldProperties!=null && oldProperties.length()>0){
  173.             String [] names = oldProperties.split(",");
  174.             for (int j = 0; j < names.length; j++) {
  175.                 if(key.equals(names[j])){
  176.                     continue;
  177.                 }
  178.                 if(bf.length()>0){
  179.                     bf.append(",");
  180.                 }
  181.                 bf.append(names[j]);
  182.             }
  183.             System.clearProperty(SYSTEM_PROPERTIES);
  184.             System.setProperty(SYSTEM_PROPERTIES, bf.toString());
  185.         }
  186.     }
  187.    
  188.     public void updateProperty(String key,String value) throws OpenSPCoop2ConfigurationException{
  189.         if(!System.getProperties().containsKey(key)){
  190.             throw new OpenSPCoop2ConfigurationException(getPrefixKey(key)+"not found");
  191.         }
  192.         System.clearProperty(key);
  193.         System.setProperty(key,value);
  194.     }
  195.    
  196.     public void insertProperty(String key,String value) throws OpenSPCoop2ConfigurationException{
  197.         if(System.getProperties().containsKey(key)){
  198.             throw new OpenSPCoop2ConfigurationException(getPrefixKey(key)+"already exists (actual value: "+System.getProperties().getProperty(key)+")");
  199.         }
  200.         System.setProperty(key,value);
  201.        
  202.         String props = System.getProperty(SYSTEM_PROPERTIES);
  203.         if(props!=null && props.length()>0){
  204.             props = props + "," + key;
  205.         }
  206.         else{
  207.             props = key;
  208.         }
  209.        
  210.         System.clearProperty(SYSTEM_PROPERTIES);
  211.         System.setProperty(SYSTEM_PROPERTIES, props);
  212.     }
  213. }