PropertiesService.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.util;

  35. import java.io.File;
  36. import java.io.FileInputStream;
  37. import java.io.IOException;
  38. import java.security.GeneralSecurityException;
  39. import java.util.LinkedHashMap;
  40. import java.util.Map;
  41. import java.util.Properties;

  42. import org.apache.commons.lang3.ArrayUtils;

  43. import fr.paris.lutece.portal.service.security.RsaService;
  44. import fr.paris.lutece.portal.service.util.AppLogService;
  45. import fr.paris.lutece.portal.service.util.AppPropertiesService;

  46. /**
  47.  * This class provides utility methods to read values of the properties stored in the .properties file of the application.
  48.  */
  49. public class PropertiesService
  50. {
  51.     // Static variables
  52.     private String _strRootPath;
  53.     private Properties _properties = new Properties( );
  54.     private Map<String, String> _mapPropertiesFiles = new LinkedHashMap<>( );

  55.     public final String RSA_KEY_PREFIX = "PROTECTED::RSA::";
  56.     private final String MESSAGE_CIPHERED_PROPERTY_SECURITY_EXCEPTION = "A ciphered property security exception occured." ;
  57.    
  58.     /**
  59.      * Constructor should define the base root path for properties files
  60.      *
  61.      * @param strRootPath
  62.      *            The root path
  63.      */
  64.     public PropertiesService( String strRootPath )
  65.     {
  66.         _strRootPath = ( strRootPath.endsWith( "/" ) ) ? strRootPath : ( strRootPath + "/" );
  67.     }

  68.     /**
  69.      * Add properties from a properties file
  70.      *
  71.      * @param strRelativePath
  72.      *            Relative path from the root path
  73.      * @param strFilename
  74.      *            The filename of the properties file (ie: config.properties)
  75.      */
  76.     public void addPropertiesFile( String strRelativePath, String strFilename )
  77.     {
  78.         String strFullPath = _strRootPath + ( ( strRelativePath.endsWith( "/" ) ) ? strRelativePath : ( strRelativePath + "/" ) ) + strFilename;
  79.         _mapPropertiesFiles.put( strFilename, strFullPath );
  80.         loadFile( strFullPath );
  81.     }

  82.     /**
  83.      * Add properties from all files found in a given directory
  84.      *
  85.      * @param strRelativePath
  86.      *            Relative path from the root path
  87.      */
  88.     public void addPropertiesDirectory( String strRelativePath )
  89.     {
  90.         File directory = new File( _strRootPath + strRelativePath );

  91.         if ( directory.exists( ) )
  92.         {
  93.             File [ ] listFile = directory.listFiles( );

  94.             if ( ArrayUtils.isNotEmpty( listFile ) )
  95.             {
  96.                 for ( File file : listFile )
  97.                 {
  98.                     if ( file.getName( ).endsWith( ".properties" ) )
  99.                     {
  100.                         String strFullPath = file.getAbsolutePath( );
  101.                         _mapPropertiesFiles.put( file.getName( ), strFullPath );
  102.                         loadFile( strFullPath );
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.     }

  108.     /**
  109.      * Load properties of a file
  110.      *
  111.      * @param strFullPath
  112.      *            The absolute path of the properties file
  113.      */
  114.     private void loadFile( String strFullPath )
  115.     {
  116.         loadFile( strFullPath, _properties );
  117.     }

  118.     /**
  119.      * Load properties of a file
  120.      *
  121.      * @param strFullPath
  122.      *            The absolute path of the properties file
  123.      * @param props
  124.      *            properties to load into
  125.      * @throws java.io.IOException
  126.      *             If an error occurs reading the file
  127.      */
  128.     private void loadFile( String strFullPath, Properties props )
  129.     {
  130.         try ( FileInputStream fis = new FileInputStream( new File( strFullPath ) ) )
  131.         {
  132.             props.load( fis );
  133.         }
  134.         catch( IOException ex )
  135.         {
  136.             AppLogService.error( "Error loading property file : {}", ex.getMessage( ), ex );
  137.         }
  138.     }

  139.     /**
  140.      * Reload a properties file .
  141.      *
  142.      * @param strFilename
  143.      *            The filename of the properties file
  144.      */
  145.     public void reload( String strFilename )
  146.     {
  147.         String strFullPath = _mapPropertiesFiles.get( strFilename );
  148.         loadFile( strFullPath );
  149.     }

  150.     /**
  151.      * Reload all properties files
  152.      *
  153.      */
  154.     public void reloadAll( )
  155.     {
  156.         Properties newProperties = new Properties( );

  157.         for ( String strFullPath : _mapPropertiesFiles.values( ) )
  158.         {
  159.             loadFile( strFullPath, newProperties );
  160.         }

  161.         _properties = newProperties;
  162.     }

  163.     /**
  164.      * Returns the value of a variable defined in the .properties file of the application as a String
  165.      *
  166.      * @param strProperty
  167.      *            The variable name
  168.      * @return The variable value read in the properties file
  169.      */
  170.     public String getProperty( String strProperty )
  171.     {
  172.         String strValue = _properties.getProperty( strProperty ) ;
  173.        
  174.         if ( strValue != null && strValue.startsWith( RSA_KEY_PREFIX ) )
  175.         {
  176.             try
  177.             {
  178.                 return RsaService.decryptRsa( strValue.substring( RSA_KEY_PREFIX.length( ) ) );
  179.             }
  180.             catch ( GeneralSecurityException e )
  181.             {
  182.                 AppLogService.error( MESSAGE_CIPHERED_PROPERTY_SECURITY_EXCEPTION, e );
  183.             }
  184.         }

  185.         return strValue;
  186.     }

  187.     /**
  188.      * Returns the value of a variable defined in the .properties file of the application as a String
  189.      *
  190.      * @param strProperty
  191.      *            The variable name
  192.      * @param strDefault
  193.      *            The default value which is returned if no value is found for the variable in the .properties file.
  194.      * @return The variable value read in the properties file
  195.      */
  196.     public String getProperty( String strProperty, String strDefault )
  197.     {
  198.         return _properties.getProperty( strProperty, strDefault );
  199.     }

  200.     /**
  201.      * Returns the value of a variable defined in the .properties file of the application as an int
  202.      *
  203.      * @param strProperty
  204.      *            The variable name
  205.      * @param nDefault
  206.      *            The default value which is returned if no value is found for the variable in the .properties file, or if the value is not numeric
  207.      * @return The variable value read in the properties file
  208.      */
  209.     public int getPropertyInt( String strProperty, int nDefault )
  210.     {
  211.         String strValue = AppPropertiesService.getProperty( strProperty );
  212.         int nValue = nDefault;

  213.         try
  214.         {
  215.             if ( strValue != null )
  216.             {
  217.                 nValue = Integer.parseInt( strValue );
  218.             }
  219.         }
  220.         catch( NumberFormatException e )
  221.         {
  222.             AppLogService.info( e.getMessage( ), e );
  223.         }

  224.         return nValue;
  225.     }

  226.     /**
  227.      * Returns the value of a variable defined in the .properties file of the application as an long
  228.      *
  229.      * @param strProperty
  230.      *            The variable name
  231.      * @param lDefault
  232.      *            The default value which is returned if no value is found for the variable in the le downloadFile .properties. .properties file.
  233.      * @return The variable value read in the properties file
  234.      */
  235.     public long getPropertyLong( String strProperty, long lDefault )
  236.     {
  237.         String strValue = AppPropertiesService.getProperty( strProperty );
  238.         long lValue = lDefault;

  239.         try
  240.         {
  241.             if ( strValue != null )
  242.             {
  243.                 lValue = Long.parseLong( strValue );
  244.             }
  245.         }
  246.         catch( NumberFormatException e )
  247.         {
  248.             AppLogService.info( e.getMessage( ), e );
  249.         }

  250.         return lValue;
  251.     }

  252.     /**
  253.      * Returns the value of a variable defined in the .properties file of the application as an boolean
  254.      *
  255.      * @param strProperty
  256.      *            The variable name
  257.      * @param bDefault
  258.      *            The default value which is returned if no value is found for the variable in the le downloadFile .properties. .properties file.
  259.      * @return The variable value read in the properties file
  260.      */
  261.     public boolean getPropertyBoolean( String strProperty, boolean bDefault )
  262.     {
  263.         String strValue = AppPropertiesService.getProperty( strProperty );
  264.         boolean bValue = bDefault;

  265.         if ( strValue != null )
  266.         {
  267.             bValue = strValue.equalsIgnoreCase( "true" );
  268.         }

  269.         return bValue;
  270.     }

  271.     /**
  272.      * Gets properties
  273.      *
  274.      * @return All properties
  275.      */
  276.     public Properties getProperties( )
  277.     {
  278.         return _properties;
  279.     }
  280. }