View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  	 * sometimes it's more convenient to use a non static counter above when
88  	 * doing in memory queueing. Other times when doing persistent async
89  	 * messaging a static counter is needed. Everything could be converted to
90  	 * this method if the tests using the above method clear the static count
91  	 * before putting testing against callbacks.
92  	 * 
93  	 * @param callbacks
94  	 * @param millisDelay
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 						// attributes will not be an exact match.
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 			    // nothing to do
128 			}
129 		}
130 	}
131 }