summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/AsyncResponseInvoker.java
blob: 1ebc9c633ac3ce7512e4e9bb8fe255c454699f98 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.core.invocation;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.tuscany.sca.assembly.Endpoint;
import org.apache.tuscany.sca.assembly.EndpointReference;
import org.apache.tuscany.sca.context.CompositeContext;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.core.ExtensionPointRegistryLocator;
import org.apache.tuscany.sca.core.FactoryExtensionPoint;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.invocation.InvokerAsyncResponse;
import org.apache.tuscany.sca.invocation.Message;
import org.apache.tuscany.sca.invocation.MessageFactory;
import org.apache.tuscany.sca.provider.EndpointAsyncProvider;
import org.apache.tuscany.sca.runtime.DomainRegistryFactory;
import org.apache.tuscany.sca.runtime.DomainRegistry;
import org.apache.tuscany.sca.runtime.ExtensibleDomainRegistryFactory;
import org.apache.tuscany.sca.runtime.RuntimeEndpoint;
import org.apache.tuscany.sca.runtime.RuntimeEndpointReference;

/**
 * A class that wraps the mechanics for sending async responses
 * and hides the decision about whether the response will be processed
 * natively or non-natively
 * 
 * This class is generic, based on the type of targetAddress information required by
 * the Binding that creates it
 */
public class AsyncResponseInvoker<T> implements InvokerAsyncResponse, Serializable {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = -7992598227671386588L;
	
	private transient RuntimeEndpoint requestEndpoint;
    private transient RuntimeEndpointReference responseEndpointReference; 
    private T responseTargetAddress;
    private String relatesToMsgID;
    private String operationName;
    private transient MessageFactory messageFactory;
    private String bindingType = "";
    private boolean isNativeAsync;
    
    private String endpointURI;
    private String endpointReferenceURI;
    private String domainURI;

	private transient DomainRegistry domainRegistry;
	private transient ExtensionPointRegistry registry;
    
    public AsyncResponseInvoker(RuntimeEndpoint requestEndpoint,
			RuntimeEndpointReference responseEndpointReference,
			T responseTargetAddress, String relatesToMsgID, 
			String operationName, MessageFactory messageFactory) {
		super();
		this.requestEndpoint = requestEndpoint;
		this.responseEndpointReference = responseEndpointReference;
		this.responseTargetAddress = responseTargetAddress;
		this.relatesToMsgID = relatesToMsgID;
		this.operationName = operationName;
		this.messageFactory = messageFactory;
		
		CompositeContext context = null;
		if(requestEndpoint != null ) {
			endpointURI = requestEndpoint.getURI();
			context = requestEndpoint.getCompositeContext(); 
		} // end if
		if(responseEndpointReference != null ) {
			endpointReferenceURI = responseEndpointReference.getURI();
			context = responseEndpointReference.getCompositeContext();
		}
		
		if( context != null ) {
			domainURI = context.getDomainURI();
			registry = context.getExtensionPointRegistry();
		} // end if
		
        if ((requestEndpoint.getBindingProvider() instanceof EndpointAsyncProvider) &&
                (((EndpointAsyncProvider)requestEndpoint.getBindingProvider()).supportsNativeAsync())){
        	isNativeAsync = true;
        } else {
        	isNativeAsync = false;
        } // end if
	} // end constructor

    /** 
     * If you have a Tuscany message you can call this
     */
    public void invokeAsyncResponse(Message responseMessage) {
    	responseMessage.getHeaders().put(Constants.ASYNC_RESPONSE_INVOKER, this);
    	responseMessage.getHeaders().put(Constants.RELATES_TO, relatesToMsgID);
    	
        if (isNativeAsync){
            // process the response as a native async response
            requestEndpoint.invokeAsyncResponse(responseMessage);
        } else {
            // process the response as a non-native async response
            responseEndpointReference.invoke(responseMessage);
        }
    } // end method invokeAsyncReponse(Message)
    
