1 package org.apache.fulcrum.yaafi.framework.component;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.avalon.framework.activity.Disposable;
23 import org.apache.avalon.framework.activity.Executable;
24 import org.apache.avalon.framework.activity.Initializable;
25 import org.apache.avalon.framework.activity.Startable;
26 import org.apache.avalon.framework.activity.Suspendable;
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.configuration.Reconfigurable;
31 import org.apache.avalon.framework.context.Context;
32 import org.apache.avalon.framework.context.ContextException;
33 import org.apache.avalon.framework.context.Contextualizable;
34 import org.apache.avalon.framework.logger.LogEnabled;
35 import org.apache.avalon.framework.logger.Logger;
36 import org.apache.avalon.framework.parameters.ParameterException;
37 import org.apache.avalon.framework.parameters.Parameterizable;
38 import org.apache.avalon.framework.parameters.Parameters;
39 import org.apache.avalon.framework.service.ServiceException;
40 import org.apache.avalon.framework.service.ServiceManager;
41 import org.apache.avalon.framework.service.Serviceable;
42 import org.apache.fulcrum.yaafi.framework.interceptor.AvalonInterceptorFactory;
43 import org.apache.fulcrum.yaafi.framework.interceptor.AvalonInterceptorService;
44 import org.apache.fulcrum.yaafi.framework.role.RoleEntry;
45 import org.apache.fulcrum.yaafi.framework.util.ReadWriteLock;
46 import org.apache.fulcrum.yaafi.framework.util.Validate;
47
48 /**
49 * This class implements a service component singleton with
50 * an arbitray lifecycle.
51 *
52 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
53 */
54
55 public class AvalonServiceComponentImpl
56 extends ServiceComponentImpl
57 {
58 /**
59 * Constructor to parse the configuration.
60 *
61 * @param roleEntry The information extracted from the role configuration file
62 * @param parentLogger the logger of the service container
63 * @param logger The logger for the service instance
64 * @param readWriteLock the read/write lock to synchronize access to services
65 */
66 public AvalonServiceComponentImpl(
67 RoleEntry roleEntry, Logger parentLogger, Logger logger, ReadWriteLock readWriteLock )
68 {
69 super( roleEntry, parentLogger, logger, readWriteLock );
70 }
71
72
73
74
75
76 /**
77 * @see org.apache.fulcrum.yaafi.framework.component.ServiceComponent#incarnate()
78 */
79 protected void incarnateInstance() throws Exception
80 {
81 this.getParentLogger().debug( "Incarnating the service " + this.getShorthand() );
82
83 if( this.getLogger() != null )
84 {
85 this.enableLogging( this.getLogger() );
86 }
87
88 if( this.getContext() != null )
89 {
90 this.contextualize( this.getContext() );
91 }
92
93 if( this.getServiceManager() != null )
94 {
95 this.service( this.getServiceManager() );
96 }
97
98 if( this.getConfiguration() != null )
99 {
100 this.configure( this.getConfiguration() );
101 }
102
103 if( this.getParamaters() != null )
104 {
105 this.parameterize( this.getParamaters() );
106 }
107
108 this.initialize();
109 this.execute();
110 this.start();
111
112
113
114
115
116
117 boolean isInterceptor = AvalonInterceptorService.class.isAssignableFrom(
118 this.getImplementationClazz()
119 );
120
121 if( (this.getRoleEntry().hasDynamicProxy()) &&
122 (isInterceptor == false ) )
123 {
124 if( this.getParentLogger().isDebugEnabled() )
125 {
126 this.getParentLogger().debug( "Creating a dynamic proxy for " + this.getShorthand() );
127 }
128
129 ReadWriteLock readWriteLock = this.getReadWriteLock();
130
131 Object proxyInstance = AvalonInterceptorFactory.create(
132 this.getName(),
133 this.getShorthand(),
134 this.getServiceManager(),
135 this.getRoleEntry().getInterceptorList(),
136 this.getRawInstance(false),
137 readWriteLock
138 );
139
140 this.setProxyInstance(proxyInstance);
141 }
142 else
143 {
144 this.getRoleEntry().setHasDynamicProxy(false);
145 }
146 }
147
148 /**
149 * @see org.apache.fulcrum.yaafi.framework.component.ServiceComponent#reconfigure()
150 */
151 public void reconfigure() throws Exception
152 {
153 Throwable lastThrowable = null;
154
155 this.getParentLogger().debug( "Reconfiguring " + this.getShorthand() );
156
157 try
158 {
159 this.suspend();
160 }
161 catch (Throwable t)
162 {
163 String msg = "Suspending the following service failed : " + this.getShorthand();
164 this.getParentLogger().error( msg, t );
165 lastThrowable = t;
166 }
167
168 try
169 {
170 if( this.getConfiguration() != null )
171 {
172 this.reconfigure( this.getConfiguration() );
173 }
174 }
175 catch (Throwable t)
176 {
177 String msg = "Reconfiguring the following service failed : " + this.getShorthand();
178 this.getParentLogger().error( msg, t );
179 lastThrowable = t;
180 }
181
182 try
183 {
184 this.resume();
185 }
186 catch (Throwable t)
187 {
188 String msg = "Resumimg the following service failed : " + this.getShorthand();
189 this.getParentLogger().error( msg, t );
190 lastThrowable = t;
191 }
192
193 if( lastThrowable != null )
194 {
195 if( lastThrowable instanceof Exception )
196 {
197 throw (Exception) lastThrowable;
198 }
199 else
200 {
201 throw new RuntimeException( lastThrowable.getMessage() );
202 }
203 }
204 }
205
206 /**
207 * Stop and dispose the service implementation.
208 *
209 * @see org.apache.fulcrum.yaafi.framework.component.ServiceComponent#decommision()
210 */
211 public void decommision() throws Exception
212 {
213 this.getParentLogger().debug( "Decommisioning the service " + this.getShorthand() );
214
215 try
216 {
217 this.stop();
218 }
219 catch (Throwable e)
220 {
221 String msg = "Stopping the following service failed : " + this.getShorthand();
222 this.getParentLogger().error( msg, e );
223 }
224
225 try
226 {
227 Object rawInstance = this.getRawInstance(false);
228
229
230
231 if( rawInstance instanceof Disposable )
232 {
233 try
234 {
235 this.getParentLogger().debug( "Disposable.dispose() for " + this.getShorthand() );
236 ((Disposable) rawInstance).dispose();
237 }
238 catch (Exception e)
239 {
240 String msg = "Disposing the following service failed : " + this.getShorthand();
241 this.getParentLogger().error(msg,e);
242 throw new RuntimeException(msg);
243 }
244 }
245 }
246 catch (Throwable e)
247 {
248 String msg = "Disposing the following service failed : " + this.getShorthand();
249 this.getParentLogger().error( msg, e );
250 }
251
252 super.decommision();
253 }
254
255
256
257
258
259 /**
260 * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger)
261 */
262 public void enableLogging(Logger logger)
263 {
264 Object rawInstance = this.getRawInstance(false);
265
266 if( rawInstance instanceof LogEnabled )
267 {
268 try
269 {
270 this.getParentLogger().debug( "LogEnabled.enableLogging() for " + this.getShorthand() );
271 ((LogEnabled) rawInstance).enableLogging(logger);
272 }
273 catch (Throwable t)
274 {
275 String msg = "LogEnable the following service failed : " + this.getShorthand();
276 this.getParentLogger().error(msg,t);
277 throw new RuntimeException(msg);
278 }
279 }
280 }
281
282 /**
283 * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
284 */
285 public void contextualize(Context context) throws ContextException
286 {
287 Object rawInstance = this.getRawInstance(false);
288
289 if( rawInstance instanceof Contextualizable )
290 {
291 try
292 {
293 this.getParentLogger().debug( "Contextualizable.contextualize() for " + this.getShorthand() );
294 ((Contextualizable) rawInstance).contextualize(context);
295 }
296 catch (ContextException e)
297 {
298 String msg = "Contextualizing the following service failed : " + this.getShorthand();
299 this.getParentLogger().error(msg,e);
300 throw e;
301 }
302 catch (Throwable t)
303 {
304 String msg = "Contextualizing the following service failed : " + this.getShorthand();
305 this.getParentLogger().error(msg,t);
306 throw new ContextException(msg,t);
307 }
308 }
309 }
310
311 /**
312 * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
313 */
314 public void service(ServiceManager serviceManager) throws ServiceException
315 {
316 Object rawInstance = this.getRawInstance(false);
317
318 if( rawInstance instanceof Serviceable )
319 {
320 try
321 {
322 this.getParentLogger().debug( "Serviceable.service() for " + this.getShorthand() );
323 ((Serviceable) rawInstance).service(serviceManager);
324 }
325 catch (ServiceException e)
326 {
327 String msg = "Servicing the following service failed : " + this.getShorthand();
328 this.getParentLogger().error(msg,e);
329 throw e;
330 }
331 catch (Throwable t)
332 {
333 String msg = "Servicing the following service failed : " + this.getShorthand();
334 this.getParentLogger().error(msg,t);
335 throw new ServiceException(this.getShorthand(),msg,t);
336 }
337 }
338 }
339
340 /**
341 * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
342 */
343 public void configure(Configuration configuration) throws ConfigurationException
344 {
345 Object rawInstance = this.getRawInstance(false);
346
347 if( rawInstance instanceof Configurable )
348 {
349 try
350 {
351 this.getParentLogger().debug( "Configurable.configure() for " + this.getShorthand() );
352 ((Configurable) rawInstance).configure(configuration);
353 }
354 catch (ConfigurationException e)
355 {
356 String msg = "Configuring the following service failed : " + this.getShorthand();
357 this.getParentLogger().error(msg,e);
358 throw e;
359 }
360 catch (Throwable t)
361 {
362 String msg = "Configuring the following service failed : " + this.getShorthand();
363 this.getParentLogger().error(msg,t);
364 throw new ConfigurationException(msg,t);
365 }
366 }
367 }
368
369 /**
370 * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
371 */
372 public void parameterize(Parameters parameters) throws ParameterException
373 {
374 Object rawInstance = this.getRawInstance(false);
375
376 if( rawInstance instanceof Parameterizable )
377 {
378 try
379 {
380 this.getParentLogger().debug( "Parameterizable.parametrize() for " + this.getShorthand() );
381 ((Parameterizable) rawInstance).parameterize(parameters);
382 }
383 catch (ParameterException e)
384 {
385 String msg = "Parameterizing the following service failed : " + this.getShorthand();
386 this.getParentLogger().error(msg,e);
387 throw e;
388 }
389 catch (Throwable t)
390 {
391 String msg = "Parameterizing the following service failed : " + this.getShorthand();
392 this.getParentLogger().error(msg,t);
393 throw new ParameterException(msg,t);
394 }
395 }
396 }
397
398 /**
399 * @see org.apache.avalon.framework.activity.Initializable#initialize()
400 */
401 public void initialize() throws Exception
402 {
403 Object rawInstance = this.getRawInstance(false);
404
405 if( rawInstance instanceof Initializable )
406 {
407 try
408 {
409 this.getParentLogger().debug( "Initializable.initialize() for " + this.getShorthand() );
410 ((Initializable) rawInstance).initialize();
411 }
412 catch (Exception e)
413 {
414 String msg = "Initializing the following service failed : " + this.getShorthand();
415 this.getParentLogger().error(msg,e);
416 throw e;
417 }
418 catch (Throwable t)
419 {
420 String msg = "Initializing the following service failed : " + this.getShorthand();
421 this.getParentLogger().error(msg,t);
422 throw new RuntimeException(msg);
423 }
424 }
425 }
426
427 /**
428 * @see org.apache.avalon.framework.activity.Executable#execute()
429 */
430 public void execute() throws Exception
431 {
432 Object rawInstance = this.getRawInstance(false);
433
434 if( rawInstance instanceof Executable )
435 {
436 try
437 {
438 this.getParentLogger().debug( "Executable.execute() for " + this.getShorthand() );
439 ((Executable) rawInstance).execute();
440 }
441 catch (Exception e)
442 {
443 String msg = "Executing the following service failed : " + this.getShorthand();
444 this.getParentLogger().error(msg,e);
445 throw e;
446 }
447 catch (Throwable t)
448 {
449 String msg = "Executing the following service failed : " + this.getShorthand();
450 this.getParentLogger().error(msg,t);
451 throw new RuntimeException(msg);
452 }
453 }
454 }
455
456 /**
457 * @see org.apache.avalon.framework.activity.Startable#start()
458 */
459 public void start() throws Exception
460 {
461 Object rawInstance = this.getRawInstance(false);
462
463 if( rawInstance instanceof Startable )
464 {
465 try
466 {
467 this.getParentLogger().debug( "Startable.start() for " + this.getShorthand() );
468 ((Startable) rawInstance).start();
469 }
470 catch (Exception e)
471 {
472 String msg = "Starting the following service failed : " + this.getShorthand();
473 this.getParentLogger().error(msg,e);
474 throw e;
475 }
476 catch (Throwable t)
477 {
478 String msg = "Starting the following service failed : " + this.getShorthand();
479 this.getParentLogger().error(msg,t);
480 throw new RuntimeException(msg);
481 }
482 }
483 }
484
485 /**
486 * @see org.apache.avalon.framework.activity.Startable#stop()
487 */
488 public void stop() throws Exception
489 {
490 Object rawInstance = this.getRawInstance(false);
491
492 if( rawInstance instanceof Startable )
493 {
494 try
495 {
496 this.getParentLogger().debug( "Startable.stop() for " + this.getShorthand() );
497 ((Startable) rawInstance).stop();
498 }
499 catch (Exception e)
500 {
501 String msg = "Stopping the following service failed : " + this.getShorthand();
502 this.getParentLogger().error(msg,e);
503 throw e;
504 }
505 catch (Throwable t)
506 {
507 String msg = "Stopping the following service failed : " + this.getShorthand();
508 this.getParentLogger().error(msg,t);
509 throw new RuntimeException(msg);
510 }
511 }
512 }
513
514 /**
515 * @see org.apache.avalon.framework.activity.Suspendable#resume()
516 */
517 public void resume()
518 {
519 Object rawInstance = this.getRawInstance(false);
520
521 if( rawInstance instanceof Suspendable )
522 {
523 try
524 {
525 this.getParentLogger().debug( "Suspendable.resume() for " + this.getShorthand() );
526 ((Suspendable) rawInstance).resume();
527 }
528 catch (Throwable t)
529 {
530 String msg = "Resuming the following service failed : " + this.getShorthand();
531 this.getParentLogger().error(msg,t);
532 throw new RuntimeException(msg);
533 }
534 }
535 }
536
537 /**
538 * @see org.apache.avalon.framework.activity.Suspendable#suspend()
539 */
540 public void suspend()
541 {
542 Object rawInstance = this.getRawInstance(false);
543
544 if( rawInstance instanceof Suspendable )
545 {
546 try
547 {
548 this.getParentLogger().debug( "Suspendable.suspend() for " + this.getShorthand() );
549 ((Suspendable) rawInstance).suspend();
550 }
551 catch (Throwable t)
552 {
553 String msg = "Suspending the following service failed : " + this.getShorthand();
554 this.getParentLogger().error(msg,t);
555 throw new RuntimeException(msg);
556 }
557 }
558 }
559
560 /**
561 * @see org.apache.avalon.framework.configuration.Reconfigurable#reconfigure(org.apache.avalon.framework.configuration.Configuration)
562 */
563 public void reconfigure(Configuration configuration) throws ConfigurationException
564 {
565 Validate.notNull( configuration, "configuration" );
566
567 Object rawInstance = this.getRawInstance(false);
568
569 if( rawInstance instanceof Reconfigurable )
570 {
571 try
572 {
573 this.getParentLogger().debug( "Reconfigurable.reconfigure() for " + this.getShorthand() );
574 ((Reconfigurable) rawInstance).reconfigure(configuration);
575 }
576 catch (Throwable t)
577 {
578 String msg = "Reconfiguring the following service failed : " + this.getShorthand();
579 this.getParentLogger().error(msg,t);
580 throw new RuntimeException(msg);
581 }
582 }
583 }
584 }