Skip to content

Commit

Permalink
add jvm type
Browse files Browse the repository at this point in the history
  • Loading branch information
vintagewang committed Jun 9, 2012
1 parent de50a42 commit c3fc38d
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 29 deletions.
3 changes: 2 additions & 1 deletion bin/jwrapper.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<java>
<javahome>C:\myapp\jdk1.6.0_26</javahome>
<javahome>C:\dummy_java_home</javahome>
<jvmtype>hotspot</jvmtype>
<mainclass>Hello</mainclass>
<classpaths>
<cp>cp1</cp>
Expand Down
65 changes: 54 additions & 11 deletions src/JWUtil.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,66 @@
#include "JWUtil.h"
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef WIN32
#include <Windows.h>
#endif

namespace JWUtil
{
std::string getCurrentExeFilePath()
{
std::string result;
#ifdef WIN32
char bufFileName[512 + 1] = {0};
GetModuleFileNameA(NULL, bufFileName, sizeof(bufFileName) - 1);
result = bufFileName;

//
// remove file extention
//
std::string::size_type pos = result.find_last_of('.');
if(pos != std::string::npos) {
result = result.substr(0, pos);
}
char path_buffer[_MAX_PATH + 1] = {0};
char drive[_MAX_DRIVE + 1] = {0};
char dir[_MAX_DIR + 1] = {0};
char fname[_MAX_FNAME + 1] = {0};
char ext[_MAX_EXT + 1] = {0};

GetModuleFileNameA(NULL, path_buffer, sizeof(path_buffer) - 1);
_splitpath(path_buffer, drive, dir, fname, ext);

result += drive;
result += dir;
result += fname;
#else
#endif

return result;
}

std::string getWorkingDir()
{
std::string result;
#ifdef WIN32
char path_buffer[_MAX_PATH + 1] = {0};
_getcwd(path_buffer, sizeof(path_buffer) - 1);
result = path_buffer;
#else
char path_buffer[MAX_PATH + 1] = {0};
getcwd(path_buffer, sizeof(path_buffer) - 1);
result = path_buffer;
#endif

return result;
}

std::string getCurrentExeFileDir()
{
std::string result;
#ifdef WIN32
char path_buffer[_MAX_PATH + 1] = {0};
char drive[_MAX_DRIVE + 1] = {0};
char dir[_MAX_DIR + 1] = {0};
char fname[_MAX_FNAME + 1] = {0};
char ext[_MAX_EXT + 1] = {0};

GetModuleFileNameA(NULL, path_buffer, sizeof(path_buffer) - 1);
_splitpath(path_buffer, drive, dir, fname, ext);

result += drive;
result += dir;
#else
#endif

Expand Down
11 changes: 11 additions & 0 deletions src/JWUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

namespace JWUtil
{
/**
* 获取工作目录
*/
std::string getWorkingDir();
/**
* 获取当前程序可执行程序所在目录
*/
std::string getCurrentExeFileDir();
/**
* 获取当前程序可执行程序的全路径,含文件名
*/
std::string getCurrentExeFilePath();
};

Expand Down
42 changes: 31 additions & 11 deletions src/JavaConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,44 @@ bool JavaConfig::load()
{
ptree pt;
read_xml(getConfigFile().c_str(), pt);
this->javaHome = pt.get<string>("java.javahome");
this->mainClass = pt.get<string>("java.mainclass");

BOOST_AUTO(child, pt.get_child("java.classpaths"));
for(BOOST_AUTO(pos, child.begin());
pos != child.end();
++pos) {
this->classPathList.push_back(pos->second.data());
try {
// java home
const char* jh = getenv("JAVA_HOME");
if(!jh) jh = "";
this->javaHome = pt.get("java.javahome", jh);

// JVM Type
this->jvmType = pt.get<string>("java.jvmtype");

// Main class
this->mainClass = pt.get<string>("java.mainclass");

// Class path
BOOST_AUTO(child, pt.get_child("java.classpaths"));
for(BOOST_AUTO(pos, child.begin());
pos != child.end();
++pos) {
this->classPathList.push_back(pos->second.data());
}
} catch(const std::exception& e) {
fprintf(stderr, "%s\n", e.what());
return false;
} catch(...) {
fprintf(stderr, "Unexpected exception.%s\n");
return false;
}

return true;
}

void JavaConfig::printAll()
{
printf("JAVA_HOME = [%s]\n", this->javaHome.c_str());
printf("MAIN CLASS = [%s]\n", this->mainClass.c_str());
printf("javaHome = [%s]\n", this->javaHome.c_str());
printf("jvmType = [%s]\n", this->jvmType.c_str());
printf("mainClass = [%s]\n", this->mainClass.c_str());

for(size_t i = 0; i < this->classPathList.size(); i++) {
printf("CLASS PATH %2d = [%s]\n", i, this->classPathList[i].c_str());
printf("classPathList %2d = [%s]\n", i, this->classPathList[i].c_str());
}

printf("EXE = [%s]\n", this->getConfigFile().c_str());
Expand Down
15 changes: 9 additions & 6 deletions src/JavaConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ typedef std::vector<std::string> ClassPathList;

class JavaConfig
{
public:
// Java Home Dir
std::string javaHome;
// JVM Type
std::string jvmType;
// Main Class
std::string mainClass;
// Class Path List
ClassPathList classPathList;

public:
JavaConfig();
~JavaConfig();
static JavaConfig* getInstance();
Expand All @@ -20,11 +28,6 @@ class JavaConfig

private:
static std::string getConfigFile();

private:
std::string javaHome;
std::string mainClass;
ClassPathList classPathList;
};

#endif // end of _JW_JAVA_CONFIG_H__
6 changes: 6 additions & 0 deletions src/jwrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#include "JavaConfig.h"
#include "JWUtil.h"
#include <stdio.h>
#include <stdlib.h>


int main(int argc, char** argv)
{
bool result = JavaConfig::getInstance()->load();
if(result) {
JavaConfig::getInstance()->printAll();
}

system("PAUSE");
return 0;
}

0 comments on commit c3fc38d

Please sign in to comment.