1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ksb.messaging;
17
18 import org.kuali.rice.ksb.api.messaging.AsynchronousCall;
19 import org.kuali.rice.ksb.api.messaging.AsynchronousCallback;
20
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.xml.namespace.QName;
28
29 public class TestCallback implements AsynchronousCallback {
30
31
32
33 private static final long serialVersionUID = 8558495197148582373L;
34
35 public static boolean callbackCalled;
36
37 public static List<Long> CURRENT_MILLIS_WHEN_CALLED;
38
39 public static int NUMBER_CALL_BACKS = 0;
40
41 public static Map<QName, Integer> SERVICE_CALL_COUNT_TRACKED = new HashMap<QName, Integer>();
42 public static Map<QName, List<AsynchronousCall>> SERVICES_CALLS_TRACKED = new HashMap<QName, List<AsynchronousCall>>();
43
44 static {
45 CURRENT_MILLIS_WHEN_CALLED = new ArrayList<Long>();
46 }
47
48 private int numberCallbacks = 0;
49
50 public static boolean isCallbackCalled() {
51 return callbackCalled;
52 }
53
54 public static void setCallbackCalled(boolean callbackCalled) {
55 TestCallback.callbackCalled = callbackCalled;
56 }
57
58 public synchronized void callback(Serializable returnObject, AsynchronousCall methodCall) {
59 CURRENT_MILLIS_WHEN_CALLED.add(System.currentTimeMillis());
60 NUMBER_CALL_BACKS++;
61 setCallbackCalled(true);
62 this.numberCallbacks++;
63 QName serviceName = methodCall.getServiceConfiguration().getServiceName();
64 Integer callCount = SERVICE_CALL_COUNT_TRACKED.get(serviceName);
65 if (callCount == null) {
66 SERVICE_CALL_COUNT_TRACKED.put(methodCall.getServiceConfiguration().getServiceName(), 1);
67 } else {
68 SERVICE_CALL_COUNT_TRACKED.put(methodCall.getServiceConfiguration().getServiceName(), callCount + 1);
69 }
70
71 List<AsynchronousCall> serviceCallsTracked = SERVICES_CALLS_TRACKED.get(serviceName);
72 if (serviceCallsTracked == null) {
73 serviceCallsTracked = new ArrayList<AsynchronousCall>();
74 SERVICES_CALLS_TRACKED.put(serviceName, serviceCallsTracked);
75 }
76 serviceCallsTracked.add(methodCall);
77
78 System.out.println("!!!Callback called number callbacks " + this.numberCallbacks);
79 }
80
81 public static void clearCallbacks() {
82 NUMBER_CALL_BACKS = 0;
83 SERVICE_CALL_COUNT_TRACKED = new HashMap<QName, Integer>();
84 }
85
86
87
88
89
90
91
92
93
94
95
96 public void pauseUntilNumberCallbacksUsingStaticCounter(int callbacks, QName serviceName) {
97 int numPauses = 0;
98 while (true) {
99 synchronized (this.getClass()) {
100 if (serviceName == null) {
101 if (NUMBER_CALL_BACKS >= callbacks) {
102 System.out.println("!!!Returning number callback met");
103 return;
104 }
105 } else {
106 if (SERVICE_CALL_COUNT_TRACKED.get(serviceName) != null && SERVICE_CALL_COUNT_TRACKED.get(serviceName) >= callbacks) {
107 System.out.println("!!!Returning number callback met for service " + serviceName);
108 return;
109 }
110
111 for (Map.Entry<QName, Integer> serviceCall : SERVICE_CALL_COUNT_TRACKED.entrySet()) {
112 if (serviceCall.getKey().getLocalPart().lastIndexOf(serviceName.getLocalPart()) > -1 && serviceCall.getKey().getNamespaceURI().equals(serviceName.getNamespaceURI()) && serviceCall.getValue() >= callbacks) {
113 System.out.println("!!!Returning number callback met for service " + serviceName);
114 return;
115 }
116 }
117 }
118 }
119 if (numPauses > 60 * 1) {
120 return;
121 }
122 try {
123 numPauses++;
124 System.out.println("!!!Test callback pausing for 1 second");
125 Thread.sleep(1000);
126 } catch (InterruptedException e) {
127
128 }
129 }
130 }
131 }