summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationInvoker.java
blob: 9ea50fce73cc192075890f3045e406c9d33c4992 (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
/*
 * 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.implementation.java.invocation;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;

import javax.xml.ws.Holder;

import org.apache.tuscany.sca.assembly.EndpointReference;
import org.apache.tuscany.sca.assembly.Service;
import org.apache.tuscany.sca.core.factory.ObjectCreationException;
import org.apache.tuscany.sca.core.invocation.Constants;
import org.apache.tuscany.sca.core.scope.Scope;
import org.apache.tuscany.sca.core.scope.ScopeContainer;
import org.apache.tuscany.sca.core.scope.ScopedRuntimeComponent;
import org.apache.tuscany.sca.implementation.java.JavaImplementation;
import org.apache.tuscany.sca.implementation.java.context.ReflectiveInstanceWrapper;
import org.apache.tuscany.sca.implementation.java.injection.Injector;
import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper;
import org.apache.tuscany.sca.interfacedef.DataType;
import org.apache.tuscany.sca.interfacedef.InterfaceContract;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.interfacedef.ParameterMode;
import org.apache.tuscany.sca.interfacedef.java.JavaInterface;
import org.apache.tuscany.sca.interfacedef.java.impl.JavaInterfaceUtil;
import org.apache.tuscany.sca.invocation.DataExchangeSemantics;
import org.apache.tuscany.sca.invocation.Invoker;
import org.apache.tuscany.sca.invocation.Message;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
import org.apache.tuscany.sca.runtime.RuntimeComponentService;
import org.oasisopen.sca.ServiceReference;
import org.oasisopen.sca.ServiceRuntimeException;

/**
 * Responsible for synchronously dispatching an invocation to a Java component
 * implementation instance
 * 
 * @version $Rev$ $Date$
 */
public class JavaImplementationInvoker implements Invoker, DataExchangeSemantics {
    protected Operation operation;
    protected Method method;
    protected RuntimeComponentService service;
    protected boolean allowsPBR;

    @SuppressWarnings("unchecked")
    protected final ScopeContainer scopeContainer;
	private final InterfaceContract interfaze;

    public JavaImplementationInvoker(Operation operation, Method method, RuntimeComponent component, RuntimeComponentService service) {
        assert method != null : "Operation method cannot be null";
        this.method = method;
        this.operation = operation;
        this.scopeContainer = ((ScopedRuntimeComponent)component).getScopeContainer();
        this.allowsPBR = ((JavaImplementation)component.getImplementation()).isAllowsPassByReference(method);
        this.service = service;
        this.interfaze = service.getInterfaceContract();
    }

    public JavaImplementationInvoker(Operation operation, RuntimeComponent component, RuntimeComponentService service) {
        // used if the method can't be computed statically in advance 
        this.operation = operation;
        this.scopeContainer = ((ScopedRuntimeComponent)component).getScopeContainer();
        this.service = service;
        this.interfaze = service.getInterfaceContract();
    }

    @SuppressWarnings("unchecked")
    public Message invoke(Message msg) {
        
        Operation op = msg.getOperation();
        if (op == null) {
            op = this.operation;
        }
        Object payload = msg.getBody();

        Object contextId = null;

        EndpointReference from = msg.getFrom();

        // store the current thread context classloader
        // as we need to replace it with the class loader
        // used to load the java class as per SCA Spec
        final ClassLoader tccl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
                public ClassLoader run() {
                    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
                    return tccl;
                 }
            });
        
        // TUSCANY-3946 - If the TCCL has not already been set to the contribution classloader earlier
        // in the wire processing then
        // set it so that the thread context classloader of the thread used to invoke an operation 
        // of a Java POJO component implementation is the class loader of the contribution 
        // used to load the POJO implementation class.
        boolean swapTCCL = (msg.getHeaders().get(Constants.SUPPRESS_TCCL_SWAP) == null);
        
        try {
            // The following call might create a new conversation, as a result, the msg.getConversationID() might 
            // return a new value
            ReflectiveInstanceWrapper wrapper = (ReflectiveInstanceWrapper) scopeContainer.getWrapper(contextId);
            
            // If there is a callback interface and the implementation is stateless, we need to
            // inject callbacks at invocation time. For Composite scope, this has already been done. 
            if (( interfaze.getCallbackInterface() != null )  && (scopeContainer.getScope().equals(Scope.STATELESS))){
                // TUSCANY-4000 - injectCallbacks needs the Java callback interface so get it 
                //                from the component type just in case the user has specified a
                //                WSDL interface and hence interfaze is WSDL
            	injectCallbacks(wrapper, (JavaInterface)service.getService().getInterfaceContract().getCallbackInterface());
            }
            
            final Object instance = wrapper.getInstance();

            // If the method couldn't be computed statically, or the instance being
            // invoked is a user-specified callback object that doesn't implement
            // the service interface from which the reflective method was obtained,
            // compute the method object dynamically for this invocation.
            Method imethod = method;
            if (imethod == null || !imethod.getDeclaringClass().isInstance(instance)) {
                try {
                    imethod = JavaInterfaceUtil.findMethod(instance.getClass(), operation);
                } catch (NoSuchMethodException e) {
                    throw new IllegalArgumentException("Callback object does not provide method " + e.getMessage());
                }
            }
            
            if (swapTCCL){
                AccessController.doPrivileged(new PrivilegedAction() {
                    public Object run() {
                        Thread.currentThread().setContextClassLoader(instance.getClass().getClassLoader());
                        return null;
                     }
                });
            }
            
            int argumentHolderCount = 0;

            // Holder pattern. Any payload parameters <T> which are should be in holders are placed in Holder<T>.
            // Only check Holder for remotable interfaces
            if (imethod != null && op.getInterface().isRemotable()) {
                Object[] payloadArray = (Object[])payload;
                List<Object> payloadList = new ArrayList<Object>();
                int nextIndex = 0;
                for (ParameterMode mode : op.getParameterModes()) {                    
                    if (mode.equals(ParameterMode.IN)) {
                        payloadList.add(payloadArray[nextIndex++]);
                    } else if (mode.equals(ParameterMode.INOUT)) {
                        // Promote array params from [<T>] to [Holder<T>]                  
                        Object item = payloadArray[nextIndex++];                           
                        Holder itemHolder = new Holder(item);
                        payloadList.add(itemHolder);
                        argumentHolderCount++;
                    } else {
                        // Create an empty Holder since we should not pass values for OUT parameters
                        payloadList.add(new Holder());
                        argumentHolderCount++;
                    }                        
                }
                
                // Maybe a bit odd to do but this way I don't have to worry about how the invoke if/else
                // immediately following might need to be changed.
                payload = payloadList.toArray();
            }

            Object ret;
            if (payload != null && !payload.getClass().isArray()) {
                ret = imethod.invoke(instance, payload);
            } else {
                ret = imethod.invoke(instance, (Object[])payload);
            }

            scopeContainer.returnWrapper(wrapper, contextId);
            
                        
            if (argumentHolderCount > 0) {
            	
                // Holder pattern. Any payload Holder<T> types are returned as part of the message body.
            	Object[] payloadArray = (Object[])payload;
            	
            	ArrayList<Object> holderOutputs = new ArrayList<Object>();
            	ArrayList<Object> result = new ArrayList<Object>();
                if (imethod != null) {                	
                    
                    for (int i = 0, size = op.getParameterModes().size(); i < size; i++) {                       
                        if (ParameterMode.IN != op.getParameterModes().get(i)) {                        	
                        	// Demote array params from Holder<T> to <T>.                                                   
                        	Holder<Object> item = (Holder<Object>)payloadArray[i];
                        	payloadArray[i] = item.value;
                        	holderOutputs.add(payloadArray[i]);                        	                      
                        }
                    }
                    
                    //
                    // Now we account for the fact that we may have a null because of a void return type,
                    // which is not part of the output DataType, and so should not be returned with the array
                    // of outputs, or we may have a null as value returned
                    // from a method with signature with return type other than void, which should be returned 
                    // in the output array.
                    // 
                    // The logic here is if we already have as many outputs in holders as we have outputs
                    // altogether, then we don't worry about the return value (which should be null).  Might
                    // be simpler to just check for void, but the code in the Java introspector has a lot
                    // of quirks for handling parameterized types, and this seems simpler for now.
                    //
                    int holderOutputSize = holderOutputs.size();
                    int numberOperationOutputs = op.getOutputType().getLogical().size();
                    if (holderOutputSize == numberOperationOutputs) {
                        if (ret != null) {
                            throw new IllegalStateException("Number of holder outputs equal to number of operations outputs." +
                                                            "\nNum = " + holderOutputSize + ", but non-null return value seen: " + ret);
                        }
                        result = holderOutputs;
                    } else if (holderOutputSize == numberOperationOutputs - 1) {
                        result.add(ret);
                        result.addAll(1, holderOutputs);
                    } else {
                        throw new IllegalStateException("Number of holder outputs seen: " + holderOutputSize +  
                                                        "\nNumber of operation outputs: " + numberOperationOutputs);
                    }
                }

                msg.setBody(result.toArray());

            } else {
                msg.setBody(ret);
            }
        } catch (InvocationTargetException e) {
            Throwable cause = e.getTargetException();
            boolean isChecked = false;
            for (DataType<?> d : operation.getFaultTypes()) {
                if (d.getPhysical().isInstance(cause)) {
                    isChecked = true;
                    msg.setFaultBody(cause);
                    break;
                }
            }
            if (!isChecked) {
                if (cause instanceof RuntimeException) {
                    throw (RuntimeException)cause;
                }
                if (cause instanceof Error) {
                    throw (Error)cause;
                } else {
                    throw new ServiceRuntimeException(cause.getMessage(), cause);
                }
            }            
                
        } catch (ObjectCreationException e) {
            throw new ServiceRuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            msg.setFaultBody(e);           
        } finally {
            // reset the tccl if it was replaced above
            // with the contribution classloader
            if (swapTCCL){
                AccessController.doPrivileged(new PrivilegedAction() {
                    public Object run() {
                        Thread.currentThread().setContextClassLoader(tccl);
                        return null;
                     }
                });
            }
        }
        return msg;
    }

	private void injectCallbacks(ReflectiveInstanceWrapper wrapper,
			JavaInterface callbackInterface) {
	
		for (Injector injector : wrapper.getCallbackInjectors()) {
			if (injector != null) {
				try {       
					if (ServiceReference.class.isAssignableFrom(injector.getType())) {
						Class<?> intf = JavaIntrospectionHelper.getBusinessInterface(injector.getType(), injector.getGenericType());
						if ( intf.isAssignableFrom(callbackInterface.getJavaClass())) {              		                		                		
							injector.inject(wrapper.getInstance());         
						}
					} else if (injector.getType().isAssignableFrom(callbackInterface.getJavaClass())) {
						injector.inject(wrapper.getInstance());
					} else {
						injector.injectNull(wrapper.getInstance());
					}
				} catch (Exception e) {	                   
					throw new ObjectCreationException("Exception invoking injector - " + e.getMessage(), e);
				}
			}
		}
		
	}

    @Override
    public boolean allowsPassByReference() {
        return allowsPBR;
    }		

}