Skip to content

Commit 5c4e988

Browse files
committed
use JNI_GetCreatedJavaVMs
1 parent 5806499 commit 5c4e988

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-1
lines changed

src/HttpsCaller.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.net.*;
2+
import java.io.*;
3+
import javax.net.ssl.HttpsURLConnection;
4+
5+
public class HttpsCaller {
6+
public static String callHttps() {
7+
try {
8+
URL url = new URL("https://api.github.com/");
9+
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
10+
conn.setRequestMethod("GET");
11+
12+
BufferedReader in = new BufferedReader(
13+
new InputStreamReader(conn.getInputStream()));
14+
String inputLine;
15+
StringBuilder content = new StringBuilder();
16+
17+
while ((inputLine = in.readLine()) != null) {
18+
content.append(inputLine);
19+
}
20+
in.close();
21+
conn.disconnect();
22+
return content.toString();
23+
} catch (Exception e) {
24+
return "Error: " + e.getMessage();
25+
}
26+
}
27+
}

src/call_java.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <jni.h>
2+
#include <sqlite3ext.h>
3+
#include <string.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <stdbool.h>
7+
8+
JavaVM* gJvm = NULL;
9+
10+
void findJvm() {
11+
if (gJvm != NULL) {
12+
return;
13+
}
14+
15+
JavaVM* vmBuf[1];
16+
jsize nVMs = 0;
17+
18+
jint res = JNI_GetCreatedJavaVMs(vmBuf, 1, &nVMs);
19+
if (res == JNI_OK && nVMs > 0) {
20+
gJvm = vmBuf[0];
21+
} else {
22+
gJvm = NULL;
23+
}
24+
}
25+
26+
JNIEnv* getEnv() {
27+
findJvm();
28+
if (gJvm == NULL) {
29+
printf("Could not find JavaVM\n");
30+
return NULL;
31+
}
32+
33+
JNIEnv* env;
34+
if ((*gJvm)->GetEnv(gJvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
35+
if ((*gJvm)->AttachCurrentThread(gJvm, (void**)&env, NULL) != 0) {
36+
printf("Failed to attach current thread\n");
37+
return NULL;
38+
}
39+
}
40+
return env;
41+
}
42+
43+
const char* call_https_java(JNIEnv* env) {
44+
jclass cls = (*env)->FindClass(env, "HttpsCaller");
45+
if (cls == NULL) {
46+
printf("Failed to find class HttpsCaller\n");
47+
return NULL;
48+
}
49+
50+
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "callHttps", "()Ljava/lang/String;");
51+
if (mid == NULL) {
52+
printf("Failed to find static method callHttps\n");
53+
(*env)->DeleteLocalRef(env, cls);
54+
return NULL;
55+
}
56+
57+
jstring result = (jstring)(*env)->CallStaticObjectMethod(env, cls, mid);
58+
if (result == NULL) {
59+
printf("callHttps returned null\n");
60+
(*env)->DeleteLocalRef(env, cls);
61+
return NULL;
62+
}
63+
64+
const char* str = (*env)->GetStringUTFChars(env, result, NULL);
65+
if (str == NULL) {
66+
printf("Failed to get UTF chars from jstring\n");
67+
(*env)->DeleteLocalRef(env, result);
68+
(*env)->DeleteLocalRef(env, cls);
69+
return NULL;
70+
}
71+
72+
char* copied = strdup(str);
73+
74+
(*env)->ReleaseStringUTFChars(env, result, str);
75+
(*env)->DeleteLocalRef(env, result);
76+
(*env)->DeleteLocalRef(env, cls);
77+
78+
return copied;
79+
}

src/call_java.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// call_java.h
2+
#ifndef CALL_JAVA_H
3+
#define CALL_JAVA_H
4+
5+
#include <jni.h>
6+
7+
JNIEnv* getEnv();
8+
const char* call_https_java(JNIEnv* env);
9+
10+
#endif

test/unit.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "dbutils.h"
2424
#include "cloudsync.h"
2525
#include "cloudsync_private.h"
26+
#include <jni.h>
27+
#include "call_java.h"
2628

2729
// declared only if macro CLOUDSYNC_UNITTEST is defined
2830
extern char *OUT_OF_MEMORY_BUFFER;
@@ -3273,6 +3275,24 @@ bool do_test_alter(int nclients, int alter_version, bool print_result, bool clea
32733275
return result;
32743276
}
32753277

3278+
bool test_call_https_java(void) {
3279+
JNIEnv* env = getEnv();
3280+
if (!env) {
3281+
printf("Could not get JNIEnv\n");
3282+
return false;
3283+
}
3284+
3285+
const char* javaResult = call_https_java(env);
3286+
if (javaResult) {
3287+
printf("Got from Java: %s\n", javaResult);
3288+
free((void*)javaResult);
3289+
return true;
3290+
} else {
3291+
printf("call_https_java returned NULL\n");
3292+
return false;
3293+
}
3294+
}
3295+
32763296
// MARK: -
32773297

32783298
int test_report(const char *description, bool result){
@@ -3341,7 +3361,8 @@ int main(int argc, const char * argv[]) {
33413361
result += test_report("Test Alter Table 1:", do_test_alter(3, 1, print_result, cleanup_databases));
33423362
result += test_report("Test Alter Table 2:", do_test_alter(3, 2, print_result, cleanup_databases));
33433363
result += test_report("Test Alter Table 3:", do_test_alter(3, 3, print_result, cleanup_databases));
3344-
3364+
result += test_report("JNI call_https_java Test:", test_call_https_java());
3365+
33453366
finalize:
33463367
printf("\n");
33473368
if (rc != SQLITE_OK) printf("%s (%d)\n", (db) ? sqlite3_errmsg(db) : "N/A", rc);

0 commit comments

Comments
 (0)