📄 java_vm.cpp
字号:
/*- @filedef
-*
-* @copyright (c) 2006 XW
-*
-* @description:
-*
-*
-* @create :
-* @date : 2006-11-2
-* @time : 16:31:22
-* @author : XiaoWei
-*
-*
-* @file : JAVA_VM.cpp
-* @version : 1.0.0.0
-* @note : <JAVA_VM>
-*
-*/
#include <string>
#include <fstream>
#include "../header/JAVA_VM.h"
#include <windows.h>
#include "../header/JAVA_Base.h"
#include "../header/XLock.h"
static XCSLock g_lock;
#define T_LOCK() _XCSLock __lock(g_lock)
JAVA_VM::JAVA_VM():m_jar("")
{
m_lpMainEnv = 0;
m_lpVM = 0;
m_StrVec.clear();
}
JAVA_VM::~JAVA_VM()
{
endVM();
}
JAVA_VM* JAVA_VM::getInstance()
{
static JAVA_VM* s_lpVMManager = (JAVA_VM*)0;
if (s_lpVMManager == (JAVA_VM*)0) {
s_lpVMManager = new JAVA_VM();
}
return s_lpVMManager;
}
/*- @functiondef
-*
-* @type : class <JAVA_VM> member function
-* @name : startVM
-* @return : bool
-*
-* @parameter : long _Ver
-*
-* @description:
-*
-*
-* @create :
-* @date : 2006-11-2
-* @time : 16:31:22
-* @author : XiaoWei
-*
-*/
bool JAVA_VM::startVM(long _Ver)
{
if (m_lpVM == 0) {
JavaVMInitArgs vm_args;
JavaVMOption options[2];
const char * __lpClassPath = getenv("CLASSPATH");
if (!__lpClassPath) {
SHOWMESSAGE("Not ClassPath!");
return false;
}
std_string __strClassPath= __lpClassPath;
m_strOldClassPath = __strClassPath;
__strClassPath = "-Djava.class.path=" + __strClassPath;
if (!m_jar.empty()) {
if (__strClassPath.find_last_of(';') != __strClassPath.length()-1) {
__strClassPath += ";";
}
__strClassPath += m_jar ;
}
options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = const_cast<char*>(__strClassPath.c_str());
vm_args.version = _Ver;
vm_args.nOptions = 2;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
m_MainThreadID = ::GetCurrentThreadId();
int res = JNI_CreateJavaVM(&m_lpVM, (void**)&m_lpMainEnv, &vm_args);
if (res<0) {
return false;
}
}
return true;
}
/*- @functiondef
-*
-* @type : class <JAVA_VM> member function
-* @name : endVM
-* @return : bool
-*
-* @description:
-*
-*
-* @create :
-* @date : 2006-11-2
-* @time : 16:31:22
-* @author : XiaoWei
-*
-*/
bool JAVA_VM::endVM()
{
if (m_lpVM) {
m_lpVM->DestroyJavaVM();
m_lpMainEnv = NULL;
m_lpVM = NULL;
JAVA_VM * __lpVM = JAVA_VM::getInstance();
delete __lpVM;
}
return true;
}
JNI_VM * JAVA_VM::getVM() const
{
return m_lpVM;
}
/*- @functiondef
-*
-* @type : class <JAVA_VM> member function
-* @name : getEnv
-* @return : JNI_Env*
-*
-* @description:
-*
-*
-* @create :
-* @date : 2006-11-2
-* @time : 16:31:22
-* @author : XiaoWei
-*
-*/
JNI_Env* JAVA_VM::getEnv()
{
long __ThreadID = ::GetCurrentThreadId();
if (m_MainThreadID == __ThreadID) {
return m_lpMainEnv;
}
return m_EnvMap[__ThreadID];
}
bool JAVA_VM::loadjarLibrary(const char * _lpFile)
{
std_string __jarfile = _getJarPath(_lpFile);
char __fullName[MAX_PATH];
DWORD _len = GetFullPathName(__jarfile.c_str(),MAX_PATH,__fullName,0);
if (_len > 0) {
m_jar += __fullName;
m_jar += ";";
}else{
std_string strmsg = "can not get jar file: ";
strmsg += _lpFile;
SHOWMESSAGE(strmsg.c_str());
return false;
}
return true;
}
/*- @functiondef
-*
-* @type : class <JAVA_VM> member function
-* @name : getClassForNew
-* @return : JAVA_Class
-*
-* @parameter : const char* _lpClassSig
-*
-* @description:
-*
-*
-* @create :
-* @date : 2006-11-2
-* @time : 16:31:22
-* @author : XiaoWei
-*
-*/
bool JAVA_VM::getClassForNew(const char* _lpClassSig,JAVA_Class & _class)
{
T_LOCK();/*--by XiaoWei 2007-4-30-- *///
try
{
std_string __strSig = _lpClassSig;
_ClassMapIt it = m_ClassMap.find(__strSig);
if (it != m_ClassMap.end()) {
_class = it->second;
}else{
JAVA_Class __class ;
JNI_Env* _lpEnv = getEnv();
JNI_Class _jniclass = JAVA_Base::FindClass(_lpEnv,_lpClassSig);
JAVA_ASSERT_MSG(_jniclass==JNI_NULL,_lpClassSig);
__class.setClass(_jniclass);
__class.setEnv(_lpEnv);
m_ClassMap[__strSig] = __class;
_class = __class;
}
}catch (...) {
return false;
}
return true;
}
/*- @functiondef
-*
-* @type : class <* JAVA_VM> member function
-* @name : startSubThread
-* @return : JNI_Env
-*
-* @description:
-*
-*
-* @create :
-* @date : 2006-11-2
-* @time : 16:31:22
-* @author : XiaoWei
-*
-*/
JNI_Env * JAVA_VM::startSubThread()
{
DWORD __ThreadId = ::GetCurrentThreadId();
_ID2EnvIt _it = m_EnvMap.find(__ThreadId);
JNI_Env* env = JNI_NULL;
if (_it == m_EnvMap.end()) {
m_lpVM->AttachCurrentThread((void**)&env, NULL);
if (env) {
m_EnvMap[__ThreadId] = env;
}
env = JNI_NULL;
}else
{
env = _it->second;
}
return env;
}
/*- @functiondef
-*
-* @type : class <JAVA_VM> member function
-* @name : endSubThread
-* @return : void
-*
-* @description:
-*
-*
-* @create :
-* @date : 2006-11-2
-* @time : 16:31:22
-* @author : XiaoWei
-*
-*/
void JAVA_VM::endSubThread()
{
DWORD __ThreadId = ::GetCurrentThreadId();
_ID2EnvIt _it = m_EnvMap.find(__ThreadId);
if (_it != m_EnvMap.end()) {
m_lpVM->DetachCurrentThread();
m_EnvMap.erase(_it);
}
}
bool JAVA_VM::__isFileExist(const char* __lpFileName)
{
FILE * __lpFile = fopen(__lpFileName,"r");
if (__lpFile) {
fclose(__lpFile);
return true;
}
return false;
}
std_string JAVA_VM::__getJarPath(const char *__lpJarFile)
{
for(_StrVecIt it = m_StrVec.begin(); it != m_StrVec.end(); ++it)
{
std_string __lpJarFileTemp = *it;
__lpJarFileTemp += __lpJarFile;
if (__isFileExist(__lpJarFileTemp.c_str())) {
return __lpJarFileTemp;
}
}
return std_string("");
}
/*- @functiondef
-*
-* @type : class <JAVA_VM> member function
-* @name : _getJarPath
-* @return : std_string
-*
-* @parameter : const char* __lpJarFile
-*
-* @description:
-*
-*
-* @create :
-* @date : 2006-11-2
-* @time : 16:31:22
-* @author : XiaoWei
-*
-*/
std_string JAVA_VM::_getJarPath(const char* __lpJarFile)
{
std_string __str = __lpJarFile;
if (__isFileExist(__lpJarFile)) {
return std_string(__lpJarFile);
}
LPCSTR __lpCmdLine = GetCommandLine();
__str = __lpCmdLine;
size_t _size = __str.find_last_of('\\');
if (_size>0) {
__str = __str.substr(0,_size);
__str += "\\";
__str += __lpJarFile;
if (__isFileExist(__str.c_str())) {
return __str;
}
}
if (m_StrVec.size() <= 0) {
__getPathEnv();
}
__str = __getJarPath(__lpJarFile);
return __str;
}
/*- @functiondef
-*
-* @type : class <JAVA_VM> member function
-* @name : __getPathEnv
-* @return : void
-*
-* @description:
-*
-*
-* @create :
-* @date : 2006-11-2
-* @time : 16:31:22
-* @author : XiaoWei
-*
-*/
void JAVA_VM::__getPathEnv()
{
char * __lpClassPath = getenv("PATH");
if (!__lpClassPath) {
SHOWMESSAGE("Not PATH!");
return ;
}
std_string __str = __lpClassPath;
char * __lpStart = __lpClassPath;
char temp = *__lpClassPath;
while (temp != '\0') {
if (temp == ';') {
std_string __strTemp(__lpStart,__lpClassPath-__lpStart);
__lpStart = ++__lpClassPath;
size_t __lastIndex= __strTemp.length() -1;
if (__strTemp[__lastIndex] != '\\' || __strTemp[__lastIndex] != '/' ) {
__strTemp += "\\";
}
m_StrVec.push_back(__strTemp);
}
++__lpClassPath;
temp = *__lpClassPath;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -