/
JvmBridge.cs
290 lines (256 loc) · 11 KB
/
JvmBridge.cs
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Text;
using Microsoft.Spark.CSharp.Network;
using Microsoft.Spark.CSharp.Services;
namespace Microsoft.Spark.CSharp.Interop.Ipc
{
/// <summary>
/// Implementation of thread safe IPC bridge between JVM and CLR
/// Using a concurrent socket connection queue (lightweight synchronization mechanism)
/// supporting async JVM calls like StreamingContext.AwaitTermination()
/// </summary>
[ExcludeFromCodeCoverage] //IPC calls to JVM validated using validation-enabled samples - unit test coverage not reqiured
internal class JvmBridge : IJvmBridge
{
private int portNumber;
private readonly ConcurrentQueue<ISocketWrapper> sockets = new ConcurrentQueue<ISocketWrapper>();
private readonly ILoggerService logger = LoggerServiceFactory.GetLogger(typeof(JvmBridge));
public void Initialize(int portNumber)
{
this.portNumber = portNumber;
}
private ISocketWrapper GetConnection()
{
ISocketWrapper socket;
if (!sockets.TryDequeue(out socket))
{
socket = SocketFactory.CreateSocket();
socket.Connect(IPAddress.Loopback, portNumber, null);
}
return socket;
}
public JvmObjectReference CallConstructor(string className, params object[] parameters)
{
return new JvmObjectReference(CallJavaMethod(true, className, "<init>", parameters).ToString());
}
public object CallStaticJavaMethod(string className, string methodName, params object[] parameters)
{
return CallJavaMethod(true, className, methodName, parameters);
}
public object CallStaticJavaMethod(string className, string methodName)
{
return CallJavaMethod(true, className, methodName, new object[] { });
}
public object CallNonStaticJavaMethod(JvmObjectReference objectId, string methodName, params object[] parameters)
{
return CallJavaMethod(false, objectId, methodName, parameters);
}
public object CallNonStaticJavaMethod(JvmObjectReference objectId, string methodName)
{
return CallJavaMethod(false, objectId, methodName, new object[] { });
}
private object CallJavaMethod(bool isStatic, object classNameOrJvmObjectReference, string methodName, params object[] parameters)
{
object returnValue = null;
try
{
var overallPayload = PayloadHelper.BuildPayload(isStatic, classNameOrJvmObjectReference, methodName, parameters);
var socket = GetConnection();
using (var s = socket.GetStream())
{
SerDe.Write(s, overallPayload);
s.Flush();
var isMethodCallFailed = SerDe.ReadInt(s);
//TODO - add boolean instead of int in the backend
if (isMethodCallFailed != 0)
{
var jvmFullStackTrace = SerDe.ReadString(s);
var errorMessage = BuildErrorMessage(isStatic, classNameOrJvmObjectReference, methodName, parameters);
logger.LogError(errorMessage);
logger.LogError(jvmFullStackTrace);
throw new Exception(errorMessage);
}
var typeAsChar = Convert.ToChar(s.ReadByte());
switch (typeAsChar) //TODO - add support for other types
{
case 'n':
break;
case 'j':
returnValue = SerDe.ReadString(s);
break;
case 'c':
returnValue = SerDe.ReadString(s);
break;
case 'i':
returnValue = SerDe.ReadInt(s);
break;
case 'd':
returnValue = SerDe.ReadDouble(s);
break;
case 'b':
returnValue = Convert.ToBoolean(s.ReadByte());
break;
case 'l':
returnValue = ReadCollection(s);
break;
default:
// convert typeAsChar to UInt32 because the char may be non-printable
throw new NotSupportedException(string.Format("Identifier for type 0x{0:X} not supported", Convert.ToUInt32(typeAsChar)));
}
}
sockets.Enqueue(socket);
}
catch (Exception e)
{
logger.LogException(e);
throw;
}
return returnValue;
}
private string BuildErrorMessage(bool isStatic, object classNameOrJvmObjectReference, string methodName, object[] parameters)
{
var errorMessage = new StringBuilder("JVM method execution failed: ");
const string constructorFormat = "Constructor failed for class {0}";
const string staticMethodFormat = "Static method {0} failed for class {1}";
const string nonStaticMethodFormat = "Nonstatic method {0} failed for class {1}";
try
{
if (isStatic)
{
if (methodName.Equals("<init>")) //<init> is hardcoded in CSharpBackend
{
errorMessage.AppendFormat(constructorFormat, classNameOrJvmObjectReference);
}
else
{
errorMessage.AppendFormat(staticMethodFormat, methodName, classNameOrJvmObjectReference);
}
}
else
{
errorMessage.AppendFormat(nonStaticMethodFormat, methodName, classNameOrJvmObjectReference);
}
if (parameters.Length == 0)
{
errorMessage.Append(" when called with no parameters");
}
else
{
errorMessage.AppendFormat(" when called with {0} parameters ({1})", parameters.Length, GetParamsAsString(parameters));
}
}
catch (Exception)
{
errorMessage.Append("Exception when converting building error message");
}
return errorMessage.ToString();
}
private string GetParamsAsString(IEnumerable<object> parameters)
{
var paramsString = new StringBuilder();
try
{
int index = 1;
foreach (var parameter in parameters)
{
var paramValue = "null";
var paramType = "null";
if (parameter != null)
{
paramValue = parameter.ToString();
paramType = parameter.GetType().Name;
}
paramsString.AppendFormat("[Index={0}, Type={1}, Value={2}], ", index++, paramType, paramValue);
}
}
catch (Exception)
{
paramsString.Append("Exception when converting parameters to string");
}
return paramsString.ToString();
}
private object ReadCollection(Stream s)
{
object returnValue;
var listItemTypeAsChar = Convert.ToChar(s.ReadByte());
int numOfItemsInList = SerDe.ReadInt(s);
switch (listItemTypeAsChar)
{
case 'c':
var strList = new List<string>();
for (int itemIndex = 0; itemIndex < numOfItemsInList; itemIndex++)
{
strList.Add(SerDe.ReadString(s));
}
returnValue = strList;
break;
case 'i':
var intList = new List<int>();
for (int itemIndex = 0; itemIndex < numOfItemsInList; itemIndex++)
{
intList.Add(SerDe.ReadInt(s));
}
returnValue = intList;
break;
case 'd':
var doubleList = new List<double>();
for (int itemIndex = 0; itemIndex < numOfItemsInList; itemIndex++)
{
doubleList.Add(SerDe.ReadDouble(s));
}
returnValue = doubleList;
break;
case 'b':
var boolList = new List<bool>();
for (int itemIndex = 0; itemIndex < numOfItemsInList; itemIndex++)
{
boolList.Add(Convert.ToBoolean(s.ReadByte()));
}
returnValue = boolList;
break;
case 'r':
var byteArrayList = new List<byte[]>();
for (int itemIndex = 0; itemIndex < numOfItemsInList; itemIndex++)
{
var byteArrayLen = SerDe.ReadInt(s);
byteArrayList.Add(SerDe.ReadBytes(s, byteArrayLen));
}
returnValue = byteArrayList;
break;
case 'j':
var jvmObjectReferenceList = new List<JvmObjectReference>();
for (int itemIndex = 0; itemIndex < numOfItemsInList; itemIndex++)
{
var itemIdentifier = SerDe.ReadString(s);
jvmObjectReferenceList.Add(new JvmObjectReference(itemIdentifier));
}
returnValue = jvmObjectReferenceList;
break;
default:
// convert listItemTypeAsChar to UInt32 because the char may be non-printable
throw new NotSupportedException(
string.Format("Identifier for list item type 0x{0:X} not supported",
Convert.ToUInt32(listItemTypeAsChar)));
}
return returnValue;
}
public void Dispose()
{
ISocketWrapper socket;
while (sockets.TryDequeue(out socket))
{
if (socket != null)
{
socket.Dispose();
}
}
}
}
}