1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <jni.h>
7
8 #include <aws/io/channel_bootstrap.h>
9
10 #include "crt.h"
11 #include "java_class_ids.h"
12
13 #if _MSC_VER
14 # pragma warning(disable : 4204) /* non-constant aggregate initializer */
15 #endif
16
17 /* on 32-bit platforms, casting pointers to longs throws a warning we don't need */
18 #if UINTPTR_MAX == 0xffffffff
19 # if defined(_MSC_VER)
20 # pragma warning(push)
21 # pragma warning(disable : 4305) /* 'type cast': truncation from 'jlong' to 'jni_tls_ctx_options *' */
22 # else
23 # pragma GCC diagnostic push
24 # pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
25 # pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
26 # endif
27 #endif
28
29 struct shutdown_callback_data {
30 JavaVM *jvm;
31 jweak java_client_bootstrap;
32 };
33
s_shutdown_callback_data_destroy(JNIEnv * env,struct shutdown_callback_data * callback_data)34 static void s_shutdown_callback_data_destroy(JNIEnv *env, struct shutdown_callback_data *callback_data) {
35 if (!callback_data) {
36 return;
37 }
38
39 if (callback_data->java_client_bootstrap) {
40 (*env)->DeleteWeakGlobalRef(env, callback_data->java_client_bootstrap);
41 }
42
43 aws_mem_release(aws_jni_get_allocator(), callback_data);
44 }
45
s_client_bootstrap_shutdown_complete(void * user_data)46 static void s_client_bootstrap_shutdown_complete(void *user_data) {
47 struct shutdown_callback_data *callback_data = user_data;
48
49 /********** JNI ENV ACQUIRE **********/
50 JNIEnv *env = aws_jni_acquire_thread_env(callback_data->jvm);
51 if (env == NULL) {
52 /* If we can't get an environment, then the JVM is probably shutting down. Don't crash. */
53 return;
54 }
55
56 jobject java_client_bootstrap = (*env)->NewLocalRef(env, callback_data->java_client_bootstrap);
57 if (java_client_bootstrap) {
58 // Tell the Java ClientBootstrap that cleanup is done. This lets it release its references.
59 (*env)->CallVoidMethod(env, java_client_bootstrap, client_bootstrap_properties.onShutdownComplete);
60 (*env)->DeleteLocalRef(env, java_client_bootstrap);
61 AWS_FATAL_ASSERT(!aws_jni_check_and_clear_exception(env));
62 }
63
64 JavaVM *jvm = callback_data->jvm;
65 s_shutdown_callback_data_destroy(env, callback_data);
66 aws_jni_release_thread_env(jvm, env);
67 /********** JNI ENV RELEASE **********/
68 }
69
70 JNIEXPORT
Java_software_amazon_awssdk_crt_io_ClientBootstrap_clientBootstrapNew(JNIEnv * env,jclass jni_class,jobject jni_bootstrap,jlong jni_elg,jlong jni_hr)71 jlong JNICALL Java_software_amazon_awssdk_crt_io_ClientBootstrap_clientBootstrapNew(
72 JNIEnv *env,
73 jclass jni_class,
74 jobject jni_bootstrap,
75 jlong jni_elg,
76 jlong jni_hr) {
77 (void)jni_class;
78 aws_cache_jni_ids(env);
79
80 struct aws_event_loop_group *elg = (struct aws_event_loop_group *)jni_elg;
81 struct aws_host_resolver *resolver = (struct aws_host_resolver *)jni_hr;
82
83 if (!elg) {
84 aws_jni_throw_runtime_exception(env, "ClientBootstrap.client_bootstrap_new: Invalid EventLoopGroup");
85 return (jlong)NULL;
86 }
87
88 if (!resolver) {
89 aws_jni_throw_runtime_exception(env, "ClientBootstrap.client_bootstrap_new: Invalid HostResolver");
90 return (jlong)NULL;
91 }
92
93 struct aws_allocator *allocator = aws_jni_get_allocator();
94
95 struct shutdown_callback_data *callback_data = aws_mem_calloc(allocator, 1, sizeof(struct shutdown_callback_data));
96 if (!callback_data) {
97 aws_jni_throw_runtime_exception(env, "ClientBootstrap.client_bootstrap_new: Unable to allocate");
98 return (jlong)NULL;
99 }
100
101 jint jvmresult = (*env)->GetJavaVM(env, &callback_data->jvm);
102 if (jvmresult != 0) {
103 aws_jni_throw_runtime_exception(env, "ClientBootstrap.client_bootstrap_new: Unable to get JVM");
104 goto error;
105 }
106
107 callback_data->java_client_bootstrap = (*env)->NewWeakGlobalRef(env, jni_bootstrap);
108 if (!callback_data->java_client_bootstrap) {
109 aws_jni_throw_runtime_exception(env, "ClientBootstrap.client_bootstrap_new: Unable to create global weak ref");
110 goto error;
111 }
112
113 struct aws_client_bootstrap_options bootstrap_options = {
114 .event_loop_group = elg,
115 .host_resolver = resolver,
116 .on_shutdown_complete = s_client_bootstrap_shutdown_complete,
117 .user_data = callback_data,
118 };
119
120 struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
121 if (!bootstrap) {
122 aws_jni_throw_runtime_exception(
123 env, "ClientBootstrap.client_bootstrap_new: Unable to allocate new aws_client_bootstrap");
124 goto error;
125 }
126
127 return (jlong)bootstrap;
128
129 error:
130 s_shutdown_callback_data_destroy(env, callback_data);
131 return (jlong)NULL;
132 }
133
134 JNIEXPORT
Java_software_amazon_awssdk_crt_io_ClientBootstrap_clientBootstrapDestroy(JNIEnv * env,jclass jni_class,jlong jni_bootstrap)135 void JNICALL Java_software_amazon_awssdk_crt_io_ClientBootstrap_clientBootstrapDestroy(
136 JNIEnv *env,
137 jclass jni_class,
138 jlong jni_bootstrap) {
139 (void)env;
140 (void)jni_class;
141 aws_cache_jni_ids(env);
142
143 struct aws_client_bootstrap *bootstrap = (struct aws_client_bootstrap *)jni_bootstrap;
144 if (!bootstrap) {
145 return;
146 }
147
148 aws_client_bootstrap_release(bootstrap);
149 }
150
151 #if UINTPTR_MAX == 0xffffffff
152 # if defined(_MSC_VER)
153 # pragma warning(pop)
154 # else
155 # pragma GCC diagnostic pop
156 # endif
157 #endif
158