static bool isInitialized = false; static char *gClasspath = NULL, *gDump = NULL, *gHostname = NULL; static JavaVM *jvm = NULL; static jclass stringClass = 0; static jmethodID appendMethod = 0; static jmethodID collectMethod = 0; static jmethodID executeMethod = 0; static jmethodID flushMethod = 0; static jmethodID monitorMethod = 0; static JNIEnv *env = NULL; static jobject toolInterface = 0; bool commandInterface::initialize (const char *layoutFileName, const char *classpath, const char *hostname, const bool dump) { JavaVMInitArgs vm_args; JavaVMOption options[(hostname == NULL) ? 2 : 3]; jint resultant; jmethodID initID; jstring xmlfile; // reset the initialization incase this not the first time being called isInitialized = false; vm_args.version = 0x00010004; vm_args.options = options; vm_args.nOptions = (hostname == NULL) ? 2 : 3; vm_args.ignoreUnrecognized = true; // only need to do this if we do not yet have JVM running if (jvm == NULL) { // Define the user class path if (gClasspath != NULL) delete[] gClasspath; gClasspath = new char[strlen (classpath) + 19]; sprintf (gClasspath, "-Djava.class.path=%s", classpath); options[0].optionString = gClasspath; gClasspath[strlen (classpath) + 18] = 0; // Define the dump flag for verbosity if (gDump != NULL) delete[] gDump; gDump = new char[dump ? 39 : 40]; sprintf (gDump, "-Dgov.nasa.jpl.testbed.control.dump=%s", (dump ? "true" : "false")); options[1].optionString = gDump; gDump[dump ? 38 : 39] = 0; // Define hostname if given if (hostname != NULL) { if (gHostname != NULL) delete[] gHostname; gHostname = new char[strlen (hostname) + 23]; sprintf (gHostname, "-Djava.lang.host.name=%s", hostname); options[2].optionString = gHostname; gHostname[strlen (hostname) + 22] = 0; } // Start the JVM resultant = JNI_CreateJavaVM (&jvm, (void**)&env, &vm_args); if (resultant < 0 || jvm == NULL) { std::cerr << "Can't create Java VM" << std::endl; return isInitialized; } // New JVM, so lets make sure we find the class toolInterface = 0; } // We currently have JVM, but now we need an instance of the Factor.class if (toolInterface == 0) { jclass theClass; // Go get the class theClass = env->FindClass ("gov/nasa/jpl/testbed/command/impl/ExternalToolInterface"); if (theClass == 0) { std::cerr << "Can't find the class " << "gov.nasa.jpl.testbed.command.impl.ExternalToolInterfaces" << std::endl; return isInitialized; } // Get the constructor for the class initID = env->GetMethodID (theClass, "", "()V"); if (initID == 0) { std::cerr << "Could not find the constructor." << std::endl; return isInitialized; } // Make an object from the class toolInterface = env->NewObject (theClass, initID, NULL); if (toolInterface == 0) { std::cerr << "Could not create the tool interface object." << std::endl; return isInitialized; } // Make global so that the JVM does not unload it on me toolInterface = env->NewGlobalRef (toolInterface); if (toolInterface == 0) { std::cerr << "Could not globally define the tool interface." << std::endl; return isInitialized; } // Have the class and instance, so lets get the init method initID = env->GetMethodID (theClass, "initialize", "(Ljava/lang/String;)Z"); if (initID == 0) { std::cerr << "Could not find the method for loading " << "the configuration" << std::endl; env->DeleteGlobalRef (toolInterface); toolInterface = 0; return isInitialized; } // For convience, look up classes and methods being used by everyone else stringClass = env->FindClass ("java/lang/String"); if (stringClass == 0) { std::cerr << "Could not find the class java.lang.String!!" << std::endl; return isInitialized; } appendMethod = env->GetMethodID (theClass, "append", "([Ljava/lang/String;Z)[Ljava/lang/String;"); if (appendMethod == 0) { std::cerr << "Could not find the appropriate append method." << std::endl; env->DeleteGlobalRef (toolInterface); toolInterface = 0; return isInitialized; } collectMethod = env->GetMethodID (theClass, "collect", "(Ljava/lang/String;ZZLgov/nasa/jpl/testbed/command/impl/ExternalToolResponse;)Z"); if (collectMethod == 0) { std::cerr << "Could not find the appropriate collect method." << std::endl; env->DeleteGlobalRef (toolInterface); toolInterface = 0; return isInitialized; } executeMethod = env->GetMethodID (theClass, "execute", "(Z)Z"); if (executeMethod == 0) { std::cerr << "Could not find the appropriate execute method." << std::endl; env->DeleteGlobalRef (toolInterface); toolInterface = 0; return isInitialized; } flushMethod = env->GetMethodID (theClass, "flush", "()V"); if (flushMethod == 0) { std::cerr << "Could not find the appropriate flush method." << std::endl; env->DeleteGlobalRef (toolInterface); toolInterface = 0; return isInitialized; } monitorMethod = env->GetMethodID (theClass, "monitor", "(Ljava/lang/String;Z)V"); if (flushMethod == 0) { std::cerr << "Could not find the appropriate monitor method." << std::endl; env->DeleteGlobalRef (toolInterface); toolInterface = 0; return isInitialized; } } if (toolInterface != 0) { // Finally, initialize the system xmlfile = env->NewStringUTF (layoutFileName); isInitialized = env->CallBooleanMethod (toolInterface, initID, xmlfile); } return isInitialized; }