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.init;
35
36 import fr.paris.lutece.portal.service.admin.AdminAuthenticationService;
37 import fr.paris.lutece.portal.service.admin.AdminUserService;
38 import fr.paris.lutece.portal.service.content.ContentPostProcessorService;
39 import fr.paris.lutece.portal.service.content.ContentService;
40 import fr.paris.lutece.portal.service.daemon.AppDaemonService;
41 import fr.paris.lutece.portal.service.database.AppConnectionService;
42 import fr.paris.lutece.portal.service.datastore.CoreDataKeys;
43 import fr.paris.lutece.portal.service.datastore.DatastoreService;
44 import fr.paris.lutece.portal.service.fileimage.FileImageService;
45 import fr.paris.lutece.portal.service.filter.FilterService;
46 import fr.paris.lutece.portal.service.html.XmlTransformerCacheService;
47 import fr.paris.lutece.portal.service.i18n.I18nService;
48 import fr.paris.lutece.portal.service.mailinglist.AdminMailingListService;
49 import fr.paris.lutece.portal.service.plugin.PluginService;
50 import fr.paris.lutece.portal.service.portal.PortalService;
51 import fr.paris.lutece.portal.service.search.IndexationService;
52 import fr.paris.lutece.portal.service.security.SecurityService;
53 import fr.paris.lutece.portal.service.servlet.ServletService;
54 import fr.paris.lutece.portal.service.spring.SpringContextService;
55 import fr.paris.lutece.portal.service.template.AppTemplateService;
56 import fr.paris.lutece.portal.service.util.AppLogService;
57 import fr.paris.lutece.portal.service.util.AppPathService;
58 import fr.paris.lutece.portal.service.util.AppPropertiesService;
59 import fr.paris.lutece.util.html.HtmlTemplate;
60
61 import java.io.FileInputStream;
62 import java.io.FileWriter;
63 import java.io.IOException;
64
65 import java.text.SimpleDateFormat;
66
67 import java.util.Date;
68 import java.util.HashMap;
69 import java.util.Locale;
70 import java.util.Map;
71 import java.util.Properties;
72
73 import javax.servlet.ServletContext;
74
75
76
77
78
79
80 public final class AppInit
81 {
82 private static final String PROPERTY_AUTOINIT = "autoInit";
83 private static final String PROPERTY_INIT_WEBAPP_PROD_URL = "init.webapp.prod.url";
84 private static final String PROPERTY_SENDMAIL_SUBJECT = "portal.system.log4j.sendmail.subject";
85 private static final String PROPERTY_SITE_NAME = "lutece.name";
86 private static final String MARK_WEBAPP_HOME = "webapp_home";
87 private static final String MARK_PROD_URL = "lutece_prod_url";
88 private static final String MARK_SENDMAIL_SUBJECT = "sendmail_subject";
89 private static final String MARK_AUTOINIT = "autoinit";
90 private static final String PATH_CONFIG = "/WEB-INF/conf/";
91 private static final String FILE_PROPERTIES_CONFIG = "config.properties";
92 private static final String FILE_PROPERTIES_DATABASE = "db.properties";
93 private static final String PATH_TEMPLATES = "/WEB-INF/templates/";
94 private static final String CONFIG_PROPERTIES_TEMPLATE = "admin/system/config_properties.html";
95 private static boolean _bInitSuccessfull;
96 private static String _strLoadingFailureCause;
97 private static String _strLoadingFailureDetails;
98
99
100
101
102 private AppInit( )
103 {
104 }
105
106
107
108
109
110 public static void initServices( String strConfPath )
111 {
112 initServices( null, strConfPath, null );
113 }
114
115
116
117
118
119
120
121 public static void initServices( ServletContext context, String strConfPath, String strRealPath )
122 {
123 try
124 {
125 Thread.currentThread( ).setName( "Lutece-MainThread" );
126
127 AppLogService.preinit();
128
129 AppPropertiesService.init( strConfPath );
130
131
132 AppTemplateService.init( PATH_TEMPLATES );
133
134
135 DatastoreService.init( );
136
137 if ( strRealPath != null )
138 {
139
140
141 initProperties( strRealPath );
142 }
143
144
145 AppLogService.init( strConfPath, FILE_PROPERTIES_CONFIG );
146 AppLogService.info( "Starting LUTECE ..." );
147 AppLogService.info( "Running version " + AppInfo.getVersion( ) );
148
149
150 AppConnectionService.init( strConfPath, FILE_PROPERTIES_DATABASE, "portal" );
151 AppLogService.info( "Creating connexions pool 'portal'." );
152
153
154 AppLogService.info( "Loading context files ..." );
155 SpringContextService.init( context );
156
157
158 AppLogService.info( "Running extra startup services ..." );
159 StartUpServiceManager.init( );
160
161
162 XmlTransformerCacheService.init( );
163
164 AdminMailingListService.init( );
165
166
167 IndexationService.init( );
168
169
170 AppLogService.info( "Initializing plugins ..." );
171 PluginService.init( );
172
173
174 AppLogService.info( "Initializing plugins filters ..." );
175 FilterService.init( context );
176 AppLogService.info( "Initializing plugins servlets ..." );
177 ServletService.init( context );
178
179
180 traceContentServicesLoading( );
181
182
183 SecurityService.init( );
184
185
186 AppTemplateService.initAutoIncludes( );
187
188
189 AppDaemonService.init( );
190
191
192 AdminAuthenticationService.init( );
193
194
195 FileImageService.init( );
196
197
198 AdminUserService.init( );
199
200
201 AppLogService.info( "Running post startup services ..." );
202 PostStartUpServiceManager.init( );
203
204
205 ContentPostProcessorService.init( );
206
207 _bInitSuccessfull = true;
208
209 logStartupTime( );
210
211
212 DatastoreService.startCache( );
213 }
214 catch ( LuteceInitException e )
215 {
216 _strLoadingFailureCause = e.getMessage( );
217
218 Throwable cause = e.getCause( );
219
220 while ( cause != null )
221 {
222 _strLoadingFailureDetails = cause.getMessage( );
223 cause = cause.getCause( );
224 }
225 }
226 }
227
228
229
230
231
232 public static boolean isWebappSuccessfullyLoaded( )
233 {
234 return _bInitSuccessfull;
235 }
236
237
238
239
240
241 public static String getLoadingFailureCause( )
242 {
243 return _strLoadingFailureCause;
244 }
245
246
247
248
249
250 public static String getLoadingFailureDetails( )
251 {
252 return _strLoadingFailureDetails;
253 }
254
255
256
257
258 private static void traceContentServicesLoading( )
259 {
260 for ( ContentService cs : PortalService.getContentServicesList( ) )
261 {
262 AppLogService.info( "Content Service '" + cs.getName( ) + "' is loaded " +
263 ( cs.isCacheEnable( ) ? " [ cache enable ] " : " [ cache disable ] " ) );
264 }
265 }
266
267
268
269
270
271
272 private static void initProperties( String strRealPath )
273 {
274 Map<String, Object> model = new HashMap<String, Object>( );
275 Properties p = new Properties( );
276
277 FileInputStream fis = null;
278
279 try
280 {
281 fis = new FileInputStream( strRealPath + PATH_CONFIG + FILE_PROPERTIES_CONFIG );
282 p.load( fis );
283 }
284 catch ( Exception e )
285 {
286 AppLogService.error( e.getMessage( ), e );
287 }
288 finally
289 {
290 if ( fis != null )
291 {
292 try
293 {
294 fis.close( );
295 }
296 catch ( IOException e )
297 {
298 AppLogService.error( e.getMessage( ), e );
299 }
300 }
301 }
302
303 if ( Boolean.parseBoolean( p.getProperty( PROPERTY_AUTOINIT ) ) )
304 {
305 Object[] params = { AppPropertiesService.getProperty( PROPERTY_SITE_NAME ) };
306 String strSendMailSubject = I18nService.getLocalizedString( PROPERTY_SENDMAIL_SUBJECT, params,
307 I18nService.getDefaultLocale( ) );
308 model.put( MARK_SENDMAIL_SUBJECT, strSendMailSubject );
309 model.put( MARK_WEBAPP_HOME, AppPathService.getWebAppPath( ) );
310 model.put( MARK_PROD_URL, p.getProperty( PROPERTY_INIT_WEBAPP_PROD_URL ) );
311 model.put( MARK_AUTOINIT, Boolean.FALSE.toString( ) );
312
313 HtmlTemplate configTemplate = AppTemplateService.getTemplate( CONFIG_PROPERTIES_TEMPLATE,
314 Locale.getDefault( ), model );
315
316 AppTemplateService.resetConfiguration( );
317
318 FileWriter fw = null;
319
320 try
321 {
322 fw = new FileWriter( strRealPath + PATH_CONFIG + FILE_PROPERTIES_CONFIG );
323 fw.write( configTemplate.getHtml( ) );
324 }
325 catch ( Exception io )
326 {
327 io.printStackTrace( );
328 }
329 finally
330 {
331 if ( fw != null )
332 {
333 try
334 {
335 fw.close( );
336 }
337 catch ( IOException e )
338 {
339 AppLogService.error( e.getMessage( ), e );
340 }
341 }
342 }
343 }
344 }
345
346
347
348
349 private static void logStartupTime( )
350 {
351 String strStartupTime = SimpleDateFormat.getDateTimeInstance( ).format( new Date( ) );
352 DatastoreService.setDataValue( CoreDataKeys.KEY_STARTUP_TIME, strStartupTime );
353 }
354 }