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
35
36
37 package org.apache.pluto.portalImpl.services;
38
39 import fr.paris.lutece.plugins.jsr168.pluto.LutecePlutoConstant;
40 import fr.paris.lutece.plugins.jsr168.pluto.exception.ContainerInitLutecePlutoException;
41 import fr.paris.lutece.plugins.jsr168.pluto.xml.ServiceXML;
42 import fr.paris.lutece.plugins.jsr168.pluto.xml.ServicesXML;
43 import fr.paris.lutece.plugins.jsr168.pluto.xml.XMLFactory;
44 import fr.paris.lutece.plugins.jsr168.pluto.xml.XMLFactoryException;
45 import fr.paris.lutece.portal.service.util.AppLogService;
46 import fr.paris.lutece.portal.service.util.AppPropertiesService;
47
48 import org.apache.pluto.portalImpl.util.Properties;
49 import org.apache.pluto.util.StringUtils;
50
51 import java.util.HashMap;
52 import java.util.Iterator;
53 import java.util.LinkedList;
54 import java.util.List;
55 import java.util.Map;
56
57 import javax.servlet.ServletConfig;
58 import javax.servlet.ServletContext;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public class ServiceManager
97 {
98 private static volatile boolean _bInitialized = false;
99 final private static Map _mapServices = new HashMap( );
100 final private static List _lstServices = new LinkedList( );
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public static void init( ServletConfig aConfig ) throws Exception
115 {
116 init( aConfig, AppPropertiesService.getProperty( LutecePlutoConstant.PROPERTY_FILE_SERVICES ) );
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public static void init( ServletConfig config, String serviceConfigFile )
134 throws ContainerInitLutecePlutoException
135 {
136 if ( config == null )
137 {
138 throw new NullPointerException( "Parameter 'config' cannot be null." );
139 }
140
141 final ServletContext context = config.getServletContext( );
142
143 if ( context == null )
144 {
145 throw new NullPointerException( "config.getServletContext() cannot sennd a null value." );
146 }
147
148
149 if ( _bInitialized )
150 {
151 return;
152 }
153
154 synchronized ( ServiceManager.class )
155 {
156 if ( !_bInitialized )
157 {
158 _bInitialized = true;
159 }
160 else
161 {
162 return;
163 }
164 }
165
166 AppLogService.info( "Lutece/Pluto[ServiceManager] Loading services..." );
167
168 final ServicesXML servicesXML;
169
170 try
171 {
172 servicesXML = XMLFactory.loadServicesXML( context, serviceConfigFile );
173 }
174 catch ( XMLFactoryException e )
175 {
176 throw new ContainerInitLutecePlutoException( "ServiceManager: can't read services configuration (file '" +
177 serviceConfigFile + "').", e );
178 }
179
180 int numAll = 0;
181 int numSuccessful = 0;
182
183 for ( Iterator it = servicesXML.getServices( ).iterator( ); it.hasNext( ); )
184 {
185 final ServiceXML serviceXML = (ServiceXML) it.next( );
186
187 ++numAll;
188
189 final String serviceBaseName = serviceXML.getServiceBase( );
190
191
192 final Class serviceBase;
193
194 try
195 {
196 serviceBase = Class.forName( serviceBaseName );
197 }
198 catch ( ClassNotFoundException e )
199 {
200 AppLogService.info( "Lutece/Pluto[ServiceManager] can't find base class '" + serviceBaseName + "'." );
201
202 continue;
203 }
204
205 final String serviceImplName = serviceXML.getImplementation( );
206 final Service service;
207
208 try
209 {
210 final Class serviceImpl = Class.forName( serviceImplName );
211 service = (Service) serviceImpl.newInstance( );
212 }
213 catch ( ClassNotFoundException e )
214 {
215 AppLogService.info( "Lutece/Pluto[ServiceManager] can't find service implementation class '" +
216 serviceImplName + "'." );
217
218 continue;
219 }
220 catch ( ClassCastException e )
221 {
222 AppLogService.info( "Lutece/Pluto[ServiceManager] class '" + serviceImplName +
223 "' isn't a service (base class must be '" + Service.class.getName( ) + "')." );
224
225 continue;
226 }
227 catch ( IllegalAccessException e )
228 {
229 AppLogService.info(
230 "Lutece/Pluto[ServiceManager] no public access to empty constructor in service class '" +
231 serviceImplName + "'." );
232
233 continue;
234 }
235 catch ( InstantiationException e )
236 {
237 AppLogService.info( "Lutece/Pluto[ServiceManager] can't instanciate service class '" + serviceImplName +
238 "'." );
239
240 continue;
241 }
242
243 try
244 {
245 final Properties serviceProps = serviceXML.getProperties( );
246
247 AppLogService.info( "Lutece/Pluto[ServiceManager] " + StringUtils.nameOf( serviceBase ) +
248 " initializing..." );
249 service.init( config, serviceProps );
250 AppLogService.info( "Lutece/Pluto[ServiceManager] " + StringUtils.nameOf( serviceBase ) + " done." );
251 }
252 catch ( Exception e )
253 {
254 AppLogService.error( "Lutece/Pluto[ServiceManager] " + StringUtils.nameOf( serviceBase ) +
255 " exception occured (" + e.getMessage( ) + ").", e );
256
257 continue;
258 }
259
260 _mapServices.put( serviceBase, service );
261
262
263 _lstServices.add( 0, service );
264
265 ++numSuccessful;
266 }
267
268 if ( numSuccessful != numAll )
269 {
270 AppLogService.info( "Lutece/Pluto[ServiceManager] Services initialized (" + numSuccessful + "/" + numAll +
271 " successful)." );
272 throw new ContainerInitLutecePlutoException( "ServiceManager: Services initialized (" + numSuccessful +
273 "/" + numAll + " successful)." );
274 }
275 else
276 {
277 AppLogService.info( "Lutece/Pluto[ServiceManager] Services initialized (" + numSuccessful + "/" + numAll +
278 " successful)." );
279 }
280 }
281
282
283
284
285
286
287
288 public static void postInit( ServletConfig aConfig )
289 throws ContainerInitLutecePlutoException
290 {
291
292 if ( !_bInitialized )
293 {
294 return;
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312 int numSuccessful = 0;
313 final int numAll = _lstServices.size( );
314
315 for ( Iterator iterator = _lstServices.iterator( ); iterator.hasNext( ); )
316 {
317 final Service service = (Service) iterator.next( );
318
319 try
320 {
321 service.postInit( aConfig );
322 ++numSuccessful;
323 }
324 catch ( Exception e )
325 {
326 AppLogService.error( "Lutece/Pluto[ServiceManager] exception occured for '" +
327 service.getClass( ).getName( ) + "' postInit phase (" + e.getMessage( ) + ").", e );
328 }
329 }
330
331 if ( numSuccessful != numAll )
332 {
333 AppLogService.info( "Lutece/Pluto[ServiceManager] Services post-initialized (" + numSuccessful + "/" +
334 numAll + " successful)." );
335 throw new ContainerInitLutecePlutoException( "ServiceManager: Services post-initialized (" + numSuccessful +
336 "/" + numAll + " successful)." );
337 }
338 else
339 {
340 AppLogService.info( "Lutece/Pluto[ServiceManager] Services post-initialized (" + numSuccessful + "/" +
341 numAll + " successful)." );
342 }
343 }
344
345
346
347
348
349
350
351 public static void destroy( ServletConfig aConfig )
352 {
353
354 if ( !_bInitialized )
355 {
356 return;
357 }
358
359 synchronized ( ServiceManager.class )
360 {
361 if ( _bInitialized )
362 {
363 _bInitialized = false;
364 }
365 else
366 {
367 return;
368 }
369 }
370
371 ServletContext context = null;
372
373 if ( aConfig != null )
374 {
375 context = aConfig.getServletContext( );
376 }
377
378
379 for ( Iterator iterator = _lstServices.iterator( ); iterator.hasNext( ); )
380 {
381 Service service = (Service) iterator.next( );
382
383 try
384 {
385 service.destroy( aConfig );
386 }
387 catch ( Exception e )
388 {
389 AppLogService.error( "Lutece/Pluto[ServiceManager] Service '" + service.getClass( ).getName( ) +
390 "' couldn't be destroyed.", e );
391 }
392 }
393
394 _lstServices.clear( );
395 _mapServices.clear( );
396 }
397
398
399
400
401
402
403
404
405
406
407 public static Service getService( Class aClass )
408 {
409
410
411 return ( (Service) _mapServices.get( aClass ) );
412 }
413 }