PropertiesReader.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.Properties;

  22. import org.openspcoop2.utils.UtilsException;
  23. import org.openspcoop2.utils.resources.MapReader;

  24. /**
  25.  * Lettore di file properties, che permette di interpretare anche le variabili di sistema
  26.  *
  27.  * @author Poli Andrea (apoli@link.it)
  28.  * @author $Author$
  29.  * @version $Rev$, $Date$
  30.  */
  31. public class PropertiesReader extends MapReader<Object, Object> {
  32.    
  33.     public PropertiesReader(Properties properties, boolean readCallsNotSynchronized){
  34.         super(properties, readCallsNotSynchronized);
  35.     }
  36.    
  37.     public String getValue(String key) throws UtilsException{
  38.         Object valueObject = super.getValue(key);
  39.         if(valueObject!=null){
  40.             if(!(valueObject instanceof String)){
  41.                 throw new UtilsException("Proprieta '"+key+"' non e' di tipo java.lang.String (trovato tipo: ["+valueObject.getClass().getName()+"] valore: ["+valueObject+"])");
  42.             }
  43.             String value = (String) valueObject;
  44.             return value.trim();
  45.         }
  46.         else{
  47.             return null;
  48.         }
  49.     }
  50.    
  51.     public String convertEnvProperties(String value)throws UtilsException{
  52.         return this.convertEnvProperties(value, false);
  53.     }
  54.     public String convertEnvProperties(String value, boolean convertKeyEnvProperties)throws UtilsException{
  55.         String label = "del valore";
  56.         if(convertKeyEnvProperties){
  57.             label = "della chiave";
  58.         }
  59.         while (value.indexOf("${")!=-1){
  60.             int indexStart = value.indexOf("${");
  61.             int indexEnd = value.indexOf("}");
  62.             if(indexEnd==-1){
  63.                 throw new UtilsException("Errore durante l'interpretazione "+label+" ["+value+"]: ${ utilizzato senza la rispettiva chiusura }");
  64.             }
  65.             String nameSystemProperty = value.substring(indexStart+"${".length(),indexEnd);
  66.             String valueSystemProperty = System.getenv(nameSystemProperty); // sistema
  67.             if(valueSystemProperty==null) {
  68.                 valueSystemProperty = System.getProperty(nameSystemProperty); // java
  69.             }
  70.             if(valueSystemProperty==null){
  71.                 throw new UtilsException("Errore durante l'interpretazione "+label+" ["+value+"]: variabile di sistema o java ${"+nameSystemProperty+"} non esistente");
  72.             }
  73.             value = value.replace("${"+nameSystemProperty+"}", valueSystemProperty);
  74.         }
  75.         return value;
  76.     }
  77.    
  78.     public String getValue_convertEnvProperties(String key)throws UtilsException{
  79.         String value = this.getValue(key);
  80.         if(value!=null)
  81.             value = this.convertEnvProperties(value);
  82.         return value;
  83.         //}

  84.     }
  85.    
  86.     /**
  87.      * Legge le proprieta' che possiedono un nome che inizia con un determinato prefisso
  88.      *
  89.      * @param prefix
  90.      * @return java.util.Properties
  91.      * @throws UtilsException
  92.      */
  93.     public java.util.Properties readProperties (String prefix)throws UtilsException{
  94.         java.util.Properties prop = new java.util.Properties();
  95.         try{
  96.            
  97.             java.util.Enumeration<?> keys = this.keys();
  98.             while (keys.hasMoreElements()) {
  99.                 Object keyIt = keys.nextElement();
  100.                 if(keyIt instanceof String){
  101.                     String property = (String) keyIt;
  102.                     if(property.startsWith(prefix)){
  103.                         String key = (property.substring(prefix.length()));
  104.                         if(key != null)
  105.                             key = key.trim();
  106.                         String value = this.getValue(property);
  107.                         if(value!=null)
  108.                             value = (value).trim();
  109.                         if(key!=null && value!=null){
  110.                             prop.setProperty(key,value);
  111.                         }
  112.                     }
  113.                 }
  114.             }
  115.             return prop;
  116.         }catch(java.lang.Exception e) {
  117.             throw new UtilsException("readProperties Riscontrato errore durante la lettura delle propriete con prefisso ["+prefix+"]: "+e.getMessage(),e);
  118.         }  
  119.     }
  120.    
  121.     /**
  122.      * Legge le proprieta' che possiedono un nome che inizia con un determinato prefisso
  123.      *
  124.      * @param prefix
  125.      * @return java.util.Properties
  126.      * @throws UtilsException
  127.      */
  128.     public java.util.Properties readProperties_convertEnvProperties (String prefix)throws UtilsException{
  129.         return this.readProperties_convertEnvProperties(prefix,false);
  130.     }
  131.     public java.util.Properties readProperties_convertEnvProperties (String prefix, boolean convertKeyEnvProperties)throws UtilsException{
  132.         java.util.Properties prop = new java.util.Properties();
  133.         try{
  134.             java.util.Properties propTmp = this.readProperties(prefix);
  135.             java.util.Enumeration<?> en = propTmp.propertyNames();
  136.             for (; en.hasMoreElements() ;) {
  137.                 String property = (String) en.nextElement();
  138.                 String value = null;
  139.                 if(property!=null) {
  140.                     value = propTmp.getProperty(property);
  141.                 }
  142.                 if(value!=null){
  143.                     value = value.trim();
  144.                     value = this.convertEnvProperties(value);
  145.                 }
  146.                 if(property!=null && value!=null){
  147.                     if(convertKeyEnvProperties){
  148.                         prop.setProperty(this.convertEnvProperties(property,true),value);
  149.                     }
  150.                     else{
  151.                         prop.setProperty(property,value);
  152.                     }
  153.                 }
  154.             }
  155.             return prop;
  156.         }catch(java.lang.Exception e) {
  157.             throw new UtilsException("readProperties Riscontrato errore durante la lettura delle propriete con prefisso ["+prefix+"]: "+e.getMessage(),e);
  158.         }  
  159.     }
  160.    
  161.     public java.util.Enumeration<?> propertyNames(){
  162.         return this.keys();
  163.     }
  164.    
  165. }