View Javadoc
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  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          if (!iface.isAssignableFrom(Lifecycle.class))
31          {
32              throw new IllegalArgumentException("Not a Lifecycle interface: " + iface);
33          }
34  
35          // all interfaces have a single method
36          Method method = iface.getMethods()[0];
37          // some interfaces have a single exception, others none
38          boolean hasException = method.getExceptionTypes().length > 0;
39          Class<?> exception = hasException ? method.getExceptionTypes()[0] : null;
40  
41          while (objects.hasNext())
42          {
43              Object target = objects.next();
44              processSingleNoRetry(target, method, exception, iface);
45          }
46      }
47  
48      private static void processSingleNoRetry(Object target, Method method, Class<?> exception, 
49          Class<?> iface) throws LifecycleException
50      {
51          if (! iface.isAssignableFrom(target.getClass()))
52          {
53              throw new IllegalArgumentException(ClassUtils.getSimpleName(target.getClass()) +
54                      " is not an " + ClassUtils.getSimpleName(iface));
55          }
56          try
57          {
58              method.invoke(target);
59          }
60          catch (IllegalAccessException e)
61          {
62              throw (IllegalArgumentException) new IllegalArgumentException("Unsupported interface: " + iface).initCause(e);
63          }
64          catch (InvocationTargetException e)
65          {
66              throw (IllegalArgumentException) new IllegalArgumentException("Unsupported interface: " + iface).initCause(e);
67          }
68      }
69  
70      public static void initialiseAll(Iterator<? extends Initialisable> children) throws InitialisationException
71      {
72          try
73          {
74              processAllNoRetry(Initialisable.class, children);
75          }
76          catch (InitialisationException e)
77          {
78              throw e;
79          }
80          catch (LifecycleException e)
81          {
82              throw new IllegalStateException("Unexpected exception: ", e);
83          }
84      }
85  }