CollectionProperties.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.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.List;

  24. import org.openspcoop2.utils.LoggerWrapperFactory;
  25. import org.openspcoop2.utils.UtilsException;

  26. /**
  27. * CollectionProperties
  28. *
  29. * @author Andrea Poli (apoli@link.it)
  30. * @author $Author$
  31. * @version $Rev$, $Date$
  32. */
  33. public class CollectionProperties {

  34.     /*
  35.      * // RICERCA
  36.         // 1. VARIABILE DI SISTEMA che identifica il singolo file di properties (es. OPENSPCOOP_PROPERTIES o OPENSPCOOP_LOGGER_PROPERTIES)
  37.         // 2. PROPRIETA' JAVA che identifica il singolo file di properties (es. OPENSPCOOP_PROPERTIES o OPENSPCOOP_LOGGER_PROPERTIES)
  38.         // 3. VARIABILE DI SISTEMA: OPENSPCOOP_HOME dove deve essere specificata una directory in cui verra' cercato il file path
  39.         // 4. PROPRIETA' JAVA (es. ApplicationServer o Java con -D): OPENSPCOOP_HOME dove deve essere specificata una directory in cui verra' cercato il file path
  40.         // 5. CLASSPATH con nome path
  41.         // 6. (DIRECTORY DI CONFIGURAZIONE)/path
  42.     */
  43.    
  44.     // NOTA: La logica di append "+," vale solo per le proprietà che sono definite dentro InstanceProperties e cioè viene usato per aggiungere nuovi valori
  45.     //       rispetto agli elementi PropertiesOriginale, CollectionProperties, PropertiesObject
  46.     //       Mentre il valore indicato per una proprietà dei files locale (Properties definiti in questa classi) viene identificato secondo l'algoritmo 1-6
  47.     //       e non va in append, ma viene utilizzato solamente il primo incontrato.
  48.    
  49.     // NOTA: la logica di leggere prima il classpath e poi la directory di configurazione può essere ribaltata impostando la proprietà GOVWAY_FORCE_CONFIG_FILE=true
  50.    
  51.     private PropertiesReader systemVariable;
  52.     private PropertiesReader javaVariable;
  53.     private PropertiesReader systemOpenSPCoopHome;
  54.     private PropertiesReader javaOpenSPCoopHome;
  55.     private PropertiesReader classpath;
  56.     private PropertiesReader configDir;
  57.    
  58.     private boolean forceConfigDir = false;
  59.    
  60.     public void setForceConfigDir(boolean forceConfigDir) {
  61.         this.forceConfigDir = forceConfigDir;
  62.     }
  63.     public PropertiesReader getSystemVariable() {
  64.         return this.systemVariable;
  65.     }
  66.     public void setSystemVariable(PropertiesReader systemVariable) {
  67.         this.systemVariable = systemVariable;
  68.     }
  69.     public PropertiesReader getJavaVariable() {
  70.         return this.javaVariable;
  71.     }
  72.     public void setJavaVariable(PropertiesReader javaVariable) {
  73.         this.javaVariable = javaVariable;
  74.     }
  75.     public PropertiesReader getSystemOpenSPCoopHome() {
  76.         return this.systemOpenSPCoopHome;
  77.     }
  78.     public void setSystemOpenSPCoopHome(PropertiesReader systemOpenSPCoopHome) {
  79.         this.systemOpenSPCoopHome = systemOpenSPCoopHome;
  80.     }
  81.     public PropertiesReader getJavaOpenSPCoopHome() {
  82.         return this.javaOpenSPCoopHome;
  83.     }
  84.     public void setJavaOpenSPCoopHome(PropertiesReader javaOpenSPCoopHome) {
  85.         this.javaOpenSPCoopHome = javaOpenSPCoopHome;
  86.     }
  87.     public PropertiesReader getClasspath() {
  88.         return this.classpath;
  89.     }
  90.     public void setClasspath(PropertiesReader classpath) {
  91.         this.classpath = classpath;
  92.     }
  93.     public PropertiesReader getConfigDir() {
  94.         return this.configDir;
  95.     }
  96.     public void setConfigDir(PropertiesReader configDir) {
  97.         this.configDir = configDir;
  98.     }
  99.    
  100.     public java.util.Enumeration<?> propertyNames(){
  101.         return Collections.enumeration(this._keys());
  102.     }
  103.     public java.util.Enumeration<?> keys(){ // WRAPPER per java.util.Properties.
  104.         return Collections.enumeration(this._keys());
  105.     }
  106.     public int size(){ // WRAPPER per java.util.Properties.
  107.         return this._keys().size();
  108.     }
  109.    
  110.     private List<String> _keys(){
  111.    
  112.         List<String> keys = new ArrayList<>();
  113.        
  114.         if(this.systemVariable!=null){
  115.             java.util.Enumeration<?> enumProp = this.systemVariable.propertyNames();
  116.             while(enumProp.hasMoreElements()){
  117.                 String key = (String)enumProp.nextElement();
  118.                 if(keys.contains(key)==false)
  119.                     keys.add(key);      
  120.             }
  121.         }
  122.        
  123.         if(this.javaVariable!=null){
  124.             java.util.Enumeration<?> enumProp = this.javaVariable.propertyNames();
  125.             while(enumProp.hasMoreElements()){
  126.                 String key = (String)enumProp.nextElement();
  127.                 if(keys.contains(key)==false)
  128.                     keys.add(key);      
  129.             }
  130.         }
  131.        
  132.         if(this.systemOpenSPCoopHome!=null){
  133.             java.util.Enumeration<?> enumProp = this.systemOpenSPCoopHome.propertyNames();
  134.             while(enumProp.hasMoreElements()){
  135.                 String key = (String)enumProp.nextElement();
  136.                 if(keys.contains(key)==false)
  137.                     keys.add(key);      
  138.             }
  139.         }
  140.        
  141.         if(this.javaOpenSPCoopHome!=null){
  142.             java.util.Enumeration<?> enumProp = this.javaOpenSPCoopHome.propertyNames();
  143.             while(enumProp.hasMoreElements()){
  144.                 String key = (String)enumProp.nextElement();
  145.                 if(keys.contains(key)==false)
  146.                     keys.add(key);      
  147.             }
  148.         }
  149.        
  150.         if(this.forceConfigDir) {
  151.             if(this.configDir!=null){
  152.                 java.util.Enumeration<?> enumProp = this.configDir.propertyNames();
  153.                 while(enumProp.hasMoreElements()){
  154.                     String key = (String)enumProp.nextElement();
  155.                     if(keys.contains(key)==false)
  156.                         keys.add(key);      
  157.                 }
  158.             }
  159.         }
  160.        
  161.         if(this.classpath!=null){
  162.             java.util.Enumeration<?> enumProp = this.classpath.propertyNames();
  163.             while(enumProp.hasMoreElements()){
  164.                 String key = (String)enumProp.nextElement();
  165.                 if(keys.contains(key)==false)
  166.                     keys.add(key);          
  167.             }
  168.         }
  169.        
  170.         if(!this.forceConfigDir) {
  171.             if(this.configDir!=null){
  172.                 java.util.Enumeration<?> enumProp = this.configDir.propertyNames();
  173.                 while(enumProp.hasMoreElements()){
  174.                     String key = (String)enumProp.nextElement();
  175.                     if(keys.contains(key)==false)
  176.                         keys.add(key);      
  177.                 }
  178.             }
  179.         }
  180.        
  181.        
  182.         return keys;
  183.     }
  184.    
  185.     public String getValue_convertEnvProperties(String key)throws UtilsException{
  186.    
  187.         if(this.systemVariable!=null){
  188.             String v = this.systemVariable.getValue_convertEnvProperties(key);
  189.             if(v!=null){
  190.                 return v;
  191.             }
  192.         }
  193.        
  194.         if(this.javaVariable!=null){
  195.             String v = this.javaVariable.getValue_convertEnvProperties(key);
  196.             if(v!=null){
  197.                 return v;
  198.             }
  199.         }
  200.        
  201.         if(this.systemOpenSPCoopHome!=null){
  202.             String v = this.systemOpenSPCoopHome.getValue_convertEnvProperties(key);
  203.             if(v!=null){
  204.                 return v;
  205.             }
  206.         }
  207.        
  208.         if(this.javaOpenSPCoopHome!=null){
  209.             String v = this.javaOpenSPCoopHome.getValue_convertEnvProperties(key);
  210.             if(v!=null){
  211.                 return v;
  212.             }
  213.         }
  214.        
  215.         if(this.forceConfigDir) {
  216.             if(this.configDir!=null){
  217.                 String v = this.configDir.getValue_convertEnvProperties(key);
  218.                 if(v!=null){
  219.                     return v;
  220.                 }
  221.             }
  222.         }
  223.        
  224.         if(this.classpath!=null){
  225.             String v = this.classpath.getValue_convertEnvProperties(key);
  226.             if(v!=null){
  227.                 return v;
  228.             }
  229.         }
  230.        
  231.         if(!this.forceConfigDir) {
  232.             if(this.configDir!=null){
  233.                 String v = this.configDir.getValue_convertEnvProperties(key);
  234.                 if(v!=null){
  235.                     return v;
  236.                 }
  237.             }
  238.         }
  239.        
  240.         return null;
  241.        
  242.     }
  243.    
  244.     public String getProperty(String key) { // WRAPPER per java.util.Properties. Non deve lanciare eccezione
  245.         return this.get(key);
  246.     }
  247.     public String get(String key) { // WRAPPER per java.util.Properties. Non deve lanciare eccezione
  248.         try{
  249.             return this.getValue(key);
  250.         }catch(Exception e){
  251.             LoggerWrapperFactory.getLogger(CollectionProperties.class).error("Lettura proprietà ["+key+"] ha generato un errore: "+e.getMessage(),e);
  252.             return null;
  253.         }
  254.     }
  255.     public String getValue(String key) throws UtilsException{
  256.        
  257.         if(this.systemVariable!=null){
  258.             String v = this.systemVariable.getValue(key);
  259.             if(v!=null){
  260.                 return v;
  261.             }
  262.         }
  263.        
  264.         if(this.javaVariable!=null){
  265.             String v = this.javaVariable.getValue(key);
  266.             if(v!=null){
  267.                 return v;
  268.             }
  269.         }
  270.        
  271.         if(this.systemOpenSPCoopHome!=null){
  272.             String v = this.systemOpenSPCoopHome.getValue(key);
  273.             if(v!=null){
  274.                 return v;
  275.             }
  276.         }
  277.        
  278.         if(this.javaOpenSPCoopHome!=null){
  279.             String v = this.javaOpenSPCoopHome.getValue(key);
  280.             if(v!=null){
  281.                 return v;
  282.             }
  283.         }
  284.        
  285.         if(this.forceConfigDir) {
  286.             if(this.configDir!=null){
  287.                 String v = this.configDir.getValue(key);
  288.                 if(v!=null){
  289.                     return v;
  290.                 }
  291.             }
  292.         }
  293.        
  294.         if(this.classpath!=null){
  295.             String v = this.classpath.getValue(key);
  296.             if(v!=null){
  297.                 return v;
  298.             }
  299.         }

  300.         if(!this.forceConfigDir) {
  301.             if(this.configDir!=null){
  302.                 String v = this.configDir.getValue(key);
  303.                 if(v!=null){
  304.                     return v;
  305.                 }
  306.             }
  307.         }
  308.        
  309.         return null;
  310.        
  311.     }
  312.    
  313.     public java.util.Properties readProperties_convertEnvProperties(String prefix)throws UtilsException{
  314.         java.util.Properties prop = new java.util.Properties();
  315.         try{
  316.            
  317.             java.util.Enumeration<?> keys = this.propertyNames(); // property names di questa classe colleziona tutti i nomi di tutti i files esterni
  318.             if(keys!=null){
  319.                 while (keys.hasMoreElements()) {
  320.                     Object keyIt = (Object) keys.nextElement();
  321.                     if(keyIt instanceof String){
  322.                         String property = (String) keyIt;
  323.                         if(property.startsWith(prefix)){
  324.                             String key = (property.substring(prefix.length()));
  325.                             if(key != null)
  326.                                 key = key.trim();
  327.                             String value = this.getValue_convertEnvProperties(property);
  328.                             if(value!=null)
  329.                                 value = ((String)value).trim();
  330.                             if(key!=null && value!=null){
  331.                                 prop.setProperty(key,(String) value);
  332.                             }
  333.                         }
  334.                     }
  335.                 }
  336.             }
  337.            
  338.             return prop;
  339.         }catch(java.lang.Exception e) {
  340.             throw new UtilsException("readProperties Riscontrato errore durante la lettura delle propriete con prefisso ["+prefix+"]: "+e.getMessage(),e);
  341.         }  
  342.     }
  343.    
  344.     public java.util.Properties readProperties(String prefix)throws UtilsException{
  345.         java.util.Properties prop = new java.util.Properties();
  346.         try{
  347.            
  348.             java.util.Enumeration<?> keys = this.propertyNames(); // property names di questa classe colleziona tutti i nomi di tutti i files esterni
  349.             if(keys!=null){
  350.                 while (keys.hasMoreElements()) {
  351.                     Object keyIt = (Object) keys.nextElement();
  352.                     if(keyIt instanceof String){
  353.                         String property = (String) keyIt;
  354.                         if(property.startsWith(prefix)){
  355.                             String key = (property.substring(prefix.length()));
  356.                             if(key != null)
  357.                                 key = key.trim();
  358.                             String value = this.getValue(property);
  359.                             if(value!=null)
  360.                                 value = ((String)value).trim();
  361.                             if(key!=null && value!=null){
  362.                                 prop.setProperty(key,(String) value);
  363.                             }
  364.                         }
  365.                     }
  366.                 }
  367.             }
  368.            
  369.             return prop;
  370.         }catch(java.lang.Exception e) {
  371.             throw new UtilsException("readProperties Riscontrato errore durante la lettura delle propriete con prefisso ["+prefix+"]: "+e.getMessage(),e);
  372.         }  
  373.     }
  374. }