View Javadoc
1   /*
2    * Copyright (c) 2002-2015, Mairie de 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  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  import fr.paris.lutece.portal.service.util.AppPropertiesService;
38  
39  import org.apache.commons.lang.StringUtils;
40  
41  import java.io.File;
42  import java.io.FileInputStream;
43  import java.io.FileNotFoundException;
44  import java.io.IOException;
45  
46  import java.util.LinkedHashMap;
47  import java.util.Map;
48  import java.util.Properties;
49  
50  
51  /**
52   * This class provides utility methods to read values of the properties stored in the .properties file of the
53   * application.
54   */
55  public class PropertiesService
56  {
57      // Static variables
58      private static String _strRootPath;
59      private static volatile Properties _properties = new Properties(  );
60      private static Map<String, String> _mapPropertiesFiles = new LinkedHashMap<String, String>(  );
61  
62      /**
63       * Constructor should define the base root path for properties files
64       * @param strRootPath The root path
65       */
66      public PropertiesService( String strRootPath )
67      {
68          _strRootPath = ( strRootPath.endsWith( "/" ) ) ? strRootPath : ( strRootPath + "/" );
69      }
70  
71      /**
72       * Add properties from a properties file
73       * @param strRelativePath Relative path from the root path
74       * @param strFilename The filename of the properties file (ie: config.properties)
75       * @throws java.io.FileNotFoundException If the file is not found
76       * @throws java.io.IOException If an error occurs reading the file
77       */
78      public void addPropertiesFile( String strRelativePath, String strFilename )
79          throws FileNotFoundException, IOException
80      {
81          String strFullPath = _strRootPath +
82              ( ( strRelativePath.endsWith( "/" ) ) ? strRelativePath : ( strRelativePath + "/" ) ) + strFilename;
83          _mapPropertiesFiles.put( strFilename, strFullPath );
84          loadFile( strFullPath );
85      }
86  
87      /**
88       * Add properties from all files found in a given directory
89       * @param strRelativePath Relative path from the root path
90       * @throws IOException If an error occurs reading the file
91       */
92      public void addPropertiesDirectory( String strRelativePath )
93          throws IOException
94      {
95          File directory = new File( _strRootPath + strRelativePath );
96  
97          if ( directory.exists(  ) )
98          {
99              File[] listFile = directory.listFiles(  );
100 
101             for ( int i = 0; i < listFile.length; i++ )
102             {
103                 File file = listFile[i];
104 
105                 if ( file.getName(  ).endsWith( ".properties" ) )
106                 {
107                     String strFullPath = file.getAbsolutePath(  );
108                     _mapPropertiesFiles.put( file.getName(  ), strFullPath );
109                     loadFile( strFullPath );
110                 }
111             }
112         }
113     }
114 
115     /**
116      * Load properties of a file
117      * @param strFullPath The absolute path of the properties file
118      * @throws java.io.IOException If an error occurs reading the file
119      * @throws java.io.FileNotFoundException If the file is not found
120      */
121     private void loadFile( String strFullPath ) throws FileNotFoundException, IOException
122     {
123         loadFile( strFullPath, _properties );
124     }
125 
126     /**
127      * Load properties of a file
128      * @param strFullPath The absolute path of the properties file
129      * @param props properties to load into
130      * @throws java.io.IOException If an error occurs reading the file
131      * @throws java.io.FileNotFoundException If the file is not found
132      */
133     private void loadFile( String strFullPath, Properties props )
134         throws FileNotFoundException, IOException
135     {
136         FileInputStream fis = new FileInputStream( new File( strFullPath ) );
137         props.load( fis );
138     }
139 
140     /**
141      * Reload a properties file .
142      * @param strFilename The filename of the properties file
143      * @throws IOException If an error occurs reading the file
144      */
145     public void reload( String strFilename ) throws IOException
146     {
147         String strFullPath = _mapPropertiesFiles.get( strFilename );
148         loadFile( strFullPath );
149     }
150 
151     /**
152      * Reload all properties files
153      * @throws IOException If an error occurs reading the file
154      */
155     public void reloadAll(  ) throws IOException
156     {
157         Properties newProperties = new Properties(  );
158 
159         for ( String strFullPath : _mapPropertiesFiles.values(  ) )
160         {
161             loadFile( strFullPath, newProperties );
162         }
163 
164         _properties = newProperties;
165     }
166 
167     /**
168      * Returns the value of a variable defined in the .properties file of the application as a String
169      *
170      * @param strProperty The variable name
171      * @return The variable value read in the properties file
172      */
173     public String getProperty( String strProperty )
174     {
175         return _properties.getProperty( strProperty );
176     }
177 
178     /**
179      * Returns the value of a variable defined in the .properties file of the application as a String
180      *
181      * @param strProperty The variable name
182      * @param strDefault The default value which is returned if no value is found for the variable in the .properties
183      *        file.
184      * @return The variable value read in the properties file
185      */
186     public String getProperty( String strProperty, String strDefault )
187     {
188         return _properties.getProperty( strProperty, strDefault );
189     }
190 
191     /**
192      * Returns the value of a variable defined in the .properties file of the application as an int
193      *
194      * @param strProperty The variable name
195      * @param nDefault The default value which is returned if no value is found for the variable in the
196      *        .properties file, or if the value is not numeric
197      * @return The variable value read in the properties file
198      */
199     public int getPropertyInt( String strProperty, int nDefault )
200     {
201         String strValue = AppPropertiesService.getProperty( strProperty );
202         int nValue = nDefault;
203 
204         try
205         {
206             if ( StringUtils.isNumeric( strValue ) )
207             {
208                 nValue = Integer.parseInt( strValue );
209             }
210         }
211         catch ( NumberFormatException e )
212         {
213             AppLogService.info( e );
214         }
215 
216         return nValue;
217     }
218 
219     /**
220      * Returns the value of a variable defined in the .properties file of the application as an long
221      *
222      * @param strProperty The variable name
223      * @param lDefault The default value which is returned if no value is found for the variable in the le downloadFile
224      *        .properties. .properties file.
225      * @return The variable value read in the properties file
226      */
227     public long getPropertyLong( String strProperty, long lDefault )
228     {
229         String strValue = AppPropertiesService.getProperty( strProperty );
230         long lValue = lDefault;
231 
232         try
233         {
234             if ( strValue != null )
235             {
236                 lValue = Long.parseLong( strValue );
237             }
238         }
239         catch ( NumberFormatException e )
240         {
241             AppLogService.info( e );
242         }
243 
244         return lValue;
245     }
246 
247     /**
248      * Returns the value of a variable defined in the .properties file of the application as an boolean
249      *
250      * @param strProperty The variable name
251      * @param bDefault The default value which is returned if no value is found for the variable in the le downloadFile
252      *        .properties. .properties file.
253      * @return The variable value read in the properties file
254      */
255     public boolean getPropertyBoolean( String strProperty, boolean bDefault )
256     {
257         String strValue = AppPropertiesService.getProperty( strProperty );
258         boolean bValue = bDefault;
259 
260         if ( strValue != null )
261         {
262             bValue = strValue.equalsIgnoreCase( "true" );
263         }
264 
265         return bValue;
266     }
267 
268     /**
269      * Gets properties
270      * @return All properties
271      */
272     public Properties getProperties(  )
273     {
274         return _properties;
275     }
276 }