1 10 11 package org.picocontainer.defaults; 12 13 import org.picocontainer.Disposable; 14 import org.picocontainer.LifecycleManager; 15 import org.picocontainer.PicoContainer; 16 import org.picocontainer.Startable; 17 18 import java.io.Serializable ; 19 import java.lang.reflect.Method ; 20 import java.util.List ; 21 22 37 public class DefaultLifecycleManager implements LifecycleManager, Serializable { 38 39 private ComponentMonitor componentMonitor; 40 41 protected static Method startMethod = null; 42 protected static Method stopMethod = null; 43 protected static Method disposeMethod = null; 44 45 private static Object [] emptyArray = new Object [0]; 46 47 static { 48 try { 49 startMethod = Startable.class.getMethod("start", new Class [0]); 50 stopMethod = Startable.class.getMethod("stop", new Class [0]); 51 disposeMethod = Disposable.class.getMethod("dispose", new Class [0]); 52 } catch (NoSuchMethodException e) { 53 } 54 } 55 56 public DefaultLifecycleManager(ComponentMonitor componentMonitor) { 57 this.componentMonitor = componentMonitor; 58 } 59 60 public DefaultLifecycleManager() { 61 this.componentMonitor = NullComponentMonitor.getInstance(); 62 } 63 64 public void start(PicoContainer node) { 65 List startables = node.getComponentInstancesOfType(Startable.class); 66 for (int i = 0; i < startables.size(); i++) { 67 doMethod(startMethod ,startables.get(i)); 68 } 69 } 70 71 public void stop(PicoContainer node) { 72 List startables = node.getComponentInstancesOfType(Startable.class); 73 for (int i = startables.size() -1 ; 0 <= i; i--) { 74 doMethod(stopMethod ,startables.get(i)); 75 } 76 } 77 78 public void dispose(PicoContainer node) { 79 List disposables = node.getComponentInstancesOfType(Disposable.class); 80 for (int i = disposables.size() -1 ; 0 <= i; i--) { 81 doMethod(disposeMethod, disposables.get(i)); 82 } 83 } 84 85 protected void doMethod(Method method, Object instance) { 86 componentMonitor.invoking(method, instance); 87 try { 88 long beginTime = System.currentTimeMillis(); 89 method.invoke(instance, emptyArray); 90 componentMonitor.invoked(method, instance, System.currentTimeMillis() - beginTime); 91 } catch (Exception e) { 92 invocationFailed(method, instance, e); 93 } 94 } 95 96 protected void invocationFailed(Method method, Object instance, Exception e) { 97 componentMonitor.invocationFailed(method, instance, e); 98 throw new org.picocontainer.PicoInitializationException("Method '" + method.getName() 99 + "' failed on instance '" + instance+ "' for reason '" + e.getMessage() + "'", e); 100 } 101 102 } 103 | Popular Tags |