summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0-Beta2-RC1/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/AsyncResponseInvoker.java
blob: 38727d467076f4b0702cebf948db0b0ebe447ed3 (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
/*
 * 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.Collection;
import java.util.Iterator;
import java.util.List;

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.FactoryExtensionPoint;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.invocation.Invoker;
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.node.NodeFactory;
import org.apache.tuscany.sca.provider.EndpointAsyncProvider;
import org.apache.tuscany.sca.runtime.DomainRegistryFactory;
import org.apache.tuscany.sca.runtime.EndpointRegistry;
import org.apache.tuscany.sca.runtime.ExtensibleDomainRegistryFactory;
import org.apache.tuscany.sca.runtime.RuntimeEndpoint;
import org.apache.tuscany.sca.runtime.RuntimeEndpointReference;
import org.oasisopen.sca.ComponentContext;

/**
 * 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 RuntimeEndpoint requestEndpoint;
    private RuntimeEndpointReference responseEndpointReference; 
    private T responseTargetAddress;
    private String relatesToMsgID;
    private String operationName;
    private MessageFactory messageFactory;
    private String bindingType = "";
    private boolean isNativeAsync;
    
    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;
		
        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
     */
    public void invokeAsyncResponse(Object args) {
        
        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( 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;
    }
	
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
    {
	    in.defaultReadObject();
	    
	    // find the real endpoint
	    ExtensionPointRegistry extensionPointRegistry = NodeFactory.getInstance().getExtensionPointRegistry();
	    DomainRegistryFactory domainRegistryFactory = ExtensibleDomainRegistryFactory.getInstance(extensionPointRegistry);
	    Collection<EndpointRegistry> endpointRegistries = domainRegistryFactory.getEndpointRegistries();
	    EndpointRegistry endpointRegistry = endpointRegistries.iterator().next();
	    List<Endpoint> endpoints = endpointRegistry.findEndpoint(requestEndpoint.getURI()); 
	    requestEndpoint = (RuntimeEndpoint)endpoints.get(0);
	    
	    if (responseTargetAddress instanceof EndpointReference){
	        // fix the target as in this case it will be an EPR
	        EndpointReference epr = (EndpointReference)responseTargetAddress;
	        List<EndpointReference> endpointReferences = endpointRegistry.getEndpointReferences();
	        for (EndpointReference endpointReference : endpointReferences){
	            if (endpointReference.getURI().equals(epr.getURI())){
	                responseTargetAddress = (T)endpointReference;
	            }
	        }
	    }     
    }	
} // end class