View Javadoc

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