    public T getResponseTargetAddress() {
		return responseTargetAddress;
	}

	public void setResponseTargetAddress(T responseTargetAddress) {
		this.responseTargetAddress = responseTargetAddress;
	}

	public String getRelatesToMsgID() {
		return relatesToMsgID;
	}

	public void setRelatesToMsgID(String relatesToMsgID) {
		this.relatesToMsgID = relatesToMsgID;
	}

	/**
     * Invokes the async response where the parameter is Java bean(s) 
     * - this method creates a Tuscany message
     * 
     * @param args the response data
     * @param headers - any header 
     */
    public void invokeAsyncResponse(Object args, Map<String, Object> headers) {
        
        Message msg = messageFactory.createMessage();

        msg.setOperation(getOperation( args ));
        
        // If this is not native async, then any Throwable is being passed as a parameter and
        // requires wrapping
        if( !isNativeAsync && args instanceof Throwable ) {
        	args = new AsyncFaultWrapper( (Throwable) args ); 
        } // end if
        
        // If this is not native async, then the message must contain an array of args since
        // this is what is expected when invoking an EPR for the async response...
        if( !isNativeAsync ) {
        	Object[] objs = new Object[1];
        	objs[0] = args;
        	args = objs;
        } // end if

        msg.setTo(requestEndpoint);
        msg.setFrom(responseEndpointReference);
        
        if( headers != null ) {
        	msg.getHeaders().putAll(headers);
        }
        
        if( args instanceof Throwable ) {
        	msg.setFaultBody(args);
        } else {
        	msg.setBody(args);
        } // end if
        
        invokeAsyncResponse(msg);
        
    } // end method invokeAsyncResponse(Object)

	private Operation getOperation( Object args ) {
		if( isNativeAsync ) {
			List<Operation> ops = requestEndpoint.getService().getInterfaceContract().getInterface().getOperations();
			for (Operation op : ops) {
				if( operationName.equals(op.getName()) ) return op;
			} // end for
			return null;
		} else {
			operationName = "setResponse";
			if( args instanceof Throwable ) { operationName = "setWrappedFault"; }
			List<Operation> ops = responseEndpointReference.getReference().getInterfaceContract().getInterface().getOperations();
			for (Operation op : ops) {
				if( operationName.equals(op.getName()) ) return op;
			} // end for
			return null;
		} // end if 
	} // end getOperation

	public void setBindingType(String bindingType) {
		this.bindingType = bindingType;
	} // end method setBindingType

	public String getBindingType() {
		return bindingType;
	} // end method getBindingType

    public RuntimeEndpoint getRequestEndpoint() {
	return this.requestEndpoint;
    }

    public RuntimeEndpointReference getResponseEndpointReference() {
	return this.responseEndpointReference;
    }

	public void setResponseEndpointReference(
			RuntimeEndpointReference responseEndpointReference) {
		this.responseEndpointReference = responseEndpointReference;
    }
	
    @SuppressWarnings("unchecked")
	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
    {
	    in.defaultReadObject();
	    
	    requestEndpoint = retrieveEndpoint(endpointURI);
	    responseEndpointReference = retrieveEndpointReference(endpointReferenceURI);
	    
	    messageFactory = getMessageFactory();
	    
	    if (responseTargetAddress instanceof EndpointReference){
	        // fix the target as in this case it will be an EPR
	        EndpointReference epr = (EndpointReference)responseTargetAddress;
	        responseTargetAddress = (T)retrieveEndpointReference(epr.getURI());
	    } // end if    
    } // end method readObject	

    /**
     * Gets a message factory
     * @return
     */
    private MessageFactory getMessageFactory() {
    	return registry.getExtensionPoint(FactoryExtensionPoint.class).getFactory(MessageFactory.class);
	} // end method getMessageFactory

