001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *//**
016: *
017: */
018: package edu.iu.uis.eden.messaging;
019:
020: import java.io.Serializable;
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Map;
025:
026: import javax.xml.namespace.QName;
027:
028: public class TestCallback implements AsynchronousCallback {
029: /**
030: *
031: */
032: private static final long serialVersionUID = 8558495197148582373L;
033:
034: public static boolean callbackCalled;
035:
036: public static List<Long> CURRENT_MILLIS_WHEN_CALLED;
037:
038: public static int NUMBER_CALL_BACKS = 0;
039:
040: public static Map<QName, Integer> SERVICE_CALL_COUNT_TRACKED = new HashMap<QName, Integer>();
041: public static Map<QName, List<AsynchronousCall>> SERVICES_CALLS_TRACKED = new HashMap<QName, List<AsynchronousCall>>();
042:
043: static {
044: CURRENT_MILLIS_WHEN_CALLED = new ArrayList<Long>();
045: }
046:
047: private int numberCallbacks = 0;
048:
049: public static boolean isCallbackCalled() {
050: return callbackCalled;
051: }
052:
053: public static void setCallbackCalled(boolean callbackCalled) {
054: TestCallback.callbackCalled = callbackCalled;
055: }
056:
057: public synchronized void callback(Serializable returnObject,
058: AsynchronousCall methodCall) {
059: CURRENT_MILLIS_WHEN_CALLED.add(System.currentTimeMillis());
060: NUMBER_CALL_BACKS++;
061: setCallbackCalled(true);
062: this .numberCallbacks++;
063: QName serviceName = methodCall.getServiceInfo().getQname();
064: Integer callCount = SERVICE_CALL_COUNT_TRACKED.get(serviceName);
065: if (callCount == null) {
066: SERVICE_CALL_COUNT_TRACKED.put(methodCall.getServiceInfo()
067: .getQname(), 1);
068: } else {
069: SERVICE_CALL_COUNT_TRACKED.put(methodCall.getServiceInfo()
070: .getQname(), callCount + 1);
071: }
072:
073: List<AsynchronousCall> serviceCallsTracked = SERVICES_CALLS_TRACKED
074: .get(serviceName);
075: if (serviceCallsTracked == null) {
076: serviceCallsTracked = new ArrayList<AsynchronousCall>();
077: SERVICES_CALLS_TRACKED
078: .put(serviceName, serviceCallsTracked);
079: }
080: serviceCallsTracked.add(methodCall);
081:
082: System.out.println("!!!Callback called number callbacks "
083: + this .numberCallbacks);
084: }
085:
086: public static void clearCallbacks() {
087: NUMBER_CALL_BACKS = 0;
088: SERVICE_CALL_COUNT_TRACKED = new HashMap<QName, Integer>();
089: }
090:
091: /**
092: * sometimes it's more convenient to use a non static counter above when
093: * doing in memory queueing. Other times when doing persistent async
094: * messaging a static counter is needed. Everything could be converted to
095: * this method if the tests using the above method clear the static count
096: * before putting testing against callbacks.
097: *
098: * @param callbacks
099: * @param millisDelay
100: */
101: public void pauseUntilNumberCallbacksUsingStaticCounter(
102: int callbacks, QName serviceName) {
103: int numPauses = 0;
104: while (true) {
105: synchronized (this .getClass()) {
106: if (serviceName == null) {
107: if (NUMBER_CALL_BACKS >= callbacks) {
108: System.out
109: .println("!!!Returning number callback met");
110: return;
111: }
112: } else {
113: if (SERVICE_CALL_COUNT_TRACKED.get(serviceName) != null
114: && SERVICE_CALL_COUNT_TRACKED
115: .get(serviceName) >= callbacks) {
116: System.out
117: .println("!!!Returning number callback met for service "
118: + serviceName);
119: return;
120: }
121: // attributes will not be an exact match.
122: for (Map.Entry<QName, Integer> serviceCall : SERVICE_CALL_COUNT_TRACKED
123: .entrySet()) {
124: if (serviceCall
125: .getKey()
126: .getLocalPart()
127: .lastIndexOf(serviceName.getLocalPart()) > -1
128: && serviceCall
129: .getKey()
130: .getNamespaceURI()
131: .equals(
132: serviceName
133: .getNamespaceURI())
134: && serviceCall.getValue() >= callbacks) {
135: System.out
136: .println("!!!Returning number callback met for service "
137: + serviceName);
138: return;
139: }
140: }
141: }
142: }
143: if (numPauses > 60 * 1) {
144: return;
145: }
146: try {
147: numPauses++;
148: System.out
149: .println("!!!Test callback pausing for 1 second");
150: Thread.sleep(1000);
151: } catch (InterruptedException e) {
152: // nothing to do
153: }
154: }
155: }
156: }
|