View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.portal.service.util;
35  
36  import fr.paris.lutece.portal.service.init.LuteceInitException;
37  import fr.paris.lutece.util.PropertiesService;
38  
39  import java.io.FileNotFoundException;
40  import java.io.IOException;
41  
42  import java.util.ArrayList;
43  import java.util.Enumeration;
44  import java.util.HashMap;
45  import java.util.List;
46  import java.util.Map;
47  import java.util.Properties;
48  
49  
50  /**
51   * this class provides management services for properties files
52   */
53  public final class AppPropertiesService
54  {
55      private static final String FILE_PROPERTIES_CONFIG = "config.properties";
56      private static final String FILE_PROPERTIES_DATABASE = "db.properties";
57      private static final String FILE_PROPERTIES_LUTECE = "lutece.properties";
58      private static final String FILE_PROPERTIES_SEARCH = "search.properties";
59      private static final String FILE_PROPERTIES_DAEMONS = "daemons.properties";
60      private static final String FILE_PROPERTIES_CACHES = "caches.properties";
61      private static final String FILE_PROPERTIES_EDITORS = "editors.properties";
62      private static final String PATH_PLUGINS = "plugins/";
63      private static final String PATH_OVERRIDE_CORE = "override/";
64      private static final String PATH_OVERRIDE_PLUGINS = "override/plugins";
65      private static PropertiesService _propertiesService;
66      private static String _strConfPath;
67  
68      /**
69       * Private constructor
70       */
71      private AppPropertiesService(  )
72      {
73      }
74  
75      /**
76       * Initializes the service
77       * @param strConfPath The configuration path
78       * @throws LuteceInitException If an error occured
79       */
80      public static void init( String strConfPath ) throws LuteceInitException
81      {
82          _strConfPath = strConfPath;
83          _propertiesService = new PropertiesService( AppPathService.getWebAppPath(  ) );
84  
85          try
86          {
87              _propertiesService.addPropertiesFile( _strConfPath, FILE_PROPERTIES_CONFIG );
88              _propertiesService.addPropertiesFile( _strConfPath, FILE_PROPERTIES_DATABASE );
89              _propertiesService.addPropertiesFile( _strConfPath, FILE_PROPERTIES_LUTECE );
90              _propertiesService.addPropertiesFile( _strConfPath, FILE_PROPERTIES_SEARCH );
91              _propertiesService.addPropertiesFile( _strConfPath, FILE_PROPERTIES_DAEMONS );
92              _propertiesService.addPropertiesFile( _strConfPath, FILE_PROPERTIES_CACHES );
93              _propertiesService.addPropertiesFile( _strConfPath, FILE_PROPERTIES_EDITORS );
94              _propertiesService.addPropertiesDirectory( _strConfPath + PATH_PLUGINS );
95              _propertiesService.addPropertiesDirectory( _strConfPath + PATH_OVERRIDE_CORE );
96              _propertiesService.addPropertiesDirectory( _strConfPath + PATH_OVERRIDE_PLUGINS );
97          }
98          catch ( FileNotFoundException e )
99          {
100             throw new LuteceInitException( "AppPropertiesService failed to load : " + e.getMessage(  ), e );
101         }
102         catch ( IOException e )
103         {
104             throw new LuteceInitException( "AppPropertiesService failed to load : " + e.getMessage(  ), e );
105         }
106     }
107 
108     /**
109      * Returns the value of a variable defined in the .properties file of the application as a String
110      *
111      * @param strProperty The variable name
112      * @return The variable value read in the properties file
113      */
114     public static String getProperty( String strProperty )
115     {
116         return _propertiesService.getProperty( strProperty );
117     }
118 
119     /**
120      * Returns the value of a variable defined in the .properties file of the application as a String
121      *
122      * @param strProperty The variable name
123      * @param strDefault The default value which is returned if no value is found for the variable in the .properties
124      *        file.
125      * @return The variable value read in the properties file
126      */
127     public static String getProperty( String strProperty, String strDefault )
128     {
129         return _propertiesService.getProperty( strProperty, strDefault );
130     }
131 
132     /**
133      * Returns the value of a variable defined in the .properties file of the application as an int
134      *
135      * @param strProperty The variable name
136      * @param nDefault The default value which is returned if no value is found for the variable in the le downloadFile
137      *        .properties. .properties file.
138      * @return The variable value read in the properties file
139      */
140     public static int getPropertyInt( String strProperty, int nDefault )
141     {
142         return _propertiesService.getPropertyInt( strProperty, nDefault );
143     }
144 
145     /**
146      * Returns the value of a variable defined in the .properties file of the application as an long
147      *
148      * @param strProperty The variable name
149      * @param lDefault The default value which is returned if no value is found for the variable in the le downloadFile
150      *        .properties. .properties file.
151      * @return The variable value read in the properties file
152      */
153     public static long getPropertyLong( String strProperty, long lDefault )
154     {
155         return _propertiesService.getPropertyLong( strProperty, lDefault );
156     }
157 
158     /**
159      * Returns the value of a variable defined in the .properties file of the application as a boolean
160      *
161      * @param strProperty The variable name
162      * @param bDefault The default value which is returned if no value is found for the variable in the le downloadFile
163      *        .properties. .properties file.
164      * @return The variable value read in the properties file
165      */
166     public static boolean getPropertyBoolean( String strProperty, boolean bDefault )
167     {
168         return _propertiesService.getPropertyBoolean( strProperty, bDefault );
169     }
170 
171     /**
172      * Reloads all the properties files
173      */
174     public static void reloadAll(  )
175     {
176         try
177         {
178             _propertiesService.reloadAll(  );
179         }
180         catch ( FileNotFoundException e )
181         {
182             AppLogService.error( e.getMessage(  ), e );
183         }
184         catch ( IOException e )
185         {
186             AppLogService.error( e.getMessage(  ), e );
187         }
188     }
189 
190     /**
191      * Reloads a given properties file
192      * @param strFilename The file name
193      */
194     public static void reload( String strFilename )
195     {
196         try
197         {
198             _propertiesService.reload( strFilename );
199         }
200         catch ( FileNotFoundException e )
201         {
202             AppLogService.error( e.getMessage(  ), e );
203         }
204         catch ( IOException e )
205         {
206             AppLogService.error( e.getMessage(  ), e );
207         }
208     }
209 
210     /**
211      * Gets properties
212      * @return All properties
213      * @since version 3.0
214      */
215     public static Properties getProperties(  )
216     {
217         // Return a copy of all properties
218         return new Properties( _propertiesService.getProperties(  ) );
219     }
220 
221     /**
222      * Gets all properties as a Map
223      * @return a Map of all properties
224      * @since version 5.0
225      */
226     public static Map<String, String> getPropertiesAsMap(  )
227     {
228         Map<String, String> res = new HashMap<String, String>(  );
229         Properties properties = _propertiesService.getProperties(  );
230 
231         // enumerate over property names to get all properties, including one which are defaults
232         Enumeration<?> names = properties.propertyNames(  );
233 
234         while ( names.hasMoreElements(  ) )
235         {
236             String name = (String) names.nextElement(  );
237             res.put( name, properties.getProperty( name ) );
238             ;
239         }
240 
241         return res;
242     }
243 
244     /**
245      * Returns a list of keys that match a given prefix.
246      *
247      * @param strPrefix the str prefix
248      * @return A list of keys that match the prefix
249      * @since version 3.0
250      */
251     public static List<String> getKeys( String strPrefix )
252     {
253         List<String> listKeys = new ArrayList<String>(  );
254         Enumeration eList = _propertiesService.getProperties(  ).keys(  );
255 
256         while ( eList.hasMoreElements(  ) )
257         {
258             String strKey = (String) eList.nextElement(  );
259 
260             if ( strKey.startsWith( strPrefix ) )
261             {
262                 listKeys.add( strKey );
263             }
264         }
265 
266         return listKeys;
267     }
268 }