Skip to content

Commit

Permalink
update java support
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Sep 25, 2021
1 parent fafebe4 commit 3d925ac
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
1 change: 0 additions & 1 deletion auto/install
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ ExecStop=/bin/kill -s QUIT \$MAINPID
PrivateTmp=true
#Restart=on-failure
#RestartSec=10s
#EnvironmentFile=$NGX_PREFIX/conf/env.conf

[Install]
WantedBy=multi-user.target
Expand Down
50 changes: 18 additions & 32 deletions module/ngx_http_hi_module/lib/java.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

#include <jni.h>
#include <string>
#include <cstring>
#include <ctime>
#include <vector>
#include <utility>
#include <unordered_map>
#include "file_mmap.hpp"
#include "utils.hpp"

namespace hi
{
Expand Down Expand Up @@ -53,7 +55,7 @@ namespace hi
{
public:
java(const std::string &classpath, const std::string &jvmoptions, int v)
: jvm(0), args(), options(), ok(false), env(0), version(v), request(0), response(0), hashmap(0), arraylist(0), iterator(0), set(0), request_ctor(0), response_ctor(0), hashmap_put(0), hashmap_get(0), hashmap_keyset(0), arraylist_get(0), arraylist_size(0), arraylist_iterator(0), hasnext(0), next(0), set_iterator(0), status(0), content(0), client(0), user_agent(0), method(0), uri(0), param(0), req_headers(0), form(0), cookies(0), req_session(0), req_cache(0), res_headers(0), res_session(0), res_cache(0)
: jvm(0), args(), options(0), ok(false), env(0), version(v), request(0), response(0), hashmap(0), arraylist(0), iterator(0), set(0), request_ctor(0), response_ctor(0), hashmap_put(0), hashmap_get(0), hashmap_keyset(0), arraylist_get(0), arraylist_size(0), arraylist_iterator(0), hasnext(0), next(0), set_iterator(0), status(0), content(0), client(0), user_agent(0), method(0), uri(0), param(0), req_headers(0), form(0), cookies(0), req_session(0), req_cache(0), res_headers(0), res_session(0), res_cache(0)
{
this->ok = this->create_vm(classpath, jvmoptions);
}
Expand All @@ -64,6 +66,10 @@ namespace hi
{
this->free_vm();
}
if (this->options)
{
free(this->options);
}

this->request = 0;
this->response = 0;
Expand Down Expand Up @@ -108,7 +114,7 @@ namespace hi
private:
JavaVM *jvm;
JavaVMInitArgs args;
JavaVMOption options[2];
JavaVMOption *options;
bool ok;

public:
Expand All @@ -122,6 +128,7 @@ namespace hi
private:
bool create_vm(const std::string &classpath, const std::string &jvmoptions)
{
JNI_GetDefaultJavaVMInitArgs(&this->args);
switch (this->version)
{
case 1:
Expand Down Expand Up @@ -152,40 +159,19 @@ namespace hi
#endif
break;
}
JNI_GetDefaultJavaVMInitArgs(&this->args);
this->args.nOptions = 2;
char *env_clspath = std::getenv("CLASSPATH");
std::string pre("-Djava.class.path=");
std::string clspath(pre);
if (env_clspath)
{
clspath.append(env_clspath).append(":");
size_t p = classpath.find(pre);
if (p == std::string::npos)
{
clspath.append(classpath);
}
else
{
clspath.append(classpath.substr(p + pre.size()));
}
this->options[0].optionString = const_cast<char *>(clspath.c_str());
}
else
std::vector<std::string> opt_vec;
splite(classpath, opt_vec, " ");
splite(jvmoptions, opt_vec, " ");
this->args.nOptions = opt_vec.size();
this->options = (JavaVMOption *)calloc(sizeof(JavaVMOption), this->args.nOptions);
if (this->options == NULL)
{
this->options[0].optionString = const_cast<char *>(classpath.c_str());
return false;
}

char *env_opt = std::getenv("JVMOPTIONS");
if (env_opt)
for (size_t i = 0; i < this->args.nOptions; ++i)
{
this->options[1].optionString = const_cast<char *>((jvmoptions + " " + env_opt).c_str());
this->options[i].optionString = const_cast<char *>(opt_vec[i].data());
}
else
{
this->options[1].optionString = const_cast<char *>(jvmoptions.c_str());
}

this->args.options = this->options;
this->args.ignoreUnrecognized = JNI_TRUE;
int rv = JNI_CreateJavaVM(&this->jvm, (void **)&this->env, &this->args);
Expand Down
16 changes: 14 additions & 2 deletions module/ngx_http_hi_module/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extern "C"
#include <memory>
#include <sstream>
#include <string>
#include <cstring>
#include <unordered_map>
#include <vector>

Expand Down Expand Up @@ -113,6 +114,17 @@ namespace hi
return std::string((char *)tmp, SHA512_DIGEST_LENGTH * 2);
}

static void splite(const std::string &str, std::vector<std::string> &v, const char *delimiters)
{
char tmp[str.size()];
strcpy(tmp, str.data());
char *p = strtok(tmp, delimiters);
while (p)
{
v.push_back(p);
p = strtok(NULL, delimiters);
}
}
class aes
{
public:
Expand Down Expand Up @@ -220,7 +232,7 @@ namespace hi
return std::string((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
}

static bool upload(hi::request &req, const std::string& body, ngx_http_core_loc_conf_t *clcf, ngx_http_request_t *r, const std::string &temp_dir, std::string &err_msg)
static bool upload(hi::request &req, const std::string &body, ngx_http_core_loc_conf_t *clcf, ngx_http_request_t *r, const std::string &temp_dir, std::string &err_msg)
{
bool result = false;
try
Expand Down Expand Up @@ -273,6 +285,6 @@ namespace hi
static void deserialize(const std::string &str, std::unordered_map<std::string, std::string> &m)
{
jsoncons::json j = jsoncons::json::parse(str);
m = std::move(j.as<std::unordered_map<std::string,std::string>>());
m = std::move(j.as<std::unordered_map<std::string, std::string>>());
}
} // namespace hi

0 comments on commit 3d925ac

Please sign in to comment.