JVM Initialization for C++ examples
The source code
This procedure checks an evironment variable called RUNOPTIONS and interprets its value as the name of a file which contains the configuration information for the JMS C++ example. In addition to JVM initialization settings like classpath, etc., this file is expected to contain some property definitions for JNDI lookup names. This scheme makes a lot of sense for our examples, because we need to be able to demonstrate JMS vendor independence. For you, a less flexible and more easily maintained scheme might make more sense. You could for example hardcode all configuration information.
Don't be confused by constants like XMOG_FILE_SEPARATOR_STR. This is simply a platform-dependent value that you may use at your convenience to write platform-portable code. You could just as easily use your own macros or write platform-specific source code if portability is not important to you.
#include "java_lang_pkg.h" #include "xmog_util.h" #include <stdio.h> /// /// @brief An example JVM initialization method. /// /// You can use different schemes for the loading of the JVM. Please /// take a look at the rtconfig examples that illustrate several different /// ways (XML config file, etc.) /// /// This function first checks the environment variable 'RUNOPTIONS' for /// the name of a configuration file. If that variable is undefined or /// the specified file cannot be found, we check the installation hierarchy /// for the default file 'runoptions.xml'. /// /// The RUNOPTIONS design makes sense for us because we want to support many /// different JMS implementations with one set of examples, without requiring /// you to rebuild with different configuration settings or lookup names. /// /// For your own (JMS provider targeted) project, you might hardcode the /// configuration settings and you would certainly know the lookup names for /// your queues and topics, so you would not have to make them configurable. /// void init_jvm( int argc, char * argv[] ) { // try to get the RUNOPTIONS config file from an environment variable char * RUNOPTIONS = getenv( "RUNOPTIONS" ); // do a sanity check on the value and reset the variable if we can't find our // configuration file if( RUNOPTIONS != NULL ) { if( 0 != xmog_util::hasFileAccess( RUNOPTIONS, Read ) ) RUNOPTIONS = NULL; } // if it is not available, use a relative path and (assuming we're in the // install hierarchy) if( RUNOPTIONS == NULL ) { RUNOPTIONS = ".." XMOG_FILE_SEPARATOR_STR ".." XMOG_FILE_SEPARATOR_STR "runoptions.xml"; // do a sanity check on the value and reset it if we can't find our // configuration file if( 0 != xmog_util::hasFileAccess( RUNOPTIONS, Read ) ) RUNOPTIONS = NULL; } // if it is not available, use another relative path (assuming we're in the // install hierarchy) if( RUNOPTIONS == NULL ) { RUNOPTIONS = ".." XMOG_FILE_SEPARATOR_STR "runoptions.xml"; // do a sanity check on the value and reset it if we can't find our // configuration file if( 0 != xmog_util::hasFileAccess( RUNOPTIONS, Read ) ) RUNOPTIONS = NULL; } xmog_jvm_loader & loader = xmog_jvm_loader::get_jvm_loader( RUNOPTIONS, true, true, TraceAll, TraceErrors ); // throw an exception if we couldn't identify a valid distribution // you can make this test as foolproof as you wish if( RUNOPTIONS == NULL ) xmog_error_handler( &loader ).handleFrameworkException( -2000, "Couldn't locate configuration file (examples/runoptions.xml)" ); // this will print out the required LD_LIBRARY_PATH for Unix-style platforms // the pattern here is a double invocation: // 1) get the configured JVM, derive the required runtime path from it, and exit // 2) invoke again with the required runtime path and load the JVM if( argc > 1 && !strcmp( argv[ 1 ], "-info" ) ) loader.printLdLibraryPathAndExit(); // this will throw an 'xmog_exception' typed framework exception // if something goes wrong // potentially, it might also throw a Java proxy exception if you // haven't granted the privileges required for the framework to // initialize itself else loader.load(); // try to find some classes that should be present to quickly diagnose // configuration problems Class::forName( "javax.jms.Message" ); }
