forked from apache/dubbo
/
RpcInvocation.java
448 lines (377 loc) · 14.8 KB
/
RpcInvocation.java
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
* 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.dubbo.rpc;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.MapUtils;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.MethodDescriptor;
import org.apache.dubbo.rpc.model.ServiceDescriptor;
import org.apache.dubbo.rpc.model.ServiceRepository;
import org.apache.dubbo.rpc.support.RpcUtils;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.rpc.Constants.TOKEN_KEY;
/**
* RPC Invocation.
*
* @serial Don't change the class name and properties.
*/
public class RpcInvocation implements Invocation, Serializable {
private static final long serialVersionUID = -4355285085441097045L;
private String targetServiceUniqueName;
private String protocolServiceKey;
private String methodName;
private String serviceName;
private transient Class<?>[] parameterTypes;
private String parameterTypesDesc;
private String[] compatibleParamSignatures;
private Object[] arguments;
/**
* Passed to the remote server during RPC call
*/
private Map<String, Object> attachments;
/**
* Only used on the caller side, will not appear on the wire.
*/
private Map<Object, Object> attributes = new HashMap<>();
private transient Invoker<?> invoker;
private transient Class<?> returnType;
private transient Type[] returnTypes;
private transient InvokeMode invokeMode;
public RpcInvocation() {
}
public RpcInvocation(Invocation invocation, Invoker<?> invoker) {
this(invocation.getMethodName(), invocation.getServiceName(), invocation.getProtocolServiceKey(),
invocation.getParameterTypes(), invocation.getArguments(), new HashMap<>(invocation.getObjectAttachments()),
invocation.getInvoker(), invocation.getAttributes());
if (invoker != null) {
URL url = invoker.getUrl();
setAttachment(PATH_KEY, url.getPath());
if (url.hasParameter(INTERFACE_KEY)) {
setAttachment(INTERFACE_KEY, url.getParameter(INTERFACE_KEY));
}
if (url.hasParameter(GROUP_KEY)) {
setAttachment(GROUP_KEY, url.getParameter(GROUP_KEY));
}
if (url.hasParameter(VERSION_KEY)) {
setAttachment(VERSION_KEY, url.getParameter(VERSION_KEY, "0.0.0"));
}
if (url.hasParameter(TIMEOUT_KEY)) {
setAttachment(TIMEOUT_KEY, url.getParameter(TIMEOUT_KEY));
}
if (url.hasParameter(TOKEN_KEY)) {
setAttachment(TOKEN_KEY, url.getParameter(TOKEN_KEY));
}
if (url.hasParameter(APPLICATION_KEY)) {
setAttachment(APPLICATION_KEY, url.getParameter(APPLICATION_KEY));
}
}
this.targetServiceUniqueName = invocation.getTargetServiceUniqueName();
this.protocolServiceKey = invocation.getProtocolServiceKey();
}
public RpcInvocation(Invocation invocation) {
this(invocation.getMethodName(), invocation.getServiceName(), invocation.getProtocolServiceKey(), invocation.getParameterTypes(),
invocation.getArguments(), invocation.getObjectAttachments(), invocation.getInvoker(), invocation.getAttributes());
this.targetServiceUniqueName = invocation.getTargetServiceUniqueName();
}
public RpcInvocation(Method method, String serviceName, String protocolServiceKey, Object[] arguments) {
this(method, serviceName, protocolServiceKey, arguments, null, null);
}
public RpcInvocation(Method method, String serviceName, String protocolServiceKey, Object[] arguments, Map<String, Object> attachment, Map<Object, Object> attributes) {
this(method.getName(), serviceName, protocolServiceKey, method.getParameterTypes(), arguments, attachment, null, attributes);
}
public RpcInvocation(String methodName, String serviceName, String protocolServiceKey, Class<?>[] parameterTypes, Object[] arguments) {
this(methodName, serviceName, protocolServiceKey, parameterTypes, arguments, null, null, null);
}
public RpcInvocation(String methodName, String serviceName, String protocolServiceKey, Class<?>[] parameterTypes, Object[] arguments, Map<String, Object> attachments) {
this(methodName, serviceName, protocolServiceKey, parameterTypes, arguments, attachments, null, null);
}
public RpcInvocation(String methodName, String serviceName, String protocolServiceKey, Class<?>[] parameterTypes, Object[] arguments,
Map<String, Object> attachments, Invoker<?> invoker, Map<Object, Object> attributes) {
this.methodName = methodName;
this.serviceName = serviceName;
this.protocolServiceKey = protocolServiceKey;
this.parameterTypes = parameterTypes == null ? new Class<?>[0] : parameterTypes;
this.arguments = arguments == null ? new Object[0] : arguments;
this.attachments = attachments == null ? new HashMap<>() : attachments;
this.attributes = attributes == null ? new HashMap<>() : attributes;
this.invoker = invoker;
initParameterDesc();
}
private void initParameterDesc() {
ServiceRepository repository = ApplicationModel.getServiceRepository();
if (StringUtils.isNotEmpty(serviceName)) {
ServiceDescriptor serviceDescriptor = repository.lookupService(serviceName);
if (serviceDescriptor != null) {
MethodDescriptor methodDescriptor = serviceDescriptor.getMethod(methodName, parameterTypes);
if (methodDescriptor != null) {
this.parameterTypesDesc = methodDescriptor.getParamDesc();
this.compatibleParamSignatures = methodDescriptor.getCompatibleParamSignatures();
this.returnTypes = methodDescriptor.getReturnTypes();
this.returnType = methodDescriptor.getReturnClass();
}
}
}
if (parameterTypesDesc == null) {
this.parameterTypesDesc = ReflectUtils.getDesc(this.getParameterTypes());
this.compatibleParamSignatures = Stream.of(this.parameterTypes).map(Class::getName).toArray(String[]::new);
this.returnTypes = RpcUtils.getReturnTypes(this);
this.returnType = RpcUtils.getReturnType(this);
}
}
@Override
public Invoker<?> getInvoker() {
return invoker;
}
public void setInvoker(Invoker<?> invoker) {
this.invoker = invoker;
}
@Override
public Object put(Object key, Object value) {
return attributes.put(key, value);
}
@Override
public Object get(Object key) {
return attributes.get(key);
}
@Override
public Map<Object, Object> getAttributes() {
return attributes;
}
@Override
public String getTargetServiceUniqueName() {
return targetServiceUniqueName;
}
@Override
public String getProtocolServiceKey() {
return protocolServiceKey;
}
public void setTargetServiceUniqueName(String targetServiceUniqueName) {
this.targetServiceUniqueName = targetServiceUniqueName;
}
@Override
public String getMethodName() {
return methodName;
}
@Override
public String getServiceName() {
return serviceName;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
@Override
public Class<?>[] getParameterTypes() {
return parameterTypes;
}
public void setParameterTypes(Class<?>[] parameterTypes) {
this.parameterTypes = parameterTypes == null ? new Class<?>[0] : parameterTypes;
}
public String getParameterTypesDesc() {
return parameterTypesDesc;
}
public void setParameterTypesDesc(String parameterTypesDesc) {
this.parameterTypesDesc = parameterTypesDesc;
}
@Override
public String[] getCompatibleParamSignatures() {
return compatibleParamSignatures;
}
// parameter signatures can be set independently, it is useful when the service type is not found on caller side and
// the invocation is not generic invocation either.
public void setCompatibleParamSignatures(String[] compatibleParamSignatures) {
this.compatibleParamSignatures = compatibleParamSignatures;
}
@Override
public Object[] getArguments() {
return arguments;
}
public void setArguments(Object[] arguments) {
this.arguments = arguments == null ? new Object[0] : arguments;
}
@Override
public Map<String, Object> getObjectAttachments() {
return attachments;
}
@Override
public void setAttachment(String key, String value) {
setObjectAttachment(key, value);
}
@Deprecated
@Override
public Map<String, String> getAttachments() {
return MapUtils.objectToStringMap(attachments);
}
@Deprecated
public void setAttachments(Map<String, String> attachments) {
this.attachments = attachments == null ? new HashMap<>() : new HashMap<>(attachments);
}
public void setObjectAttachments(Map<String, Object> attachments) {
this.attachments = attachments == null ? new HashMap<>() : attachments;
}
@Override
public void setAttachment(String key, Object value) {
setObjectAttachment(key, value);
}
@Override
public void setObjectAttachment(String key, Object value) {
if (attachments == null) {
attachments = new HashMap<>();
}
attachments.put(key, value);
}
@Override
public void setAttachmentIfAbsent(String key, String value) {
setObjectAttachmentIfAbsent(key, value);
}
@Override
public void setAttachmentIfAbsent(String key, Object value) {
setObjectAttachmentIfAbsent(key, value);
}
@Override
public void setObjectAttachmentIfAbsent(String key, Object value) {
if (attachments == null) {
attachments = new HashMap<>();
}
if (!attachments.containsKey(key)) {
attachments.put(key, value);
}
}
@Deprecated
public void addAttachments(Map<String, String> attachments) {
if (attachments == null) {
return;
}
if (this.attachments == null) {
this.attachments = new HashMap<>();
}
this.attachments.putAll(attachments);
}
public void addObjectAttachments(Map<String, Object> attachments) {
if (attachments == null) {
return;
}
if (this.attachments == null) {
this.attachments = new HashMap<>();
}
this.attachments.putAll(attachments);
}
@Deprecated
public void addAttachmentsIfAbsent(Map<String, String> attachments) {
if (attachments == null) {
return;
}
for (Map.Entry<String, String> entry : attachments.entrySet()) {
setAttachmentIfAbsent(entry.getKey(), entry.getValue());
}
}
public void addObjectAttachmentsIfAbsent(Map<String, Object> attachments) {
if (attachments == null) {
return;
}
for (Map.Entry<String, Object> entry : attachments.entrySet()) {
setAttachmentIfAbsent(entry.getKey(), entry.getValue());
}
}
@Override
public String getAttachment(String key) {
if (attachments == null) {
return null;
}
Object value = attachments.get(key);
if (value instanceof String) {
return (String) value;
}
return null;
}
@Override
public Object getObjectAttachment(String key) {
if (attachments == null) {
return null;
}
return attachments.get(key);
}
@Override
@Deprecated
public String getAttachment(String key, String defaultValue) {
if (attachments == null) {
return defaultValue;
}
Object value = attachments.get(key);
if (value instanceof String) {
String strValue = (String) value;
if (StringUtils.isEmpty(strValue)) {
return defaultValue;
} else {
return strValue;
}
}
return null;
}
@Deprecated
@Override
public Object getObjectAttachment(String key, Object defaultValue) {
if (attachments == null) {
return defaultValue;
}
Object value = attachments.get(key);
if (value == null) {
return defaultValue;
}
return value;
}
public Class<?> getReturnType() {
return returnType;
}
public void setReturnType(Class<?> returnType) {
this.returnType = returnType;
}
public Type[] getReturnTypes() {
return returnTypes;
}
public void setReturnTypes(Type[] returnTypes) {
this.returnTypes = returnTypes;
}
public InvokeMode getInvokeMode() {
return invokeMode;
}
public void setInvokeMode(InvokeMode invokeMode) {
this.invokeMode = invokeMode;
}
@Override
public String toString() {
return "RpcInvocation [methodName=" + methodName + ", parameterTypes="
+ Arrays.toString(parameterTypes) + ", arguments=" + Arrays.toString(arguments)
+ ", attachments=" + attachments + "]";
}
}