InstanceProperties.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.utils.properties;

  21. import java.lang.reflect.Method;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.Enumeration;
  25. import java.util.List;
  26. import java.util.Properties;
  27. import java.util.concurrent.ConcurrentMap;

  28. import org.slf4j.Logger;
  29. import org.openspcoop2.utils.LoggerWrapperFactory;
  30. import org.openspcoop2.utils.Utilities;
  31. import org.openspcoop2.utils.UtilsException;



  32. /**
  33. * InstanceProperties
  34. *
  35. * @author Andrea Poli (apoli@link.it)
  36. * @author $Author$
  37. * @version $Rev$, $Date$
  38. */

  39. public abstract class InstanceProperties {

  40.     private PropertiesReader propertiesOriginale;
  41.     private CollectionProperties propertiesRidefinitoFile;
  42.     private PropertiesReader propertiesRidefinitoObject;
  43.     protected Logger log;
  44.     private String openspcoop2LocalHome;
  45.     private boolean readCallsNotSynchronized;
  46.    
  47.     protected InstanceProperties(String openspcoop2LocalHome,Properties propertiesOriginale,Logger log) {
  48.         this(openspcoop2LocalHome, propertiesOriginale, log, true);
  49.     }
  50.     protected InstanceProperties(String openspcoop2LocalHome,Properties propertiesOriginale,Logger log, boolean readCallsNotSynchronized) {
  51.         this.propertiesOriginale = new PropertiesReader(propertiesOriginale,readCallsNotSynchronized);
  52.         this.log = log;
  53.         if(this.log==null){
  54.             this.log = LoggerWrapperFactory.getLogger(InstanceProperties.class);
  55.         }
  56.         this.openspcoop2LocalHome = openspcoop2LocalHome;
  57.         this.readCallsNotSynchronized = readCallsNotSynchronized;
  58.     }
  59.    
  60.     private void logError(String msg, Exception e) {
  61.         this.log.error(msg, e);
  62.     }
  63.    
  64.     public void setLocalFileImplementation(String variable,String path,String confDirectory){
  65.         CollectionProperties prop = PropertiesUtilities.searchLocalImplementation(this.openspcoop2LocalHome,this.log, variable, path, confDirectory, this.readCallsNotSynchronized);
  66.         if(prop!=null)
  67.             this.propertiesRidefinitoFile = prop;
  68.     }
  69.    
  70.     public void setLocalFileImplementation(CollectionProperties prop){
  71.         this.propertiesRidefinitoFile = prop;
  72.     }
  73.    
  74.     public void setLocalObjectImplementation(Properties prop){
  75.         this.propertiesRidefinitoObject = new PropertiesReader(prop,this.readCallsNotSynchronized);
  76.     }
  77.    
  78.     // RIDEFINIZIONE METODI PROPERTIES READER
  79.    
  80.     public String getValue(String key) throws UtilsException{
  81.         return getValueEngine(key, false);
  82.     }
  83.        
  84.     public String getValueConvertEnvProperties(String key)throws UtilsException{
  85.         return getValueEngine(key, true);
  86.     }
  87.        
  88.     public java.util.Properties readProperties (String prefix)throws UtilsException{
  89.         return readPropertiesEngine(prefix, false);
  90.     }
  91.    
  92.     public java.util.Properties readPropertiesConvertEnvProperties (String prefix)throws UtilsException{
  93.         return readPropertiesEngine(prefix, true);
  94.     }
  95.    
  96.     public ConcurrentMap<String, String> readPropertiesAsConcurrentHashMap (String prefix)throws UtilsException{
  97.         return Utilities.convertToConcurrentHashMap(readProperties(prefix));
  98.     }
  99.    
  100.     public ConcurrentMap<String, String> readPropertiesAsConcurrentHashMapConvertEnvProperties (String prefix)throws UtilsException{
  101.         return Utilities.convertToConcurrentHashMap(readPropertiesConvertEnvProperties(prefix));
  102.     }
  103.    
  104.     public java.util.Map<String, String> readPropertiesAsHashMap (String prefix)throws UtilsException{
  105.         return Utilities.convertToHashMap(readProperties(prefix));
  106.     }
  107.    
  108.     public java.util.Map<String, String> readPropertiesAsHashMapConvertEnvProperties (String prefix)throws UtilsException{
  109.         return Utilities.convertToHashMap(readPropertiesConvertEnvProperties(prefix));
  110.     }
  111.    
  112.     public String convertEnvProperties(String value)throws UtilsException{
  113.         return this.propertiesOriginale.convertEnvProperties(value);
  114.     }
  115.    
  116.     public java.util.Enumeration<String> propertyNames(){
  117.        
  118.         java.util.Enumeration<?> enumProp = this.propertiesOriginale.propertyNames();
  119.         List<String> object = new ArrayList<>();
  120.         while(enumProp.hasMoreElements()){
  121.             object.add((String)enumProp.nextElement());    
  122.         }
  123.        
  124.         if(this.propertiesRidefinitoFile!=null){
  125.             java.util.Enumeration<?> enumPropRidefinito = this.propertiesRidefinitoFile.propertyNames();
  126.             while(enumPropRidefinito!=null && enumPropRidefinito.hasMoreElements()){
  127.                 String ridefinito = (String)enumPropRidefinito.nextElement();
  128.                 if(!object.contains(ridefinito)){
  129.                     object.add(ridefinito);    
  130.                 }
  131.             }
  132.         }
  133.        
  134.         if(this.propertiesRidefinitoObject!=null){
  135.             java.util.Enumeration<?> enumPropRidefinito = this.propertiesRidefinitoObject.propertyNames();
  136.             while(enumPropRidefinito!=null && enumPropRidefinito.hasMoreElements()){
  137.                 String ridefinito = (String)enumPropRidefinito.nextElement();
  138.                 if(!object.contains(ridefinito)){
  139.                     object.add(ridefinito);    
  140.                 }
  141.             }
  142.         }
  143.        
  144.         return Collections.enumeration(object);
  145.     }


  146.    
  147.    
  148.    
  149.    
  150.     /* ------------ UTILITY INTERNE ----------------- */
  151.     private String getValueEngine(String key,boolean convertEnvProperties) throws UtilsException{
  152.        
  153.         // NOTA: La logica di append "+," vale solo rispetto agli elementi PropertiesOriginale, CollectionProperties, PropertiesObject
  154.         //       Mentre il valore indicato per una proprietà dei files locali all'interno del CollectionProperties non va in append, ma viene utilizzato solamente il primo incontrato (che poi sarà utilizzato per come valore del 2. File).
  155.                
  156.         try{
  157.                    
  158.             // 1. Object
  159.             String [] addObject = null;
  160.             boolean appendObject = false;
  161.             String tmpObject = null;
  162.             if(this.propertiesRidefinitoObject!=null){
  163.                 if(convertEnvProperties){
  164.                     tmpObject = this.propertiesRidefinitoObject.getValue_convertEnvProperties(key);
  165.                 }else{
  166.                     tmpObject = this.propertiesRidefinitoObject.getValue(key);
  167.                 }
  168.             }
  169.             if(tmpObject!=null){
  170.                 tmpObject = tmpObject.trim();
  171.                 String add = null;
  172.                 if(tmpObject.startsWith("+")){
  173.                     appendObject = true;
  174.                     add = tmpObject.substring(1);
  175.                     add = add.trim();
  176.                 }
  177.                 if(add!=null){
  178.                     if(add.startsWith(",") && add.length()>1){
  179.                         addObject = add.substring(1).split(",");
  180.                         if(addObject!=null && addObject.length>0){
  181.                             for (int i = 0; i < addObject.length; i++) {
  182.                                 addObject[i] = addObject[i].trim();
  183.                             }
  184.                         }else{
  185.                             // valore strano, lo ritorno
  186.                             addObject = new String[1];
  187.                             addObject[0] = tmpObject;
  188.                         }
  189.                     }
  190.                 }else{
  191.                     addObject = new String[1];
  192.                     addObject[0] = tmpObject;
  193.                 }
  194.             }
  195.            
  196.            
  197.             // 2. File
  198.             String [] addFile = null;
  199.             boolean appendFile = false;
  200.             String tmpFile = null;
  201.             if(this.propertiesRidefinitoFile!=null){
  202.                 if(convertEnvProperties){
  203.                     tmpFile = this.propertiesRidefinitoFile.getValue_convertEnvProperties(key);
  204.                 }else{
  205.                     tmpFile = this.propertiesRidefinitoFile.getValue(key);
  206.                 }
  207.             }
  208.             if(tmpFile!=null){
  209.                 tmpFile = tmpFile.trim();
  210.                 String add = null;
  211.                 if(tmpFile.startsWith("+")){
  212.                     appendFile = true;
  213.                     add = tmpFile.substring(1);
  214.                     add = add.trim();
  215.                 }
  216.                 if(add!=null){
  217.                     if(add.startsWith(",") && add.length()>1){
  218.                         addFile = add.substring(1).split(",");
  219.                         if(addFile!=null && addFile.length>0){
  220.                             for (int i = 0; i < addFile.length; i++) {
  221.                                 addFile[i] = addFile[i].trim();
  222.                             }
  223.                         }else{
  224.                             // valore strano, lo ritorno
  225.                             addFile = new String[1];
  226.                             addFile[0] = tmpFile;
  227.                         }
  228.                     }
  229.                 }else{
  230.                     addFile = new String[1];
  231.                     addFile[0] = tmpFile;
  232.                 }
  233.             }
  234.            
  235.            
  236.             // 3. Originale
  237.             String tmp = null;
  238.             if(tmp==null){
  239.                 if(convertEnvProperties){
  240.                     tmp = this.propertiesOriginale.getValue_convertEnvProperties(key);
  241.                 }else{
  242.                     tmp = this.propertiesOriginale.getValue(key);
  243.                 }
  244.             }
  245.            
  246.            
  247.             // 4. Gestione valore ritornato
  248.             if(addObject==null && addFile==null){
  249.                 return tmp;
  250.             }
  251.             if(!appendObject && !appendFile){
  252.                 // Tecnica append non utilizzato
  253.                 // Ritorno il primo valore disponibile rispetto all'ordine
  254.                 if(addObject!=null){
  255.                     return addObject[0];
  256.                 }
  257.                 else if(addFile!=null){
  258.                     return addFile[0];
  259.                 }
  260.                 else{
  261.                     return tmp;
  262.                 }
  263.             }
  264.            
  265.            
  266.             // 5. Se sono state fornite proprieta' con il + le gestisco.
  267.             // In caso di valori uguali utilizzo quelli dell'object, poi quelli del file e infine quelli dell'originale
  268.             StringBuilder bf = new StringBuilder();
  269.             List<String> valoriAggiunti = new ArrayList<>();
  270.             if(addObject!=null){
  271.                 for (int i = 0; i < addObject.length; i++) {
  272.                     if(!valoriAggiunti.contains(addObject[i])){
  273.                         if(bf.length()>0){
  274.                             bf.append(",");
  275.                         }
  276.                         bf.append(addObject[i]);
  277.                         valoriAggiunti.add(addObject[i]);
  278.                     }
  279.                 }
  280.             }
  281.             if(addFile!=null){
  282.                 for (int i = 0; i < addFile.length; i++) {
  283.                     if(!valoriAggiunti.contains(addFile[i])){
  284.                         if(bf.length()>0){
  285.                             bf.append(",");
  286.                         }
  287.                         bf.append(addFile[i]);
  288.                         valoriAggiunti.add(addFile[i]);
  289.                     }
  290.                 }
  291.             }
  292.             if(tmp!=null &&
  293.                 !valoriAggiunti.contains(tmp)){
  294.                 if(bf.length()>0){
  295.                     bf.append(",");
  296.                 }
  297.                 bf.append(tmp);
  298.                 valoriAggiunti.add(tmp);
  299.             }
  300.            
  301.             return bf.toString();
  302.            
  303.         }catch(Exception e){
  304.             this.logError("Errore durante la lettura della proprieta' ["+key+"]("+convertEnvProperties+")",e);
  305.             throw new UtilsException(e.getMessage(),e);
  306.         }
  307.        
  308.     }
  309.    
  310.     private java.util.Properties readPropertiesEngine(String prefix,boolean convertEnvProperties)throws UtilsException{
  311.        
  312.         java.util.Properties tmp = null;
  313.         try{
  314.        
  315.             if(convertEnvProperties){
  316.                 tmp = this.propertiesOriginale.readProperties_convertEnvProperties(prefix);
  317.             }else{
  318.                 tmp = this.propertiesOriginale.readProperties(prefix);
  319.             }
  320.            
  321.             if(this.propertiesRidefinitoFile!=null){
  322.                 java.util.Properties tmp2 = null;
  323.                 if(convertEnvProperties){
  324.                     tmp2 = this.propertiesRidefinitoFile.readProperties_convertEnvProperties(prefix);
  325.                 }else{
  326.                     tmp2 = this.propertiesRidefinitoFile.readProperties(prefix);
  327.                 }
  328.                 if(tmp2!=null){
  329.                     Enumeration<?> keys = tmp2.keys();
  330.                     while (keys.hasMoreElements()) {
  331.                         String key = (String) keys.nextElement();
  332.                         if(tmp.containsKey(key)){
  333.                             tmp.remove(key);
  334.                         }
  335.                         tmp.put(key, tmp2.get(key));
  336.                     }
  337.                 }
  338.             }
  339.            
  340.             if(this.propertiesRidefinitoObject!=null){
  341.                 java.util.Properties tmp3 = null;
  342.                 if(convertEnvProperties){
  343.                     tmp3 = this.propertiesRidefinitoObject.readProperties_convertEnvProperties(prefix);
  344.                 }else{
  345.                     tmp3 = this.propertiesRidefinitoObject.readProperties(prefix);
  346.                 }
  347.                 if(tmp3!=null){
  348.                     Enumeration<?> keys = tmp3.keys();
  349.                     while (keys.hasMoreElements()) {
  350.                         String key = (String) keys.nextElement();
  351.                         if(tmp.containsKey(key)){
  352.                             tmp.remove(key);
  353.                         }
  354.                         tmp.put(key, tmp3.get(key));
  355.                     }
  356.                 }
  357.             }
  358.            
  359.         }catch(Exception e){
  360.             this.logError("Errore durante la lettura delle proprieta' con prefix ["+prefix+"]("+convertEnvProperties+")",e);
  361.             throw new UtilsException(e.getMessage(),e);
  362.         }
  363.        
  364.         return tmp;
  365.     }
  366.    
  367.    
  368.     public static String readConfDirFromGovWayProperties() {
  369.         try {
  370.             Class<?> cOp2Props = Class.forName("org.openspcoop2.pdd.config.OpenSPCoop2Properties");
  371.             Method cOp2PropsMethodGetInstance = cOp2Props.getMethod("getInstance");
  372.             Object op2Props = cOp2PropsMethodGetInstance.invoke(null);
  373.             if(op2Props!=null) {
  374.                 Method rootDirMethod = op2Props.getClass().getMethod("getRootDirectory");
  375.                 Object rootDir = rootDirMethod.invoke(op2Props);
  376.                 if(rootDir instanceof String) {
  377.                     return (String) rootDir;
  378.                 }
  379.             }
  380.         }catch(Exception t) {
  381.             // ignore
  382.         }
  383.         return null;
  384.     }
  385.    
  386. }