Skip to content

Commit

Permalink
refactor(bindings/java): split different classes (#2288)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun authored May 23, 2023
1 parent 01e70a6 commit edb48e8
Show file tree
Hide file tree
Showing 12 changed files with 554 additions and 302 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bindings/java/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ doc = false
[dependencies]
opendal.workspace = true

anyhow = "1.0.71"
jni = "0.21.1"
once_cell = "1.17.1"
tokio = { version = "1.28.1", features = ["full"] }
Expand Down
150 changes: 150 additions & 0 deletions bindings/java/src/blocking_operator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use std::str::FromStr;

use jni::objects::{JClass, JObject, JString};
use jni::sys::{jlong, jstring};
use jni::JNIEnv;

use opendal::{BlockingOperator, Operator, Scheme};

use crate::jmap_to_hashmap;
use crate::Result;

#[no_mangle]
pub extern "system" fn Java_org_apache_opendal_BlockingOperator_constructor(
mut env: JNIEnv,
_: JClass,
scheme: JString,
map: JObject,
) -> jlong {
intern_constructor(&mut env, scheme, map).unwrap_or_else(|e| {
e.throw(&mut env);
0
})
}

fn intern_constructor(env: &mut JNIEnv, scheme: JString, map: JObject) -> Result<jlong> {
let scheme = Scheme::from_str(env.get_string(&scheme)?.to_str()?)?;
let map = jmap_to_hashmap(env, &map)?;
let op = Operator::via_map(scheme, map)?;
Ok(Box::into_raw(Box::new(op.blocking())) as jlong)
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_BlockingOperator_disposeInternal(
_: JNIEnv,
_: JClass,
op: *mut BlockingOperator,
) {
drop(Box::from_raw(op));
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_BlockingOperator_read(
mut env: JNIEnv,
_: JClass,
op: *mut BlockingOperator,
file: JString,
) -> jstring {
intern_read(&mut env, &mut *op, file).unwrap_or_else(|e| {
e.throw(&mut env);
JObject::null().into_raw()
})
}

fn intern_read(env: &mut JNIEnv, op: &mut BlockingOperator, file: JString) -> Result<jstring> {
let file = env.get_string(&file)?;
let content = String::from_utf8(op.read(file.to_str()?)?)?;
Ok(env.new_string(content)?.into_raw())
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_BlockingOperator_write(
mut env: JNIEnv,
_: JClass,
op: *mut BlockingOperator,
file: JString,
content: JString,
) {
intern_write(&mut env, &mut *op, file, content).unwrap_or_else(|e| {
e.throw(&mut env);
})
}

fn intern_write(
env: &mut JNIEnv,
op: &mut BlockingOperator,
file: JString,
content: JString,
) -> Result<()> {
let file = env.get_string(&file)?;
let content = env.get_string(&content)?;
Ok(op.write(file.to_str()?, content.to_str()?.to_string())?)
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_BlockingOperator_stat(
mut env: JNIEnv,
_: JClass,
op: *mut BlockingOperator,
file: JString,
) -> jlong {
intern_stat(&mut env, &mut *op, file).unwrap_or_else(|e| {
e.throw(&mut env);
0
})
}

fn intern_stat(env: &mut JNIEnv, op: &mut BlockingOperator, file: JString) -> Result<jlong> {
let file = env.get_string(&file)?;
let metadata = op.stat(file.to_str()?)?;
Ok(Box::into_raw(Box::new(metadata)) as jlong)
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_BlockingOperator_delete(
mut env: JNIEnv,
_: JClass,
op: *mut BlockingOperator,
file: JString,
) {
intern_delete(&mut env, &mut *op, file).unwrap_or_else(|e| {
e.throw(&mut env);
})
}

fn intern_delete(env: &mut JNIEnv, op: &mut BlockingOperator, file: JString) -> Result<()> {
let file = env.get_string(&file)?;
Ok(op.delete(file.to_str()?)?)
}
116 changes: 116 additions & 0 deletions bindings/java/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use jni::objects::{JThrowable, JValue};
use jni::JNIEnv;
use opendal::ErrorKind;
use std::fmt::{Debug, Display, Formatter};

pub(crate) struct Error {
inner: opendal::Error,
}

impl Error {
pub(crate) fn throw(&self, env: &mut JNIEnv) {
if let Err(err) = self.do_throw(env) {
env.fatal_error(err.to_string());
}
}

pub(crate) fn to_exception<'local>(
&self,
env: &mut JNIEnv<'local>,
) -> jni::errors::Result<JThrowable<'local>> {
let class = env.find_class("org/apache/opendal/exception/ODException")?;
let code = env.new_string(match self.inner.kind() {
ErrorKind::Unexpected => "Unexpected",
ErrorKind::Unsupported => "Unsupported",
ErrorKind::ConfigInvalid => "ConfigInvalid",
ErrorKind::NotFound => "NotFound",
ErrorKind::PermissionDenied => "PermissionDenied",
ErrorKind::IsADirectory => "IsADirectory",
ErrorKind::NotADirectory => "NotADirectory",
ErrorKind::AlreadyExists => "AlreadyExists",
ErrorKind::RateLimited => "RateLimited",
ErrorKind::IsSameFile => "IsSameFile",
ErrorKind::ConditionNotMatch => "ConditionNotMatch",
ErrorKind::ContentTruncated => "ContentTruncated",
ErrorKind::ContentIncomplete => "ContentIncomplete",
_ => "Unexpected",
})?;
let message = env.new_string(self.inner.to_string())?;
let exception = env.new_object(
class,
"(Ljava/lang/String;Ljava/lang/String;)V",
&[JValue::Object(&code), JValue::Object(&message)],
)?;
Ok(JThrowable::from(exception))
}

fn do_throw(&self, env: &mut JNIEnv) -> jni::errors::Result<()> {
let exception = self.to_exception(env)?;
env.throw(exception)
}
}

impl From<opendal::Error> for Error {
fn from(error: opendal::Error) -> Self {
Self { inner: error }
}
}

impl From<jni::errors::Error> for Error {
fn from(error: jni::errors::Error) -> Self {
Self {
inner: opendal::Error::new(ErrorKind::Unexpected, &error.to_string()).set_source(error),
}
}
}

impl From<std::str::Utf8Error> for Error {
fn from(error: std::str::Utf8Error) -> Self {
Self {
inner: opendal::Error::new(ErrorKind::Unexpected, &error.to_string()).set_source(error),
}
}
}

impl From<std::string::FromUtf8Error> for Error {
fn from(error: std::string::FromUtf8Error) -> Self {
Self {
inner: opendal::Error::new(ErrorKind::Unexpected, &error.to_string()).set_source(error),
}
}
}

impl Debug for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.inner, f)
}
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.inner, f)
}
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.inner.source()
}
}
Loading

0 comments on commit edb48e8

Please sign in to comment.