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