1 | |
package org.jtheque.core.spring.processors; |
2 | |
|
3 | |
import org.jtheque.core.managers.module.annotations.Module; |
4 | |
import org.jtheque.core.managers.module.annotations.Plug; |
5 | |
import org.jtheque.core.managers.module.annotations.PrePlug; |
6 | |
import org.jtheque.core.managers.module.annotations.UnPlug; |
7 | |
import org.jtheque.core.managers.module.beans.EmptyBeanMethod; |
8 | |
import org.jtheque.core.managers.module.beans.ModuleContainer; |
9 | |
import org.jtheque.core.managers.module.beans.ReflectionBeanMethod; |
10 | |
import org.jtheque.core.managers.module.loaders.ModuleLoader; |
11 | |
import org.springframework.beans.factory.config.BeanPostProcessor; |
12 | |
|
13 | |
import java.lang.reflect.AnnotatedElement; |
14 | |
import java.lang.reflect.Method; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public final class ModulePostProcessor implements BeanPostProcessor { |
36 | |
@Override |
37 | |
public Object postProcessBeforeInitialization(Object bean, String beanName) { |
38 | 0 | return bean; |
39 | |
} |
40 | |
|
41 | |
@Override |
42 | |
public Object postProcessAfterInitialization(Object bean, String beanName) { |
43 | 0 | Class<?> c = bean.getClass(); |
44 | |
|
45 | 0 | if (isModule(c)) { |
46 | 0 | registerModule(bean, beanName); |
47 | |
} |
48 | |
|
49 | 0 | return bean; |
50 | |
} |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private static boolean isModule(AnnotatedElement c) { |
59 | 0 | return c.getAnnotation(Module.class) != null; |
60 | |
} |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
private static void registerModule(Object bean, String beanName) { |
69 | 0 | ModuleContainer module = new ModuleContainer( |
70 | |
beanName, |
71 | |
bean.getClass().getAnnotation(Module.class) |
72 | |
); |
73 | |
|
74 | 0 | searchForAnnotatedMethods(bean, beanName, module); |
75 | |
|
76 | 0 | completeMethods(module); |
77 | |
|
78 | 0 | ModuleLoader.getModules().add(module); |
79 | 0 | } |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
private static void searchForAnnotatedMethods(Object bean, String beanName, ModuleContainer module) { |
89 | 0 | searchMethod(bean.getClass().getSuperclass(), beanName, module); |
90 | 0 | searchMethod(bean.getClass(), beanName, module); |
91 | 0 | } |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
private static void searchMethod(Class<?> beanClass, String beanName, ModuleContainer module) { |
101 | 0 | for (Method m : beanClass.getDeclaredMethods()) { |
102 | 0 | if (m.getAnnotation(UnPlug.class) != null) { |
103 | 0 | module.setUnPlugMethod(new ReflectionBeanMethod(beanName, m.getName())); |
104 | 0 | } else if (m.getAnnotation(Plug.class) != null) { |
105 | 0 | module.setPlugMethod(new ReflectionBeanMethod(beanName, m.getName())); |
106 | 0 | } else if (m.getAnnotation(PrePlug.class) != null) { |
107 | 0 | module.setPrePlugMethod(new ReflectionBeanMethod(beanName, m.getName())); |
108 | |
} |
109 | |
} |
110 | 0 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
private static void completeMethods(ModuleContainer module) { |
118 | 0 | if (module.getUnPlugMethod() == null) { |
119 | 0 | module.setUnPlugMethod(new EmptyBeanMethod()); |
120 | |
} |
121 | |
|
122 | 0 | if (module.getPlugMethod() == null) { |
123 | 0 | module.setPlugMethod(new EmptyBeanMethod()); |
124 | |
} |
125 | |
|
126 | 0 | if (module.getPrePlugMethod() == null) { |
127 | 0 | module.setPrePlugMethod(new EmptyBeanMethod()); |
128 | |
} |
129 | 0 | } |
130 | |
} |