4
4
import java .lang .reflect .Method ;
5
5
import java .lang .reflect .ParameterizedType ;
6
6
import java .lang .reflect .Type ;
7
+ import java .util .ArrayList ;
8
+ import java .util .List ;
7
9
import java .util .Map ;
8
10
import java .util .Map .Entry ;
9
11
@@ -19,6 +21,7 @@ public class MediatorImpl implements Mediator {
19
21
20
22
/**
21
23
* Initializes a new instance of Mediator
24
+ *
22
25
* @param ctx Application context of Spring
23
26
*/
24
27
public MediatorImpl (ApplicationContext ctx ) {
@@ -29,19 +32,45 @@ public MediatorImpl(ApplicationContext ctx) {
29
32
public <T > Response <T > request (Request <T > request ) {
30
33
Response <T > response = new Response <>();
31
34
try {
32
- MediatorPlan <T > plan = new MediatorPlan <>(RequestHandler .class , "handle" , request .getClass (), ctx );
35
+ MediatorPlanRequest <T > plan = new MediatorPlanRequest <>(RequestHandler .class , "handle" , request .getClass (),
36
+ ctx );
33
37
response .data = plan .invoke (request );
34
38
} catch (Exception e ) {
35
39
response .exception = e ;
36
40
}
37
41
return response ;
38
42
}
39
43
40
- class MediatorPlan <T > {
44
+ @ Override
45
+ public Response <Void > notify (Notification notification ) {
46
+ Response <Void > response = new Response <>();
47
+ List <NotificationHandler <Notification >> handlers = MediatorPlanNotify .getInstances (ctx ,
48
+ notification .getClass ());
49
+ List <Exception > exceptions = null ;
50
+
51
+ for (NotificationHandler <Notification > handler : handlers ) {
52
+ try {
53
+ handler .handle (notification );
54
+ } catch (Exception ex ) {
55
+ if (exceptions == null )
56
+ exceptions = new ArrayList <>();
57
+
58
+ exceptions .add (ex );
59
+ }
60
+ }
61
+
62
+ if (exceptions != null ) {
63
+ response .exception = new AggregateException (exceptions );
64
+ }
65
+
66
+ return response ;
67
+ }
68
+
69
+ class MediatorPlanRequest <T > {
41
70
Method handleMethod ;
42
71
Object handlerInstanceBuilder ;
43
72
44
- public MediatorPlan (Class <?> handlerType , String handlerMethodName , Class <?> messageType ,
73
+ public MediatorPlanRequest (Class <?> handlerType , String handlerMethodName , Class <?> messageType ,
45
74
ApplicationContext context ) throws NoSuchMethodException , SecurityException , ClassNotFoundException {
46
75
handlerInstanceBuilder = getBean (handlerType , messageType , context );
47
76
handleMethod = handlerInstanceBuilder .getClass ().getDeclaredMethod (handlerMethodName , messageType );
@@ -70,4 +99,24 @@ public T invoke(Request<T> request)
70
99
}
71
100
}
72
101
102
+ static class MediatorPlanNotify {
103
+ public static List <NotificationHandler <Notification >> getInstances (ApplicationContext ctx ,
104
+ Class <?> messageType ) {
105
+ List <NotificationHandler <Notification >> instances = new ArrayList <>();
106
+
107
+ Map <String , ?> beans = ctx .getBeansOfType (NotificationHandler .class );
108
+ for (Entry <String , ?> entry : beans .entrySet ()) {
109
+ Class <?> clazz = entry .getValue ().getClass ();
110
+ Type [] interfaces = clazz .getGenericInterfaces ();
111
+ for (Type interace : interfaces ) {
112
+ Type parameterType = ((ParameterizedType ) interace ).getActualTypeArguments ()[0 ];
113
+ if (parameterType .equals (messageType )) {
114
+ instances .add ((NotificationHandler <Notification >) entry .getValue ());
115
+ }
116
+ }
117
+ }
118
+
119
+ return instances ;
120
+ }
121
+ }
73
122
}
0 commit comments