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