TyErrorId JEDIIEngine::initializeImpl()

in src/framework/internal_jedii_engine.cpp [611:838]


    TyErrorId JEDIIEngine::initializeImpl(AnalysisEngineDescription const & specifier) {
      util::Trace clTrace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);

      UIMA_TPRINT("initializing JVM DLL");
      initializeJVMFunctions();
      UIMA_TPRINT("JVM DLL loaded");

      assert( iv_createJavaVMFunc != NULL );
      assert( iv_getCreatedJavaVMsFunc != NULL );

      if (specifier.isPrimitive()) {
        iv_isPrimitive = 1;
      } else {
        iv_isPrimitive = 2;
      }

      icu::UnicodeString config;
      specifier.toXMLBuffer(config);

//         ofstream ofs("jedii_tmp_specifier.xml"); ofs << config << endl; ofs.close();

      UIMA_TPRINT("Initializing Java VM");
      initializeJavaVM();
      UIMA_TPRINT("VM initialized");

      assert( EXISTS(iv_env) );
      assert( EXISTS(iv_vm) );

      TyErrorId err = UIMA_ERR_NONE;

      UIMA_TPRINT("setup VM");
      // create Java object and initialize method IDs
      assert( EXISTS(iv_env) );
      jclass clazz = iv_env->FindClass("org/apache/uima/uimacpp/CppUimajEngine");
      checkForUnexpectedJNIException();
      jmethodID constructor = iv_env->GetMethodID(clazz, "<init>", "()V");
      checkForUnexpectedJNIException();

      iv_initializeMethod = iv_env->GetMethodID(clazz, "initialize", "("
                            "Ljava/lang/String;"  // config,
                            "Ljava/lang/String;"  // datapath
                            "[I"                  // typeInheritance
                            "[I"                  // typePriorities
                            "[I"                  // featureDefs
                            "[I"                  // featureOffset
                            "[Ljava/lang/String;" // typeNames
                            "[Ljava/lang/String;" // featureNames
                            "[I"                  // stringSubTypes
                            "[Ljava/lang/String;" // stringSubTypeValues
                            "[I"                  // stringSubTypeValuePos
                            "[Ljava/lang/String;" // indexIDs
                            "[I"                  // indexKinds
                            "[I"                  // compStarts
                            "[I"                  // compDefs
                            ")I");
      checkForUnexpectedJNIException();

      //check if UIMA Framework version is compatible
      iv_getVersionMethod = iv_env->GetStaticMethodID(clazz, "getVersion", "()Ljava/lang/String;");
      checkForUnexpectedJNIException();

      jobject versionStringObj = iv_env->CallStaticObjectMethod(clazz, iv_getVersionMethod);
      checkForUnexpectedJNIException();
      jstring versionString = (jstring) versionStringObj;
      const TCHAR ** compatibleVersions = gs_compatibleUIMAVersions;
      if (versionString != NULL) {
        cout << versionString << endl;
        jboolean isCopy;
        jchar const * jc = iv_env->GetStringChars(versionString, &isCopy);
        checkForUnexpectedJNIException();
        jsize js = iv_env->GetStringLength(versionString);
        checkForUnexpectedJNIException();
        icu::UnicodeString  uVersion((UChar const *) jc, (size_t) js);
        cout << uVersion << endl;
        bool compatible = false;

        for (int i=0; i < NUM_COMPATIBLE_UIMA_JAVA_VERSIONS; i++) {
          if (uVersion.compare(compatibleVersions[i]) == 0) {
            compatible=true;
          }
        }

        if (!compatible) {
          ErrorMessage msg = ErrorMessage(UIMA_MSG_ID_INCOMPATIBLE_UIMA_JAVA_VERSION);
          msg.addParam(uVersion);
          for (int i=0; i < NUM_COMPATIBLE_UIMA_JAVA_VERSIONS; i++) {
            msg.addParam(compatibleVersions[i]);
          }
          UIMA_EXC_THROW_NEW(JavaException,
                             UIMA_ERR_JAVA_EXCEPTION,
                             msg,
                             UIMA_MSG_ID_INCOMPATIBLE_UIMA_JAVA_VERSION,
                             ErrorInfo::unrecoverable);
        }
        iv_env->ReleaseStringChars(versionString, jc);
        checkForUnexpectedJNIException();
      }  else {
        ErrorMessage msg = ErrorMessage(UIMA_MSG_ID_INCOMPATIBLE_UIMA_JAVA_VERSION);
        msg.addParam("could not determine UIMA Java version");
        UIMA_EXC_THROW_NEW(JavaException,
                           UIMA_ERR_JAVA_EXCEPTION,
                           msg,
                           UIMA_MSG_ID_INCOMPATIBLE_UIMA_JAVA_VERSION,
                           ErrorInfo::unrecoverable);
      }




      iv_processMethod = iv_env->GetMethodID(clazz, "process", "("
                                             "Ljava/lang/String;"  // doc
                                             "[I"                  // heap
                                             "[I"                  // fsIndex
                                             "[Ljava/lang/String;" // stringTable
                                             "[I"                  // resultSpecTypes
                                             "[I"                  // resultSpecFeatures
                                             "I"                   // sofanum
                                             "[B"         // byte heap
                                             "[S"      // short heap
                                             "[J"      // long heap
                                             ")I");
      checkForUnexpectedJNIException();
      iv_getByteHeapMethod = iv_env->GetMethodID(clazz, "getByteHeap", "()[B");
      checkForUnexpectedJNIException();
      iv_getShortHeapMethod = iv_env->GetMethodID(clazz, "getShortHeap", "()[S");
      checkForUnexpectedJNIException();
      iv_getLongHeapMethod = iv_env->GetMethodID(clazz, "getLongHeap", "()[J");
      checkForUnexpectedJNIException();






      iv_destroyMethod = iv_env->GetMethodID(clazz, "destroy", "()I");
      checkForUnexpectedJNIException();
      iv_getLastExceptionStringMethod = iv_env->GetMethodID(clazz, "getLastExceptionString", "()Ljava/lang/String;");
      checkForUnexpectedJNIException();
      iv_getHeapMethod = iv_env->GetMethodID(clazz, "getHeap", "()[I");
      checkForUnexpectedJNIException();
      iv_getIndexedFSsMethod = iv_env->GetMethodID(clazz, "getIndexedFSs", "()[I");
      checkForUnexpectedJNIException();
      iv_getStringTableMethod = iv_env->GetMethodID(clazz, "getStringTable", "()[Ljava/lang/String;");
      checkForUnexpectedJNIException();

      iv_batchProcessCompleteMethod = iv_env->GetMethodID(clazz, "batchProcessComplete", "()I");
      checkForUnexpectedJNIException();

      iv_collectionProcessCompleteMethod = iv_env->GetMethodID(clazz, "collectionProcessComplete", "()I");
      checkForUnexpectedJNIException();



      iv_obj = iv_env->NewObject(clazz, constructor);
      checkForUnexpectedJNIException();
      iv_obj = iv_env->NewGlobalRef(iv_obj);
      checkForUnexpectedJNIException();
      UIMA_TPRINT("setup VM complete");


      // serialize type system and pass it to the initialize method
      CASSerializer serializer(true);
      SerializedCAS serializedCAS;
      serializer.serializeDefinitions( *iv_casDefinition, serializedCAS);
      UIMA_TPRINT("definitions serialized");
      {
        jint pushFrameResult = iv_env->PushLocalFrame(serializedCAS.getNumberOfJavaObjectsToBeCreatedDefinitions() );
        checkForUnexpectedJNIException();
        handleEngineProxyError(pushFrameResult);
        checkForUnexpectedJNIException();

        // prepare arguments for initialize()
        jintArray typeInheritance = createJIntArray(serializedCAS.getTypeInheritanceTable() );
        jintArray typePriorities = createJIntArray(serializedCAS.getTypePriorityTable());
        jintArray featureDefs = createJIntArray(serializedCAS.getFeatureDefinitionTable());
        jintArray featureOffset = createJIntArray(serializedCAS.getFeatureOffsetTable());

        jobjectArray typeNames = createJStringArray(serializedCAS.getTypeSymbolTable());
        jobjectArray featureNames = createJStringArray(serializedCAS.getFeatureSymbolTable());

        jintArray subStringTypes = createJIntArray(serializedCAS.getStringSubTypes());
        jobjectArray subStringTypeValues = createJStringArray(serializedCAS.getStringSubTypeValues());
        jintArray subStringTypeValuePos = createJIntArray(serializedCAS.getStringSubTypeValuePos());

        jintArray indexKinds = createJIntArray(serializedCAS.getIndexKindTable());
        jintArray compStarts = createJIntArray(serializedCAS.getComparatorStartTable());
        jintArray compDefs = createJIntArray(serializedCAS.getComparatorDefinitionTable());
        jobjectArray indexIDs = createJStringArray(serializedCAS.getIndexIDTable());

        jstring conf = iv_env->NewString((jchar const *)config.getBuffer(), config.length());
        checkForUnexpectedJNIException();

        icu::UnicodeString dataPathUS(uima::ResourceManager::getInstance().getLocationData().getAsCString() );
        jstring dataPath = iv_env->NewString((jchar const *)dataPathUS.getBuffer(), dataPathUS.length() );
        checkForUnexpectedJNIException();

        UIMA_TPRINT("Java args created");
        // call initialize on org.apache.uima.uimacpp.CppUimajEngine
        jint initResult = iv_env->CallIntMethod(iv_obj, iv_initializeMethod,
                                                conf,
                                                dataPath,

                                                typeInheritance,
                                                typePriorities,
                                                featureDefs,
                                                featureOffset,
                                                typeNames,
                                                featureNames,

                                                subStringTypes,
                                                subStringTypeValues,
                                                subStringTypeValuePos,

                                                indexIDs,
                                                indexKinds,
                                                compStarts,
                                                compDefs);
        checkForUnexpectedJNIException();
        handleEngineProxyError(initResult);
        UIMA_TPRINT("Java initalize called");

        iv_env->PopLocalFrame(NULL);
        checkForUnexpectedJNIException();
      }

      UIMA_TPRINT("result spec initialized");
      return err;
    }