Skip to content

Commit

Permalink
begin jvm create
Browse files Browse the repository at this point in the history
  • Loading branch information
vintagewang committed Jun 10, 2012
1 parent 7abd96b commit 700515a
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 9 deletions.
90 changes: 90 additions & 0 deletions src/JavaConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/typeof/std/utility.hpp>
Expand Down Expand Up @@ -59,6 +61,11 @@ bool JavaConfig::load()
fprintf(stderr, "You must set JAVA_HOME environment variable or set it in config file.\n");
return false;
}

if(!this->expandMacro(this->javaHome)) {
return false;
}

JWUtil::setEnv("JAVA_HOME", this->javaHome.c_str());

// JVM Type
Expand Down Expand Up @@ -186,3 +193,86 @@ bool JavaConfig::expandMacro(std::string& value)

return result;
}

void JavaConfig::printLog(const char* fmt, ...)
{
if(this->debug) {
const static int BUFFER_SIZE = 1024 * 2;
char *buf = new char[BUFFER_SIZE + 1];
va_list args;

va_start(args, fmt);
vsnprintf(buf, BUFFER_SIZE, fmt, args);
va_end(args);

printf("%s\n", buf);

delete[] buf;
}
}

std::string JavaConfig::getJVMDllPath()
{
std::string result;
#if defined(WIN32)
result += this->javaHome;
result += "/jre/bin/";
result += this->jvmType;
result += "/jvm.dll";
#elif defined(SOLARIS_X86)
#if defined(ARCH64)
result += this->javaHome;
result += "/jre/lib/amd64";
result += this->jvmType;
result += "/libjvm.so";
#else
result += this->javaHome;
result += "/jre/lib/i386";
result += this->jvmType;
result += "/libjvm.so";
#endif
#elif defined(SOLARIS_X86)
#if defined(ARCH64)
result += this->javaHome;
result += "/jre/lib/sparcv9";
result += this->jvmType;
result += "/libjvm.so";
#else
result += this->javaHome;
result += "/jre/lib/sparc";
result += this->jvmType;
result += "/libjvm.so";
#endif
#elif defined(HPUX)
#if defined(ARCH64)
result += this->javaHome;
result += "/jre/lib/IA64W";
result += this->jvmType;
result += "/libjvm.so";
#else
result += this->javaHome;
result += "/jre/lib/IA64N";
result += this->jvmType;
result += "/libjvm.so";
#endif
#elif defined(LINUX)
#if defined(ARCH64)
result += this->javaHome;
result += "/jre/lib/amd64";
result += this->jvmType;
result += "/libjvm.so";
#else
result += this->javaHome;
result += "/jre/lib/i386";
result += this->jvmType;
result += "/libjvm.so";
#endif
#elif defined(AIX)
result += this->javaHome;
result += "/jre/bin/classic";
result += this->jvmType;
result += "/libjvm.a";
#endif

return result;
}
4 changes: 4 additions & 0 deletions src/JavaConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class JavaConfig

void printAll();

void printLog(const char* fmt, ...);

std::string getJVMDllPath();

private:
static std::string getConfigFile();
static bool expandMacro(std::string& value);
Expand Down
36 changes: 28 additions & 8 deletions src/JavaLauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/
#include "JavaLauncher.h"
#include "JavaConfig.h"
#include "DLL.h"

#define NULL_CHECK0(e) if ((e) == 0) return 0
#define NULL_CHECK(e) if ((e) == 0) return
Expand All @@ -32,9 +34,27 @@ JavaLauncher* JavaLauncher::getInstance()
return singleton;
}

bool JavaLauncher::launchJavaApp(int argc, char** argv)
int JavaLauncher::launchJavaApp(int argc, char** argv)
{
return false;
// 1, 获取JVM动态库全路径
std::string jvmPath = JavaConfig::getInstance()->getJVMDllPath();

// 2, 定义DLL对象,并加载JVM动态库
DLL jvmDll(jvmPath.c_str());

// 3, 获取创建JVM的函数指针
typedef jint(JNICALL *CreateJVMFun)(JavaVM**, void**, void*);
CreateJVMFun createJVMFun = (CreateJVMFun)jvmDll.getFunction("JNI_CreateJavaJVM");
if(NULL == createJVMFun){
fprintf(stderr, "Load JVM error %s\n", jvmPath.c_str());
return -1;
}
JavaConfig::getInstance()->printLog("Get JVM function OK.");

// 4, 创建JVM
JavaVMInitArgs jvmArgs;

return 0;
}

void JavaLauncher::exitHandler(int code)
Expand Down Expand Up @@ -67,7 +87,7 @@ jobject JavaLauncher::newPlatformString(JNIEnv *env, char *s)
if(isEncodingSupported(env, enc) == JNI_TRUE) {
NULL_CHECK0(cls = env->FindClass("java/lang/String"));
NULL_CHECK0(mid = env->GetMethodID(cls, "<init>",
"([BLjava/lang/String;)V"));
"([BLjava/lang/String;)V"));
str = env->NewObject(cls, mid, ary, enc);
} else {
/*If the encoding specified in sun.jnu.encoding is not
Expand All @@ -78,7 +98,7 @@ jobject JavaLauncher::newPlatformString(JNIEnv *env, char *s)
*/
NULL_CHECK0(cls = env->FindClass("java/lang/String"));
NULL_CHECK0(mid = env->GetMethodID(cls, "<init>",
"([B)V"));
"([B)V"));
str = env->NewObject(cls, mid, ary);
}
env->DeleteLocalRef(ary);
Expand All @@ -99,10 +119,10 @@ jobject JavaLauncher::getPlatformEncoding(JNIEnv *env)
jmethodID mid;
NULL_CHECK0(cls = env->FindClass("java/lang/System"));
NULL_CHECK0(mid = env->GetStaticMethodID(cls,
"getProperty",
"(Ljava/lang/String;)Ljava/lang/String;"));
"getProperty",
"(Ljava/lang/String;)Ljava/lang/String;"));
platformEncoding = env->CallStaticObjectMethod(
cls, mid, propname);
cls, mid, propname);
}
}
return platformEncoding;
Expand All @@ -114,7 +134,7 @@ jboolean JavaLauncher::isEncodingSupported(JNIEnv *env, jobject enc)
jmethodID mid;
NULL_CHECK0(cls = env->FindClass("java/nio/charset/Charset"));
NULL_CHECK0(mid = env->GetStaticMethodID(
cls,
cls,
"isSupported",
"(Ljava/lang/String;)Z"));
return env->CallStaticBooleanMethod(cls, mid, enc);
Expand Down
2 changes: 1 addition & 1 deletion src/JavaLauncher.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class JavaLauncher
~JavaLauncher();
static JavaLauncher* getInstance();

bool launchJavaApp(int argc, char** argv);
int launchJavaApp(int argc, char** argv);
static void exitHandler(int code);
static void abortHandler(int code);

Expand Down

0 comments on commit 700515a

Please sign in to comment.