KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > monitors > LifecycleComponentMonitor


1 package org.picocontainer.monitors;
2
3 import java.lang.reflect.Constructor JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 import org.picocontainer.ComponentMonitor;
11 import org.picocontainer.PicoException;
12
13 /**
14  * A {@link ComponentMonitor} which collects lifecycle failures
15  * and rethrows them on demand after the failures.
16  *
17  * @author Paul Hammant
18  * @author Mauro Talevi
19  */

20 public class LifecycleComponentMonitor implements ComponentMonitor {
21
22     private final ComponentMonitor delegate;
23     private final List JavaDoc lifecycleFailures = new ArrayList JavaDoc();
24
25     public LifecycleComponentMonitor(ComponentMonitor delegate) {
26         this.delegate = delegate;
27     }
28
29     public LifecycleComponentMonitor() {
30         delegate = new NullComponentMonitor();
31     }
32
33     public void instantiating(Constructor JavaDoc constructor) {
34         delegate.instantiating(constructor);
35     }
36
37     public void instantiated(Constructor JavaDoc constructor, long duration) {
38         delegate.instantiated(constructor, duration);
39     }
40
41     public void instantiated(Constructor JavaDoc constructor, Object JavaDoc instantiated, Object JavaDoc[] parameters, long duration) {
42         delegate.instantiated(constructor, instantiated, parameters, duration);
43     }
44
45     public void instantiationFailed(Constructor JavaDoc constructor, Exception JavaDoc cause) {
46         delegate.instantiationFailed(constructor, cause);
47     }
48
49     public void invoking(Method JavaDoc method, Object JavaDoc instance) {
50         delegate.invoking(method, instance);
51     }
52
53     public void invoked(Method JavaDoc method, Object JavaDoc instance, long duration) {
54         delegate.invoked(method, instance, duration);
55     }
56
57     public void invocationFailed(Method JavaDoc method, Object JavaDoc instance, Exception JavaDoc cause) {
58         delegate.invocationFailed(method, instance, cause);
59     }
60
61     public void lifecycleInvocationFailed(Method JavaDoc method, Object JavaDoc instance, RuntimeException JavaDoc cause) {
62         lifecycleFailures.add(cause);
63         delegate.lifecycleInvocationFailed(method, instance, cause);
64     }
65
66
67     public void rethrowLifecycleFailuresException() {
68         throw new LifecycleFailuresException(lifecycleFailures);
69     }
70
71     /**
72      * Subclass of {@link PicoException} that is thrown when the collected
73      * lifecycle failures need to be be collectively rethrown.
74      *
75      * @author Paul Hammant
76      * @author Mauro Talevi
77      */

78     public class LifecycleFailuresException extends PicoException {
79
80         private List JavaDoc lifecycleFailures;
81
82         public LifecycleFailuresException(List JavaDoc lifecycleFailures) {
83             this.lifecycleFailures = lifecycleFailures;
84         }
85
86         public String JavaDoc getMessage() {
87             StringBuffer JavaDoc message = new StringBuffer JavaDoc();
88             for ( Iterator JavaDoc i = lifecycleFailures.iterator(); i.hasNext(); ) {
89                 Exception JavaDoc failure = (Exception JavaDoc) i.next();
90                 message.append(failure.getMessage()).append("; ");
91             }
92             return message.toString();
93         }
94
95         public Collection JavaDoc getFailures() {
96             return lifecycleFailures;
97         }
98     }
99 }
100
Popular Tags