1
2
3
4
5
6
7
8
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
21
22 public final class LifecycleTransitionResult
23 {
24
25
26
27
28
29
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
40 Method method = iface.getMethods()[0];
41
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 }