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.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
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
70
71 private AppPropertiesService( )
72 {
73 }
74
75
76
77
78
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
110
111
112
113
114 public static String getProperty( String strProperty )
115 {
116 return _propertiesService.getProperty( strProperty );
117 }
118
119
120
121
122
123
124
125
126
127 public static String getProperty( String strProperty, String strDefault )
128 {
129 return _propertiesService.getProperty( strProperty, strDefault );
130 }
131
132
133
134
135
136
137
138
139
140 public static int getPropertyInt( String strProperty, int nDefault )
141 {
142 return _propertiesService.getPropertyInt( strProperty, nDefault );
143 }
144
145
146
147
148
149
150
151
152
153 public static long getPropertyLong( String strProperty, long lDefault )
154 {
155 return _propertiesService.getPropertyLong( strProperty, lDefault );
156 }
157
158
159
160
161
162
163
164
165
166 public static boolean getPropertyBoolean( String strProperty, boolean bDefault )
167 {
168 return _propertiesService.getPropertyBoolean( strProperty, bDefault );
169 }
170
171
172
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
192
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
212
213
214
215 public static Properties getProperties( )
216 {
217
218 return new Properties( _propertiesService.getProperties( ) );
219 }
220
221
222
223
224
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
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
246
247
248
249
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 }