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.portal.service.util;
35
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Optional;
41 import java.util.Properties;
42 import java.util.stream.Collectors;
43 import java.util.stream.StreamSupport;
44
45 import org.eclipse.microprofile.config.Config;
46 import org.eclipse.microprofile.config.ConfigProvider;
47 import org.eclipse.microprofile.config.spi.ConfigSource;
48
49 import fr.paris.lutece.util.AppInitPropertiesService;
50
51 /**
52 * this class provides management services for properties files
53 */
54 public final class AppPropertiesService
55 {
56
57 private static Config _config;
58 /**
59 * Private constructor
60 */
61 private AppPropertiesService( )
62 {
63 }
64
65 /**
66 * Initializes the service
67 *
68 * @param strConfPath
69 * The configuration path
70 */
71 public static void init( String strConfPath )
72 {
73 AppInitPropertiesService.init(strConfPath);
74
75 try {
76 _config= ConfigProvider.getConfig();
77
78 } catch (Exception e) {
79 AppLogService.error(e.getMessage( ), e);
80 throw e;
81 }
82
83 }
84
85 /**
86 * Returns the value of a variable defined in the .properties file of the application as a String
87 *
88 * @param strProperty
89 * The variable name
90 * @return The variable value read in the properties file
91 */
92 public static String getProperty( String strProperty )
93 {
94 return _config.getOptionalValue(strProperty, String.class).orElse(null);
95 }
96
97 /**
98 * Returns the value of a variable defined in the .properties file of the application as a String
99 *
100 * @param strProperty
101 * The variable name
102 * @param strDefault
103 * The default value which is returned if no value is found for the variable in the .properties file.
104 * @return The variable value read in the properties file
105 */
106 public static String getProperty( String strProperty, String strDefault )
107 {
108 return _config.getOptionalValue(strProperty, String.class).orElse( strDefault );
109 }
110
111 /**
112 * Returns the value of a variable defined in the .properties file of the application as an int
113 *
114 * @param strProperty
115 * The variable name
116 * @param nDefault
117 * The default value which is returned if no value is found for the variable in the le downloadFile .properties. .properties file.
118 * @return The variable value read in the properties file
119 */
120 public static int getPropertyInt( String strProperty, int nDefault )
121 {
122 return _config.getOptionalValue(strProperty, Integer.class).orElse( nDefault );
123 }
124
125 /**
126 * Returns the value of a variable defined in the .properties file of the application as an long
127 *
128 * @param strProperty
129 * The variable name
130 * @param lDefault
131 * The default value which is returned if no value is found for the variable in the le downloadFile .properties. .properties file.
132 * @return The variable value read in the properties file
133 */
134 public static long getPropertyLong( String strProperty, long lDefault )
135 {
136 return _config.getOptionalValue(strProperty, Long.class).orElse( lDefault );
137 }
138
139 /**
140 * Returns the value of a variable defined in the .properties file of the application as a boolean
141 *
142 * @param strProperty
143 * The variable name
144 * @param bDefault
145 * The default value which is returned if no value is found for the variable in the le downloadFile .properties. .properties file.
146 * @return The variable value read in the properties file
147 */
148 public static boolean getPropertyBoolean( String strProperty, boolean bDefault )
149 {
150 return _config.getOptionalValue(strProperty, Boolean.class).orElse( bDefault );
151 }
152
153 /**
154 * Return the resolved property value with the specified type for the specified property name from the underlying
155 * {@linkplain ConfigSource configuration sources}.
156 * <p>
157 * The configuration value is not guaranteed to be cached by the implementation, and may be expensive to compute;
158 * therefore, if the returned value is intended to be frequently used, callers should consider storing rather than
159 * recomputing it.
160 * <p>
161 * If this method is used very often then consider to locally store the configured value.
162 *
163 * @param <T>
164 * The property type
165 * @param propertyName
166 * The configuration property name
167 * @param propertyType
168 * The type into which the resolved property value should be converted
169 * @return The resolved property value as an {@code Optional} wrapping the requested type
170 *
171 * @throws IllegalArgumentException
172 * if the property cannot be converted to the specified type
173 */
174 public <T> Optional<T> getOptionalValue(String name, Class<T> aClass)
175 {
176 return _config.getOptionalValue(name, aClass );
177 }
178 /**
179 * Reloads all the properties files
180 */
181 @Deprecated
182 public static void reloadAll( )
183 {
184 AppInitPropertiesService.reloadAll( );
185 }
186
187 /**
188 * Reloads a given properties file
189 *
190 * @param strFilename
191 * The file name
192 */
193 @Deprecated
194 public static void reload( String strFilename )
195 {
196 AppInitPropertiesService.reload( strFilename );
197 }
198
199 /**
200 * Gets properties
201 *
202 * @return All properties
203 * @since version 3.0
204 */
205 public static Properties getProperties( )
206 {
207 Properties properties = new Properties( );
208 _config.getPropertyNames().forEach(x-> properties.put(x, _config.getOptionalValue(x, String.class).orElse("")));
209
210 return properties;
211
212 }
213
214 /**
215 * Gets all properties as a Map
216 *
217 * @return a Map of all properties
218 * @since version 5.0
219 */
220 public static Map<String, String> getPropertiesAsMap( )
221 {
222
223 Map<String,String> mapProperties=new HashMap<String, String>();
224 _config.getPropertyNames().forEach(x-> mapProperties.put(x, _config.getOptionalValue(x, String.class).orElse("")));
225
226
227 return mapProperties;
228 }
229
230 /**
231 * Returns a list of keys that match a given prefix.
232 *
233 * @param strPrefix
234 * the str prefix
235 * @return A list of keys that match the prefix
236 * @since version 3.0
237 */
238 public static List<String> getKeys( String strPrefix )
239 {
240 return StreamSupport.stream(_config.getPropertyNames().spliterator(), false).filter(key -> key.startsWith(strPrefix)).collect(Collectors.toList());
241 }
242 }