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