Clover coverage report - picocontainer - 1.2-beta-1
Coverage timestamp: Sun May 29 2005 14:29:04 BST
file stats: LOC: 102   Methods: 7
NCLOC: 62   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultLifecycleManager.java 100% 82.6% 71.4% 83.3%
coverage coverage
 1    /*****************************************************************************
 2    * Copyright (C) PicoContainer Organization. All rights reserved. *
 3    * ------------------------------------------------------------------------- *
 4    * The software in this package is published under the terms of the BSD *
 5    * style license a copy of which has been included with this distribution in *
 6    * the LICENSE.txt file. *
 7    * *
 8    * Original code by Paul Hammant *
 9    *****************************************************************************/
 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    /**
 23    * This class implements the default lifecycle based on
 24    * <ul>
 25    * <li>{@link org.picocontainer.Startable#start()}</li>
 26    * <li>{@link org.picocontainer.Startable#stop()}</li>
 27    * <li>{@link org.picocontainer.Disposable#dispose()}</li>
 28    * </ul>
 29    *
 30    * It also allows custom lifecycle strategies to be plugged in via {@link #DefaultLifecycleManager(org.picocontainer.PicoVisitor, org.picocontainer.PicoVisitor, org.picocontainer.PicoVisitor)}.
 31    *
 32    * @author Paul Hammant
 33    * @author J&ouml;rg Schaible
 34    * @author Aslak Helles&oslash;y
 35    * @version $Revision: 1870 $
 36    */
 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  84 try {
 49  84 startMethod = Startable.class.getMethod("start", new Class[0]);
 50  84 stopMethod = Startable.class.getMethod("stop", new Class[0]);
 51  84 disposeMethod = Disposable.class.getMethod("dispose", new Class[0]);
 52    } catch (NoSuchMethodException e) {
 53    }
 54    }
 55   
 56  0 public DefaultLifecycleManager(ComponentMonitor componentMonitor) {
 57  0 this.componentMonitor = componentMonitor;
 58    }
 59   
 60  1012 public DefaultLifecycleManager() {
 61  1012 this.componentMonitor = NullComponentMonitor.getInstance();
 62    }
 63   
 64  142 public void start(PicoContainer node) {
 65  142 List startables = node.getComponentInstancesOfType(Startable.class);
 66  140 for (int i = 0; i < startables.size(); i++) {
 67  106 doMethod(startMethod ,startables.get(i));
 68    }
 69    }
 70   
 71  94 public void stop(PicoContainer node) {
 72  94 List startables = node.getComponentInstancesOfType(Startable.class);
 73  94 for (int i = startables.size() -1 ; 0 <= i; i--) {
 74  64 doMethod(stopMethod ,startables.get(i));
 75    }
 76    }
 77   
 78  92 public void dispose(PicoContainer node) {
 79  92 List disposables = node.getComponentInstancesOfType(Disposable.class);
 80  92 for (int i = disposables.size() -1 ; 0 <= i; i--) {
 81  58 doMethod(disposeMethod, disposables.get(i));
 82    }
 83    }
 84   
 85  228 protected void doMethod(Method method, Object instance) {
 86  228 componentMonitor.invoking(method, instance);
 87  228 try {
 88  228 long beginTime = System.currentTimeMillis();
 89  228 method.invoke(instance, emptyArray);
 90  228 componentMonitor.invoked(method, instance, System.currentTimeMillis() - beginTime);
 91    } catch (Exception e) {
 92  0 invocationFailed(method, instance, e);
 93    }
 94    }
 95   
 96  0 protected void invocationFailed(Method method, Object instance, Exception e) {
 97  0 componentMonitor.invocationFailed(method, instance, e);
 98  0 throw new org.picocontainer.PicoInitializationException("Method '" + method.getName()
 99    + "' failed on instance '" + instance+ "' for reason '" + e.getMessage() + "'", e);
 100    }
 101   
 102    }