Coverage Report - org.mule.api.lifecycle.LifecycleTransitionResult
 
Classes in this File Line Coverage Branch Coverage Complexity
LifecycleTransitionResult
33%
9/27
40%
4/10
5.333
 
 1  
 /*
 2  
  * $Id: LifecycleTransitionResult.java 11517 2008-03-31 21:34:19Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.api.lifecycle;
 12  
 
 13  
 import org.mule.util.ClassUtils;
 14  
 
 15  
 import java.lang.reflect.InvocationTargetException;
 16  
 import java.lang.reflect.Method;
 17  
 import java.util.Iterator;
 18  
 
 19  
 /**
 20  
  * Restrict possible results - only OK or a retry based on some throwable are currently allowed.
 21  
  */
 22  0
 public final class LifecycleTransitionResult
 23  
 {
 24  
 
 25  
     /**
 26  
      * The logic for processing a collection of children
 27  
      *
 28  
      * @param iface The lifecycle interface to be called
 29  
      * @param objects An iterator over all children that must also be called
 30  
      * @throws org.mule.api.lifecycle.LifecycleException if any fail
 31  
      */
 32  
     private static void processAllNoRetry(Class iface, Iterator objects) throws LifecycleException
 33  
     {
 34  1146
         if (!iface.isAssignableFrom(Lifecycle.class))
 35  
         {
 36  0
             throw new IllegalArgumentException("Not a Lifecycle interface: " + iface);
 37  
         }
 38  
 
 39  
         // all interfaces have a single method
 40  1146
         Method method = iface.getMethods()[0];
 41  
         // some interfaces have a single exception, others none
 42  1146
         boolean hasException = method.getExceptionTypes().length > 0;
 43  1146
         Class exception = hasException ? method.getExceptionTypes()[0] : null;
 44  
 
 45  1146
         while (objects.hasNext())
 46  
         {
 47  0
             Object target = objects.next();
 48  0
             processSingleNoRetry(target, method, exception, iface);
 49  0
         }
 50  1146
     }
 51  
 
 52  
     private static void processSingleNoRetry(Object target, Method method, Class exception, Class iface)
 53  
             throws LifecycleException
 54  
     {
 55  0
         if (! iface.isAssignableFrom(target.getClass()))
 56  
         {
 57  0
             throw new IllegalArgumentException(ClassUtils.getSimpleName(target.getClass()) +
 58  
                     " is not an " + ClassUtils.getSimpleName(iface));
 59  
         }
 60  
         try
 61  
         {
 62  0
             method.invoke(target, ClassUtils.NO_ARGS);
 63  
         }
 64  0
         catch (IllegalAccessException e)
 65  
         {
 66  0
             throw (IllegalArgumentException) new IllegalArgumentException("Unsupported interface: " + iface).initCause(e);
 67  
         }
 68  0
         catch (InvocationTargetException e)
 69  
         {
 70  0
             throw (IllegalArgumentException) new IllegalArgumentException("Unsupported interface: " + iface).initCause(e);
 71  0
         }
 72  0
     }
 73  
 
 74  
     /**
 75  
      * Initialise children
 76  
      */
 77  
     public static void initialiseAll(Iterator children) throws InitialisationException
 78  
     {
 79  
         try
 80  
         {
 81  1146
             processAllNoRetry(Initialisable.class, children);
 82  
         }
 83  0
         catch (InitialisationException e)
 84  
         {
 85  0
             throw e;
 86  
         }
 87  0
         catch (LifecycleException e)
 88  
         {
 89  0
             throw (IllegalStateException) new IllegalStateException("Unexpected exception: " + e).initCause(e);
 90  1146
         }
 91  1146
     }
 92  
 
 93  
 }