1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util;
35
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.IOException;
39 import java.security.GeneralSecurityException;
40 import java.util.LinkedHashMap;
41 import java.util.Map;
42 import java.util.Properties;
43
44 import org.apache.commons.lang3.ArrayUtils;
45
46 import fr.paris.lutece.portal.service.security.RsaService;
47 import fr.paris.lutece.portal.service.util.AppLogService;
48 import fr.paris.lutece.portal.service.util.AppPropertiesService;
49
50
51
52
53 public class PropertiesService
54 {
55
56 private String _strRootPath;
57 private Properties _properties = new Properties( );
58 private Map<String, String> _mapPropertiesFiles = new LinkedHashMap<>( );
59
60 public final String RSA_KEY_PREFIX = "PROTECTED::RSA::";
61 private final String MESSAGE_CIPHERED_PROPERTY_SECURITY_EXCEPTION = "A ciphered property security exception occured." ;
62
63
64
65
66
67
68
69 public PropertiesService( String strRootPath )
70 {
71 _strRootPath = ( strRootPath.endsWith( "/" ) ) ? strRootPath : ( strRootPath + "/" );
72 }
73
74
75
76
77
78
79
80
81
82 public void addPropertiesFile( String strRelativePath, String strFilename )
83 {
84 String strFullPath = _strRootPath + ( ( strRelativePath.endsWith( "/" ) ) ? strRelativePath : ( strRelativePath + "/" ) ) + strFilename;
85 _mapPropertiesFiles.put( strFilename, strFullPath );
86 loadFile( strFullPath );
87 }
88
89
90
91
92
93
94
95 public void addPropertiesDirectory( String strRelativePath )
96 {
97 File directory = new File( _strRootPath + strRelativePath );
98
99 if ( directory.exists( ) )
100 {
101 File [ ] listFile = directory.listFiles( );
102
103 if ( ArrayUtils.isNotEmpty( listFile ) )
104 {
105 for ( File file : listFile )
106 {
107 if ( file.getName( ).endsWith( ".properties" ) )
108 {
109 String strFullPath = file.getAbsolutePath( );
110 _mapPropertiesFiles.put( file.getName( ), strFullPath );
111 loadFile( strFullPath );
112 }
113 }
114 }
115 }
116 }
117
118
119
120
121
122
123
124 private void loadFile( String strFullPath )
125 {
126 loadFile( strFullPath, _properties );
127 }
128
129
130
131
132
133
134
135
136
137
138
139 private void loadFile( String strFullPath, Properties props )
140 {
141 try ( FileInputStream fis = new FileInputStream( new File( strFullPath ) ) )
142 {
143 props.load( fis );
144 }
145 catch( IOException ex )
146 {
147 AppLogService.error( "Error loading property file : {}", ex.getMessage( ), ex );
148 }
149 }
150
151
152
153
154
155
156
157 public void reload( String strFilename )
158 {
159 String strFullPath = _mapPropertiesFiles.get( strFilename );
160 loadFile( strFullPath );
161 }
162
163
164
165
166
167 public void reloadAll( )
168 {
169 Properties newProperties = new Properties( );
170
171 for ( String strFullPath : _mapPropertiesFiles.values( ) )
172 {
173 loadFile( strFullPath, newProperties );
174 }
175
176 _properties = newProperties;
177 }
178
179
180
181
182
183
184
185
186 public String getProperty( String strProperty )
187 {
188 String strValue = _properties.getProperty( strProperty ) ;
189
190 if ( strValue != null && strValue.startsWith( RSA_KEY_PREFIX ) )
191 {
192 try
193 {
194 return RsaService.decryptRsa( strValue.substring( RSA_KEY_PREFIX.length( ) ) );
195 }
196 catch ( GeneralSecurityException e )
197 {
198 AppLogService.error( MESSAGE_CIPHERED_PROPERTY_SECURITY_EXCEPTION, e );
199 }
200 }
201
202 return strValue;
203 }
204
205
206
207
208
209
210
211
212
213
214 public String getProperty( String strProperty, String strDefault )
215 {
216 return _properties.getProperty( strProperty, strDefault );
217 }
218
219
220
221
222
223
224
225
226
227
228 public int getPropertyInt( String strProperty, int nDefault )
229 {
230 String strValue = AppPropertiesService.getProperty( strProperty );
231 int nValue = nDefault;
232
233 try
234 {
235 if ( strValue != null )
236 {
237 nValue = Integer.parseInt( strValue );
238 }
239 }
240 catch( NumberFormatException e )
241 {
242 AppLogService.info( e.getMessage( ), e );
243 }
244
245 return nValue;
246 }
247
248
249
250
251
252
253
254
255
256
257 public long getPropertyLong( String strProperty, long lDefault )
258 {
259 String strValue = AppPropertiesService.getProperty( strProperty );
260 long lValue = lDefault;
261
262 try
263 {
264 if ( strValue != null )
265 {
266 lValue = Long.parseLong( strValue );
267 }
268 }
269 catch( NumberFormatException e )
270 {
271 AppLogService.info( e.getMessage( ), e );
272 }
273
274 return lValue;
275 }
276
277
278
279
280
281
282
283
284
285
286 public boolean getPropertyBoolean( String strProperty, boolean bDefault )
287 {
288 String strValue = AppPropertiesService.getProperty( strProperty );
289 boolean bValue = bDefault;
290
291 if ( strValue != null )
292 {
293 bValue = strValue.equalsIgnoreCase( "true" );
294 }
295
296 return bValue;
297 }
298
299
300
301
302
303
304 public Properties getProperties( )
305 {
306 return _properties;
307 }
308 }