View Javadoc
1   /*
2    * Copyright (c) 2002-2025, 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  
36  import java.util.ArrayList;
37  import java.util.Enumeration;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  import java.util.Properties;
42  
43  import fr.paris.lutece.portal.service.util.AppPathService;
44  
45  /**
46   * This class provides management services for properties files
47   */
48  public final class AppInitPropertiesService
49  {
50      private static final String FILE_PROPERTIES_CONFIG = "config.properties";
51      private static final String FILE_PROPERTIES_DATABASE = "db.properties";
52      private static final String FILE_PROPERTIES_LUTECE = "lutece.properties";
53      private static final String FILE_PROPERTIES_SEARCH = "search.properties";
54      private static final String FILE_PROPERTIES_DAEMONS = "daemons.properties";
55      private static final String FILE_PROPERTIES_CACHES = "caches.properties";
56      private static final String FILE_PROPERTIES_EDITORS = "editors.properties";
57      private static final String PATH_PLUGINS = "plugins/";
58      private static final String PATH_THEMES="themes/";
59      private static final String PATH_OVERRIDE_CORE = "override/";
60      private static final String PATH_OVERRIDE_PLUGINS = "override/plugins";
61      private static PropertiesService _propertiesService;
62  
63      /**
64       * Private constructor
65       */
66      private AppInitPropertiesService( )
67      {
68      }
69  
70      /**
71       * Initializes the service
72       * 
73       * @param strConfPath
74       *            The configuration path
75       */
76      public static void init( String strConfPath )
77      {
78          String confPath = strConfPath;
79          _propertiesService = new PropertiesService( AppPathService.getWebAppPath( ) );
80  
81          _propertiesService.addPropertiesFile( confPath, FILE_PROPERTIES_CONFIG );
82          _propertiesService.addPropertiesFile( confPath, FILE_PROPERTIES_DATABASE );
83          _propertiesService.addPropertiesFile( confPath, FILE_PROPERTIES_LUTECE );
84          _propertiesService.addPropertiesFile( confPath, FILE_PROPERTIES_SEARCH );
85          _propertiesService.addPropertiesFile( confPath, FILE_PROPERTIES_DAEMONS );
86          _propertiesService.addPropertiesFile( confPath, FILE_PROPERTIES_CACHES );
87          _propertiesService.addPropertiesFile( confPath, FILE_PROPERTIES_EDITORS );
88          _propertiesService.addPropertiesDirectory( confPath + PATH_PLUGINS );
89          _propertiesService.addPropertiesDirectory( confPath + PATH_THEMES );
90          _propertiesService.addPropertiesDirectory( confPath + PATH_OVERRIDE_CORE );
91          _propertiesService.addPropertiesDirectory( confPath + PATH_OVERRIDE_PLUGINS );
92          
93      }
94  
95      /**
96       * Returns the value of a variable defined in the .properties file of the application as a String
97       *
98       * @param strProperty
99       *            The variable name
100      * @return The variable value read in the properties file
101      */
102     public static String getProperty( String strProperty )
103     {
104         return _propertiesService.getProperty( strProperty );
105     }
106     /**
107      * Reloads all the properties files
108      */
109     public static void reloadAll( )
110     {
111         _propertiesService.reloadAll( );
112     }
113 
114     /**
115      * Reloads a given properties file
116      * 
117      * @param strFilename
118      *            The file name
119      */
120     public static void reload( String strFilename )
121     {
122         _propertiesService.reload( strFilename );
123     }
124 
125     /**
126      * Gets properties
127      * 
128      * @return All properties
129      * @since version 3.0
130      */
131     public static Properties getProperties( )
132     {
133         // Return a copy of all properties
134         return new Properties( _propertiesService.getProperties( ) );
135     }
136     /**
137      * Get all properties As map
138      * @return Properties as map
139      */
140     public static Map<String, String> getPropertiesAsMap( )
141     {
142         Map<String, String> res = new HashMap<>( );
143         Properties properties = _propertiesService.getProperties( );
144 
145         // enumerate over property names to get all properties, including one which are defaults
146         Enumeration<?> names = properties.propertyNames( );
147 
148         while ( names.hasMoreElements( ) )
149         {
150             String name = (String) names.nextElement( );
151             res.put( name, properties.getProperty( name ) );
152         }
153 
154         return res;
155     }
156     /**
157      * Returns a list of keys that match a given prefix.
158      *
159      * @param strPrefix
160      *            the str prefix
161      * @return A list of keys that match the prefix
162      * @since version 7.0
163      */
164     public static List<String> getKeys( String strPrefix )
165     {
166         List<String> listKeys = new ArrayList<>( );
167         Enumeration eList = _propertiesService.getProperties( ).keys( );
168 
169         while ( eList.hasMoreElements( ) )
170         {
171             String strKey = (String) eList.nextElement( );
172 
173             if ( strKey.startsWith( strPrefix ) )
174             {
175                 listKeys.add( strKey );
176             }
177         }
178 
179         return listKeys;
180     }
181 }