001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.util;
021:
022: import org.apache.axiom.soap.SOAPEnvelope;
023: import org.apache.axis2.AxisFault;
024: import org.apache.axis2.addressing.RelatesTo;
025: import org.apache.axis2.client.async.AsyncResult;
026: import org.apache.axis2.client.async.Callback;
027: import org.apache.axis2.client.async.AxisCallback;
028: import org.apache.axis2.context.MessageContext;
029: import org.apache.axis2.context.OperationContext;
030: import org.apache.axis2.engine.MessageReceiver;
031:
032: import java.util.HashMap;
033:
034: /**
035: * This is a MessageReceiver which is used on the client side to accept the
036: * messages (response) that come to the client. This correlates the incoming message to
037: * the related messages and makes a call to the appropriate callback.
038: */
039: public class CallbackReceiver implements MessageReceiver {
040: public static String SERVICE_NAME = "ClientService";
041: private HashMap callbackStore;
042:
043: public CallbackReceiver() {
044: callbackStore = new HashMap();
045: }
046:
047: public void addCallback(String MsgID, Callback callback) {
048: callbackStore.put(MsgID, callback);
049: }
050:
051: public void addCallback(String msgID, AxisCallback callback) {
052: callbackStore.put(msgID, callback);
053: }
054:
055: public Object lookupCallback(String msgID) {
056: return callbackStore.remove(msgID);
057: }
058:
059: public void receive(MessageContext msgContext) throws AxisFault {
060: RelatesTo relatesTO = msgContext.getOptions().getRelatesTo();
061: if (relatesTO == null) {
062: throw new AxisFault(
063: "Cannot identify correct Callback object. RelatesTo is null");
064: }
065: String messageID = relatesTO.getValue();
066:
067: Object callbackObj = callbackStore.remove(messageID);
068:
069: if (callbackObj == null) {
070: throw new AxisFault("The Callback for MessageID "
071: + messageID + " was not found");
072: }
073:
074: if (callbackObj instanceof AxisCallback) {
075: AxisCallback axisCallback = (AxisCallback) callbackObj;
076: if (msgContext.isFault()) {
077: axisCallback.onFault(msgContext);
078: } else {
079: axisCallback.onMessage(msgContext);
080: }
081: // Either way we're done.
082: axisCallback.onComplete();
083: return;
084: }
085:
086: // THIS NEXT PART WILL EVENTUALLY GO AWAY WHEN Callback DOES
087:
088: // OK, this must be an old-style Callback
089: Callback callback = (Callback) callbackObj;
090: AsyncResult result = new AsyncResult(msgContext);
091:
092: // check whether the result is a fault.
093: try {
094: SOAPEnvelope envelope = result.getResponseEnvelope();
095:
096: OperationContext opContext = msgContext
097: .getOperationContext();
098: if (opContext != null && !opContext.isComplete()) {
099: opContext.addMessageContext(msgContext);
100: }
101:
102: if (envelope.getBody().hasFault()) {
103: AxisFault axisFault = Utils
104: .getInboundFaultFromMessageContext(msgContext);
105: callback.onError(axisFault);
106: } else {
107: callback.onComplete(result);
108: }
109: } catch (Exception e) {
110: callback.onError(e);
111: } finally {
112: callback.setComplete(true);
113: }
114: }
115:
116: //to get the pending request
117: public HashMap getCallbackStore() {
118: return callbackStore;
119: }
120: }
|