feat(java): use config struct for connect

This commit is contained in:
zaaarf 2024-09-11 15:47:56 +02:00
parent 02f8f0c0a9
commit 6559fcd5f3
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
3 changed files with 59 additions and 18 deletions

View file

@ -10,7 +10,9 @@ import java.util.Optional;
public class Client {
private final long ptr;
public static native Client connect(String url, String username, String password) throws ConnectionException;
public static native Client connect(String username, String password) throws ConnectionException;
public static native Client connectToServer(String username, String password, String host, int port, boolean tls) throws ConnectionException;
Client(long ptr) {
this.ptr = ptr;
}

View file

@ -1,27 +1,69 @@
use jni::{objects::{JClass, JObject, JString, JValueGen}, sys::{jboolean, jlong, jobject, jobjectArray}, JNIEnv};
use crate::{client::Client, Workspace};
use jni::{objects::{JClass, JObject, JString, JValueGen}, sys::{jboolean, jint, jlong, jobject, jobjectArray}, JNIEnv};
use crate::{api::Config, client::Client, Workspace};
use super::{JExceptable, RT};
/// Connect to a given URL and return a [Client] to interact with that server.
/// Connect using the given credentials to the default server, and return a [Client] to interact with it.
#[no_mangle]
pub extern "system" fn Java_mp_code_Client_connect<'local>(
mut env: JNIEnv,
_class: JClass<'local>,
url: JString<'local>,
user: JString<'local>,
pwd: JString<'local>
) -> jobject {
let url: String = env.get_string(&url)
let username: String = env.get_string(&user)
.map(|s| s.into())
.jexcept(&mut env);
let user: String = env.get_string(&user)
let password: String = env.get_string(&pwd)
.map(|s| s.into())
.jexcept(&mut env);
let pwd: String = env.get_string(&pwd)
connect_internal(env, Config {
username,
password,
host: None,
port: None,
tls: None
})
}
/// Connect to a given URL and return a [Client] to interact with that server.
#[no_mangle]
#[allow(non_snake_case)]
pub extern "system" fn Java_mp_code_Client_connectToServer<'local>(
mut env: JNIEnv,
_class: JClass<'local>,
user: JString<'local>,
pwd: JString<'local>,
host: JString<'local>,
port: jint,
tls: jboolean
) -> jobject {
let username: String = env.get_string(&user)
.map(|s| s.into())
.jexcept(&mut env);
RT.block_on(crate::Client::connect(&url, &user, &pwd))
let password: String = env.get_string(&pwd)
.map(|s| s.into())
.jexcept(&mut env);
let host: String = env.get_string(&host)
.map(|s| s.into())
.jexcept(&mut env);
if port < 0 {
env.throw_new("mp/code/exceptions/JNIException", "Negative port number!")
.jexcept(&mut env);
}
connect_internal(env, Config {
username,
password,
host: Some(host),
port: Some(port as u16),
tls: Some(tls != 0),
})
}
fn connect_internal(mut env: JNIEnv, config: Config) -> jobject {
RT.block_on(Client::connect(config))
.map(|client| Box::into_raw(Box::new(client)) as jlong)
.map(|ptr| {
env.find_class("mp/code/Client")
@ -123,13 +165,12 @@ pub extern "system" fn Java_mp_code_Client_list_1workspaces<'local>(
env.find_class("java/lang/String")
.and_then(|class| env.new_object_array(list.len() as i32, class, JObject::null()))
.map(|arr| {
.inspect(|arr| {
for (idx, path) in list.iter().enumerate() {
env.new_string(path)
.and_then(|path| env.set_object_array_element(&arr, idx as i32, path))
.and_then(|path| env.set_object_array_element(arr, idx as i32, path))
.jexcept(&mut env)
}
arr
}).jexcept(&mut env).as_raw()
}

View file

@ -85,13 +85,12 @@ pub extern "system" fn Java_mp_code_Workspace_get_1file_1tree(
let file_tree = workspace.filetree(filter.as_deref());
env.find_class("java/lang/String")
.and_then(|class| env.new_object_array(file_tree.len() as i32, class, JObject::null()))
.map(|arr| {
.inspect(|arr| {
for (idx, path) in file_tree.iter().enumerate() {
env.new_string(path)
.and_then(|path| env.set_object_array_element(&arr, idx as i32, path))
.and_then(|path| env.set_object_array_element(arr, idx as i32, path))
.jexcept(&mut env)
}
arr
}).jexcept(&mut env).as_raw()
}
@ -180,13 +179,12 @@ pub extern "system" fn Java_mp_code_Workspace_list_1buffer_1users<'local>(
env.find_class("java/util/UUID")
.and_then(|class| env.new_object_array(users.len() as i32, &class, JObject::null()))
.map(|arr| {
.inspect(|arr| {
for (idx, user) in users.iter().enumerate() {
user.id.jobjectify(&mut env)
.and_then(|id| env.set_object_array_element(&arr, idx as i32, id))
.and_then(|id| env.set_object_array_element(arr, idx as i32, id))
.jexcept(&mut env);
}
arr
}).jexcept(&mut env).as_raw()
}