1
2
3
4
5
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
17
18 public final class LifecycleTransitionResult
19 {
20
21
22
23
24
25
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
36 Method method = iface.getMethods()[0];
37
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 }