コード例 #1
0
        private object CallJavaMethod(bool isStatic, object classNameOrJvmObjectReference, string methodName, params object[] parameters)
        {
            object returnValue = null;

            try
            {
                var overallPayload = PayloadHelper.BuildPayload(isStatic, classNameOrJvmObjectReference, methodName, parameters);

                Socket socket = GetConnection();
                using (NetworkStream s = new NetworkStream(socket))
                {
                    SerDe.Write(s, overallPayload);

                    var isMethodCallFailed = SerDe.ReadInt(s);
                    //TODO - add boolean instead of int in the backend
                    if (isMethodCallFailed != 0)
                    {
                        var    jvmFullStackTrace = SerDe.ReadString(s);
                        string errorMessage      = string.Format("JVM method execution failed: {0}", methodName);
                        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 = ReadJvmObjectReferenceCollection(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);
        }
コード例 #2
0
ファイル: JvmBridge.cs プロジェクト: guiling/SparkCLR
        private object ReadCollection(NetworkStream 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);
        }