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