	/**
     * Fetches the EndpointReference identified by an endpoint reference URI
     * @param uri - the URI of the endpoint reference
     * @return - the EndpointReference matching the supplied URI - null if no EPR is found which
     * matches the URI
     */
    private RuntimeEndpointReference retrieveEndpointReference(String uri) {
		if( uri == null ) return null;
    	if( domainRegistry == null ) return null;
		List<EndpointReference> refs = domainRegistry.findEndpointReferences( uri );
		// If there is more than EndpointReference with the uri...
		if( refs.isEmpty() ) return null;
		// TODO: what if there is more than 1 EPR with the given URI?
		return (RuntimeEndpointReference) refs.get(0);
	} // end method retrieveEndpointReference

	/**
     * Fetches the Endpoint identified by an endpoint URI
     * - the Endpoint is retrieved from the DomainRegistry 
     * @param uri - the URI of the Endpoint
     * @return - the Endpoint corresponding to the URI, or null if no Endpoint is found which has the
     * supplied URI
     */
	private RuntimeEndpoint retrieveEndpoint(String uri) {
		if( uri == null ) return null;
		if( domainRegistry == null ) domainRegistry = getEndpointRegistry( uri );
		if( domainRegistry == null ) return null;
		// TODO what if more than one Endpoint gets returned??
		return (RuntimeEndpoint) domainRegistry.findEndpoint(uri).get(0);
	} // end method retrieveEndpoint

	/**
	 * Gets the DomainRegistry which contains an Endpoint with the supplied URI
	 * @param uri - The URI of an Endpoint
	 * @return - the DomainRegistry containing the Endpoint with the supplied URI - null if no
	 *           such DomainRegistry can be found
	 */
	private DomainRegistry getEndpointRegistry(String uri) {
		ExtensionPointRegistry registry   = null;
		DomainRegistry domainRegistry = null;
		
		CompositeContext context = CompositeContext.getCurrentCompositeContext();
		if( context == null && requestEndpoint != null ) context = requestEndpoint.getCompositeContext();
		if( context != null ) {
			registry = context.getExtensionPointRegistry();
			domainRegistry = getEndpointRegistry( registry );
			if( domainRegistry != null ) {
				this.registry = registry;
				return domainRegistry;
			} // end if
		} // end if
		
		// Deal with the case where there is no context available
    	for(ExtensionPointRegistry r : ExtensionPointRegistryLocator.getExtensionPointRegistries()) {
                registry = r;
    		if( registry != null ) {
    			// Find the actual Endpoint in the DomainRegistry
        		domainRegistry = getEndpointRegistry( registry );
                
                if( domainRegistry != null ) {
                    for( Endpoint endpoint : domainRegistry.findEndpoint(uri) ) {
                    	// TODO: For the present, simply return the first registry with a matching endpoint
                    	this.registry = registry;
                    	return domainRegistry;
                    } // end for
                } // end if 
    		} // end if
        } // end for
		
		return null;
	} // end method getEndpointRegistry
	
    /**
     * Get the DomainRegistry
     * @param registry - the ExtensionPoint registry
     * @return the DomainRegistry - will be null if the DomainRegistry cannot be found
     */
    private DomainRegistry getEndpointRegistry( ExtensionPointRegistry registry) {
        DomainRegistryFactory domainRegistryFactory = ExtensibleDomainRegistryFactory.getInstance(registry);
        
        if( domainRegistryFactory == null ) return null;
        
        // Find the first endpoint registry that matches the domain name 
        if( domainURI != null ) {
	        for( DomainRegistry domainRegistry : domainRegistryFactory.getEndpointRegistries() ) {
	        	if( domainURI.equals( domainRegistry.getDomainURI() ) ) return domainRegistry;
	        } // end for
        } // end if
        
        // if there was no domainName to match, simply return the first DomainRegistry if there is one...
        
        if (domainRegistryFactory.getEndpointRegistries().size() > 0){
            DomainRegistry domainRegistry = (DomainRegistry) domainRegistryFactory.getEndpointRegistries().toArray()[0];
            return domainRegistry;
        } else {
            return null;
        }
        
    } // end method 
	
} // end class