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  
  * $Id: LifecycleTransitionResult.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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  
      * The logic for processing a collection of children
 26  
      *
 27  
      * @param iface The lifecycle interface to be called
 28  
      * @param objects An iterator over all children that must also be called
 29  
      * @throws org.mule.api.lifecycle.LifecycleException if any fail
 30  
      */
 31  
     private static void processAllNoRetry(Class<? extends Initialisable> iface, 
 32  
         Iterator<? extends Initialisable> objects) throws LifecycleException
 33  
     {
 34  0
         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  0
         Method method = iface.getMethods()[0];
 41  
         // some interfaces have a single exception, others none
 42  0
         boolean hasException = method.getExceptionTypes().length > 0;
 43  0
         Class<?> exception = hasException ? method.getExceptionTypes()[0] : null;
 44  
 
 45  0
         while (objects.hasNext())
 46  
         {
 47  0
             Object target = objects.next();
 48  0
             processSingleNoRetry(target, method, exception, iface);
 49  0
         }
 50  0
     }
 51  
 
 52  
     private static void processSingleNoRetry(Object target, Method method, Class<?> exception, 
 53  
         Class<?> iface) 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);
 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  
     public static void initialiseAll(Iterator<? extends Initialisable> children) throws InitialisationException
 75  
     {
 76  
         try
 77  
         {
 78  0
             processAllNoRetry(Initialisable.class, children);
 79  
         }
 80  0
         catch (InitialisationException e)
 81  
         {
 82  0
             throw e;
 83  
         }
 84  0
         catch (LifecycleException e)
 85  
         {
 86  0
             throw new IllegalStateException("Unexpected exception: ", e);
 87  0
         }
 88  0
     }
 89  
 }