1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.inject.api; |
18 | |
|
19 | |
import java.lang.annotation.Annotation; |
20 | |
import java.lang.reflect.Method; |
21 | |
|
22 | |
import javax.annotation.PostConstruct; |
23 | |
import javax.annotation.PreDestroy; |
24 | |
|
25 | |
import org.apache.commons.inject.api.bind.IBinder; |
26 | |
import org.apache.commons.inject.api.bind.IModule; |
27 | |
import org.apache.commons.inject.api.bind.IBinder.IInjectionListener; |
28 | |
import org.apache.commons.inject.impl.DefaultLifecycleController; |
29 | |
import org.apache.commons.inject.util.Exceptions; |
30 | |
|
31 | 14 | public class PostConstructModule implements IModule { |
32 | |
private ILifecycleController controller; |
33 | |
|
34 | |
public ILifecycleController getLifecycleController() { |
35 | 2 | if (controller == null) { |
36 | 2 | controller = new DefaultLifecycleController(); |
37 | |
} |
38 | 2 | return controller; |
39 | |
} |
40 | |
|
41 | |
public void setLifecycleController(ILifecycleController pController) { |
42 | 0 | controller = pController; |
43 | 0 | } |
44 | |
|
45 | |
@Override |
46 | |
public void configure(IBinder pBinder) { |
47 | 2 | pBinder.add(new IInjectionListener() { |
48 | |
@Override |
49 | |
public void initialized(IKey<?> pKey, Object pObject) { |
50 | 7 | final ILifecycleListener listener = getListenerFor(pObject); |
51 | 7 | if (listener != null && listener != controller) { |
52 | 5 | controller.add(listener); |
53 | |
} |
54 | 7 | } |
55 | |
}); |
56 | 2 | pBinder.bind(ILifecycleController.class).toInstance(controller); |
57 | 2 | } |
58 | |
|
59 | |
protected ILifecycleListener getListenerFor(final Object pObject) { |
60 | 7 | if (pObject instanceof ILifecycleListener) { |
61 | 3 | return (ILifecycleListener) pObject; |
62 | |
} |
63 | 4 | final Method postConstructMethod = findMethod(PostConstruct.class, pObject.getClass()); |
64 | 4 | final Method preDestroyMethod = findMethod(PreDestroy.class, pObject.getClass()); |
65 | 4 | if (postConstructMethod == null && preDestroyMethod == null) { |
66 | 0 | return null; |
67 | |
} |
68 | 4 | return new ILifecycleListener(){ |
69 | |
@Override |
70 | |
public void start() { |
71 | 4 | invoke(postConstructMethod, pObject); |
72 | 4 | } |
73 | |
|
74 | |
@Override |
75 | |
public void shutdown() { |
76 | 4 | invoke(preDestroyMethod, pObject); |
77 | 4 | } |
78 | |
|
79 | |
private void invoke(Method pMethod, Object pObject) { |
80 | 8 | if (pMethod == null) { |
81 | 0 | return; |
82 | |
} |
83 | |
try { |
84 | 8 | if (!pMethod.isAccessible()) { |
85 | 8 | pMethod.setAccessible(true); |
86 | |
} |
87 | 8 | pMethod.invoke(pObject); |
88 | 0 | } catch (Throwable t) { |
89 | 0 | throw Exceptions.show(t); |
90 | 8 | } |
91 | 8 | } |
92 | |
}; |
93 | |
} |
94 | |
|
95 | |
private Method findMethod(Class<? extends Annotation> pAnnotationClass, Class<?> pType) { |
96 | 8 | final Method[] methods = pType.getDeclaredMethods(); |
97 | 12 | for (Method method : methods) { |
98 | 12 | if (method.isAnnotationPresent(pAnnotationClass)) { |
99 | 8 | return method; |
100 | |
} |
101 | |
} |
102 | 0 | return null; |
103 | |
} |
104 | |
} |