1 8 package org.picocontainer.defaults; 9 10 import org.picocontainer.ComponentMonitor; 11 import org.picocontainer.Disposable; 12 import org.picocontainer.Startable; 13 14 import java.lang.reflect.Method ; 15 16 25 public class DefaultLifecycleStrategy extends AbstractMonitoringLifecycleStrategy { 26 27 private static Method start, stop, dispose; 28 { 29 try { 30 start = Startable.class.getMethod("start", (Class [])null); 31 stop = Startable.class.getMethod("stop", (Class [])null); 32 dispose = Disposable.class.getMethod("dispose", (Class [])null); 33 } catch (NoSuchMethodException e) { 34 } 35 } 36 37 public DefaultLifecycleStrategy(ComponentMonitor monitor) { 38 super(monitor); 39 } 40 41 public void start(Object component) { 42 if (component != null && component instanceof Startable) { 43 long str = System.currentTimeMillis(); 44 currentMonitor().invoking(start, component); 45 try { 46 ((Startable) component).start(); 47 currentMonitor().invoked(start, component, System.currentTimeMillis() - str); 48 } catch (RuntimeException cause) { 49 currentMonitor().lifecycleInvocationFailed(start, component, cause); } 51 } 52 } 53 54 public void stop(Object component) { 55 if (component != null && component instanceof Startable) { 56 long str = System.currentTimeMillis(); 57 currentMonitor().invoking(stop, component); 58 try { 59 ((Startable) component).stop(); 60 currentMonitor().invoked(stop, component, System.currentTimeMillis() - str); 61 } catch (RuntimeException cause) { 62 currentMonitor().lifecycleInvocationFailed(stop, component, cause); } 64 } 65 } 66 67 public void dispose(Object component) { 68 if (component != null && component instanceof Disposable) { 69 long str = System.currentTimeMillis(); 70 currentMonitor().invoking(dispose, component); 71 try { 72 ((Disposable) component).dispose(); 73 currentMonitor().invoked(dispose, component, System.currentTimeMillis() - str); 74 } catch (RuntimeException cause) { 75 currentMonitor().lifecycleInvocationFailed(dispose, component, cause); } 77 } 78 } 79 80 public boolean hasLifecycle(Class type) { 81 return Startable.class.isAssignableFrom(type) || Disposable.class.isAssignableFrom(type); 82 } 83 } 84 | Popular Tags |