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