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 28, 2021
1 parent 3d925ac commit 4c50c46
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 44 deletions.
50 changes: 17 additions & 33 deletions module/ngx_http_hi_module/java_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ namespace hi

static bool java_init_handler(ngx_http_hi_loc_conf_t *conf)
{
if (hi::java::JAVA_IS_READY)
return hi::java::JAVA_IS_READY;
if (!JAVA)
{
JAVA = std::make_shared<hi::java>((char *)conf->java_classpath.data, (char *)conf->java_options.data, conf->java_version);
if (JAVA->is_ok())
if (JAVA && JAVA->is_ok())
{
JAVA->request = JAVA->env->FindClass("hi/request");
if (JAVA->request != NULL)
Expand Down Expand Up @@ -57,12 +55,22 @@ namespace hi

JAVA->set = JAVA->env->FindClass("java/util/Set");
JAVA->set_iterator = JAVA->env->GetMethodID(JAVA->set, "iterator", "()Ljava/util/Iterator;");
hi::java::JAVA_IS_READY = true;

JAVA->servlet.SERVLET = JAVA->env->FindClass((const char *)conf->java_servlet.data);

if (JAVA->servlet.SERVLET != NULL)
{
JAVA->servlet.CTOR = JAVA->env->GetMethodID(JAVA->servlet.SERVLET, "<init>", "()V");
JAVA->servlet.GET_INSTANCE = JAVA->env->GetStaticMethodID(JAVA->servlet.SERVLET, "get_instance", "()Lhi/servlet;");
JAVA->servlet.HANDLER = JAVA->env->GetMethodID(JAVA->servlet.SERVLET, "handler", "(Lhi/request;Lhi/response;)V");
JAVA->servlet.t = time(0);
JAVA->set_inined(true);
}
}
}
}
}
return hi::java::JAVA_IS_READY;
return JAVA->is_inited();
}

static void java_input_handler(ngx_http_hi_loc_conf_t *conf, hi::request &req, hi::response &res, jobject request_instance, jobject response_instance)
Expand Down Expand Up @@ -267,40 +275,16 @@ namespace hi

java_input_handler(conf, req, res, request_instance, response_instance);

hi::java_servlet_t jtmp;
if (JAVA_SERVLET_CACHE->exists((const char *)conf->java_servlet.data))
{
jtmp = JAVA_SERVLET_CACHE->get((const char *)conf->java_servlet.data);
time_t now = time(0);
if (difftime(now, jtmp.t) > conf->java_servlet_cache_expires)
{
JAVA_SERVLET_CACHE->erase((const char *)conf->java_servlet.data);
goto update_java_servlet;
}
}
else
{
update_java_servlet:
jtmp.SERVLET = JAVA->env->FindClass((const char *)conf->java_servlet.data);

if (jtmp.SERVLET == NULL)
return;
jtmp.CTOR = JAVA->env->GetMethodID(jtmp.SERVLET, "<init>", "()V");
jtmp.GET_INSTANCE = JAVA->env->GetStaticMethodID(jtmp.SERVLET, "get_instance", "()Lhi/servlet;");
jtmp.HANDLER = JAVA->env->GetMethodID(jtmp.SERVLET, "handler", "(Lhi/request;Lhi/response;)V");
jtmp.t = time(0);
JAVA_SERVLET_CACHE->put((const char *)conf->java_servlet.data, jtmp);
}
jobject servlet_instance;
if (jtmp.GET_INSTANCE == NULL)
if (JAVA->servlet.GET_INSTANCE == NULL)
{
servlet_instance = JAVA->env->NewObject(jtmp.SERVLET, jtmp.CTOR);
servlet_instance = JAVA->env->NewObject(JAVA->servlet.SERVLET, JAVA->servlet.CTOR);
}
else
{
servlet_instance = JAVA->env->CallStaticObjectMethod(jtmp.SERVLET, jtmp.GET_INSTANCE);
servlet_instance = JAVA->env->CallStaticObjectMethod(JAVA->servlet.SERVLET, JAVA->servlet.GET_INSTANCE);
}
JAVA->env->CallVoidMethod(servlet_instance, jtmp.HANDLER, request_instance, response_instance);
JAVA->env->CallVoidMethod(servlet_instance, JAVA->servlet.HANDLER, request_instance, response_instance);
JAVA->env->DeleteLocalRef(servlet_instance);

java_output_handler(conf, req, res, request_instance, response_instance);
Expand Down
27 changes: 23 additions & 4 deletions module/ngx_http_hi_module/lib/java.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,16 @@ namespace hi
{
public:
java(const std::string &classpath, const std::string &jvmoptions, int v)
: 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)
: jvm(0), args(), options(0), ok(false), inited(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), servlet()
{
this->ok = this->create_vm(classpath, jvmoptions);
}
Expand Down Expand Up @@ -111,19 +120,29 @@ namespace hi
return this->ok;
}

void set_inined(bool b)
{
this->inited = b;
}

bool is_inited() const
{
return this->inited;
}

private:
JavaVM *jvm;
JavaVMInitArgs args;
JavaVMOption *options;
bool ok;
bool ok, inited;

public:
JNIEnv *env;
int version;
jclass request, response, hashmap, arraylist, iterator, set;
jmethodID request_ctor, response_ctor, hashmap_put, hashmap_get, hashmap_keyset, arraylist_get, arraylist_size, arraylist_iterator, hasnext, next, set_iterator;
jfieldID status, content, client, user_agent, method, uri, param, req_headers, form, cookies, req_session, req_cache, res_headers, res_session, res_cache;
static bool JAVA_IS_READY;
java_servlet_t servlet;

private:
bool create_vm(const std::string &classpath, const std::string &jvmoptions)
Expand Down Expand Up @@ -183,5 +202,5 @@ namespace hi
this->jvm->DestroyJavaVM();
}
};
bool java::JAVA_IS_READY = false;

} // namespace hi
1 change: 0 additions & 1 deletion module/ngx_http_hi_module/module_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ static std::shared_ptr<hi::lua> LUA;
#ifdef HTTP_HI_JAVA
#include "lib/java.hpp"
static std::shared_ptr<hi::java> JAVA;
static std::shared_ptr<hi::cache::lru_cache<std::string, hi::java_servlet_t>> JAVA_SERVLET_CACHE;
#endif


Expand Down
6 changes: 0 additions & 6 deletions module/ngx_http_hi_module/ngx_http_hi_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,6 @@ static char *ngx_http_hi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *chil
if (conf->java_servlet.len > 0)
{
conf->app_type = application_t::__java__;
if (!JAVA_SERVLET_CACHE)
{
JAVA_SERVLET_CACHE = std::move(std::make_shared<hi::cache::lru_cache<std::string, hi::java_servlet_t>>(conf->java_servlet_cache_size));
}
}
#endif

Expand Down Expand Up @@ -495,8 +491,6 @@ static void ngx_http_hi_process_exit(ngx_cycle_t *cycle)
#endif
#ifdef HTTP_HI_JAVA
JAVA.reset();
JAVA_SERVLET_CACHE.reset();
hi::java::JAVA_IS_READY = false;
#endif
}

Expand Down

0 comments on commit 4c50c46

Please sign in to comment.