1 package org.picocontainer.monitors; 2 3 import java.lang.reflect.Constructor ; 4 import java.lang.reflect.Method ; 5 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 import org.picocontainer.ComponentMonitor; 11 import org.picocontainer.PicoException; 12 13 20 public class LifecycleComponentMonitor implements ComponentMonitor { 21 22 private final ComponentMonitor delegate; 23 private final List lifecycleFailures = new ArrayList (); 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 constructor) { 34 delegate.instantiating(constructor); 35 } 36 37 public void instantiated(Constructor constructor, long duration) { 38 delegate.instantiated(constructor, duration); 39 } 40 41 public void instantiated(Constructor constructor, Object instantiated, Object [] parameters, long duration) { 42 delegate.instantiated(constructor, instantiated, parameters, duration); 43 } 44 45 public void instantiationFailed(Constructor constructor, Exception cause) { 46 delegate.instantiationFailed(constructor, cause); 47 } 48 49 public void invoking(Method method, Object instance) { 50 delegate.invoking(method, instance); 51 } 52 53 public void invoked(Method method, Object instance, long duration) { 54 delegate.invoked(method, instance, duration); 55 } 56 57 public void invocationFailed(Method method, Object instance, Exception cause) { 58 delegate.invocationFailed(method, instance, cause); 59 } 60 61 public void lifecycleInvocationFailed(Method method, Object instance, RuntimeException 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 78 public class LifecycleFailuresException extends PicoException { 79 80 private List lifecycleFailures; 81 82 public LifecycleFailuresException(List lifecycleFailures) { 83 this.lifecycleFailures = lifecycleFailures; 84 } 85 86 public String getMessage() { 87 StringBuffer message = new StringBuffer (); 88 for ( Iterator i = lifecycleFailures.iterator(); i.hasNext(); ) { 89 Exception failure = (Exception ) i.next(); 90 message.append(failure.getMessage()).append("; "); 91 } 92 return message.toString(); 93 } 94 95 public Collection getFailures() { 96 return lifecycleFailures; 97 } 98 } 99 } 100 | Popular Tags |