blob: e7db583eddc74934ea5a901d9e435b13aceca089 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
package org.apache.tuscany.sca.implementation.java.invocation;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.tuscany.sca.core.invocation.AsyncFaultWrapper;
import org.apache.tuscany.sca.core.invocation.AsyncResponseHandler;
import org.apache.tuscany.sca.core.invocation.JDKAsyncResponseInvoker;
import org.apache.tuscany.sca.invocation.Message;
import org.oasisopen.sca.ServiceRuntimeException;
/**
* Class which handles the asynchronous response message from an async service back to the client Java component
*
* This class provides a registration function which permits the reference invoking code to register the Future
* which is used to return the response to the Java component code
*/
public class JavaAsyncResponseInvokerImpl implements JDKAsyncResponseInvoker {
// Map used to link between async requests and async responses
private ConcurrentMap<String, Object> asyncMessageMap;
public JavaAsyncResponseInvokerImpl() {
asyncMessageMap = new ConcurrentHashMap<String, Object>();
} // end constructor
/**
* Deal with the asynchronous response message
* @param msg - the response message
*
* The response message must contain a RELATES_TO id, which is used to identify the Future that represents
* the operation yet to complete. The Future is then completed with the content of the message.
* The Future either calls back to the application code, or it is expected that the application is polling
* the Future for its completion.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public void invokeAsyncResponse(Message msg) {
// Obtain the Message ID for this message
String relatesID = getMessageRelatesID( msg );
if( relatesID == null )
throw new ServiceRuntimeException("JavaAsyncResponseInvoker - response message has no RELATES_TO id");
// Look up the response object & remove it from the Map
Object responseHandler = asyncMessageMap.remove(relatesID);
if( responseHandler == null )
throw new ServiceRuntimeException("JavaAsyncResponseInvoker - no Future matches the RELATES_TO id: " + relatesID);
// Invoke the response handler with the content of the message
// - in the case of a Java implementation, the response handler is a Future...
AsyncResponseHandler future = (AsyncResponseHandler) responseHandler;
Object payload = msg.getBody();
Object response;
if( payload == null ) {
System.out.println("Returned response message was null");
} else {
if (payload.getClass().isArray()) {
response = ((Object[])payload)[0];
} else {
response = payload;
} // end if
if( response.getClass().equals(AsyncFaultWrapper.class)) {
future.setWrappedFault((AsyncFaultWrapper) response );
} else if ( response instanceof Throwable ) {
future.setFault( (Throwable)response );
} else {
future.setResponse(response);
} // end if
} // end if
} // end method invokeAsyncResponse
/**
* Registers an Async response, which provides an ID which identifies a given response
* and an object which can handle the response
* @param id - the ID
* @param responseHandler - the response handler object
*/
public void registerAsyncResponse( String id, Object responseHandler ) {
// Add the ID/response handler mapping into the table
if( id != null && responseHandler != null ) asyncMessageMap.put(id, responseHandler);
} // end method registerAsyncResponse
/**
* Extracts the RELATES_TO header from the message
* @param msg - the Tuscany message
* @return - the value of the RELATES_TO header as a String
*/
private String getMessageRelatesID( Message msg ) {
return (String)msg.getHeaders().get("RELATES_TO");
} // end method getMessageRelatesID
} // end class JavaAsyncResponseInvoker
|