LoaderProperties.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.generic_project.utils;

  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.InputStream;
  24. import java.util.Properties;

  25. import org.openspcoop2.generic_project.exception.ServiceException;

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

  34.     protected Properties properties = null;
  35.     protected String filePropertiesName = null;
  36.    
  37.     public LoaderProperties(String filePropertiesName) throws ServiceException{
  38.         InputStream is = null;
  39.         try{
  40.             this.filePropertiesName = filePropertiesName;
  41.             File f = new File(filePropertiesName);
  42.             if(f.exists()){
  43.                 is = new FileInputStream(f);
  44.             }else{
  45.                
  46. //              java.net.URL url = LoaderProperties.class.getResource(filePropertiesName);
  47. //              if(url!=null){
  48. //                  System.out.println("PATH ["+url.toString()+"]");
  49. //              }
  50. //              else{
  51. //                   url = LoaderProperties.class.getResource("/"+filePropertiesName);
  52. //                   System.out.println("PATH 2 ["+url.toString()+"]");
  53. //              }
  54.                
  55.                 is = LoaderProperties.class.getResourceAsStream(filePropertiesName);
  56.                 if(is==null){
  57.                     is = LoaderProperties.class.getResourceAsStream("/"+filePropertiesName);
  58.                 }
  59.             }
  60.             if(is==null){
  61.                 throw new Exception("InputStream is null");
  62.             }
  63.             this.properties = new Properties();
  64.             this.properties.load(is);
  65.            
  66.         }catch(Exception e){
  67.             throw new ServiceException("Loading properties file ["+filePropertiesName+"] failed: "+e.getMessage(),e);
  68.         }finally{
  69.             try{
  70.                 if(is!=null) {
  71.                     is.close();
  72.                 }
  73.             }catch(Exception e){
  74.                 // close
  75.             }
  76.         }
  77.     }
  78.     public LoaderProperties(Properties properties) throws ServiceException{
  79.         this.properties = properties;
  80.     }

  81.     public Properties getProperties() {
  82.         return this.properties;
  83.     }
  84.    
  85.    
  86.    
  87. }