Skip to content

Commit 43638af

Browse files
committed
new api migration
1 parent f3e855f commit 43638af

11 files changed

Lines changed: 142 additions & 153 deletions

File tree

zenoh-flat-jni/src/zbytes.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ use zenoh_ext::{VarInt, ZDeserializeError, ZDeserializer, ZSerializer};
2323

2424
type JResult<T> = core::result::Result<T, JniBindingError<()>>;
2525

26-
/// Throw `io.zenoh.jni.ZException` with the error's message. This hand-written
27-
/// JNI surface (ZSerializer/ZDeserializer) throws directly — the generated
28-
/// code instead routes errors through the per-call `ErrorSink` callback.
26+
/// Throw the SDK's `io.zenoh.exceptions.ZError` with the error's message. This
27+
/// hand-written JNI surface (ZSerializer/ZDeserializer) throws directly —
28+
/// matching the canonical model where every error surfaces as `ZError` (the
29+
/// generated wrappers reach it via their `onError` callback; this hand-written
30+
/// path throws it straight, so callers no longer need an exception-bridge).
31+
/// `ZError(message: String?)` compiles to a `(Ljava/lang/String;)V` ctor.
2932
fn throw_jni_error(env: &mut JNIEnv, err: &impl core::fmt::Display) {
30-
let _ = env.throw_new("io/zenoh/jni/ZException", err.to_string());
33+
let _ = env.throw_new("io/zenoh/exceptions/ZError", err.to_string());
3134
}
3235

3336
/// Helper function to convert a JByteArray into a Vec<u8>.

zenoh-java/src/commonMain/kotlin/io/zenoh/Config.kt

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ package io.zenoh
1616

1717
import io.zenoh.ZenohLoad
1818
import io.zenoh.exceptions.ZError
19-
import io.zenoh.exceptions.wrapJNIExceptionAsZError
19+
import io.zenoh.exceptions.throwZError
20+
import io.zenoh.exceptions.throwZError0
2021
import io.zenoh.jni.config.ZConfig
2122
import java.io.File
2223
import java.nio.file.Path
@@ -51,9 +52,7 @@ class Config internal constructor(internal val zConfig: ZConfig) {
5152
* Returns the default config.
5253
*/
5354
@JvmStatic
54-
fun loadDefault(): Config = wrapJNIExceptionAsZError {
55-
Config(io.zenoh.jni.config.zConfigDefault())
56-
}
55+
fun loadDefault(): Config = Config(io.zenoh.jni.config.zConfigDefault(throwZError0))
5756

5857
/**
5958
* Loads the configuration from the [File] specified.
@@ -75,9 +74,7 @@ class Config internal constructor(internal val zConfig: ZConfig) {
7574
*/
7675
@JvmStatic
7776
@Throws(ZError::class)
78-
fun fromFile(path: Path): Config = wrapJNIExceptionAsZError {
79-
Config(io.zenoh.jni.config.zConfigFromFile(path.toString()))
80-
}
77+
fun fromFile(path: Path): Config = Config(io.zenoh.jni.config.zConfigFromFile(path.toString(), throwZError))
8178

8279
/**
8380
* Loads the configuration from json-formatted string.
@@ -90,9 +87,7 @@ class Config internal constructor(internal val zConfig: ZConfig) {
9087
*/
9188
@JvmStatic
9289
@Throws(ZError::class)
93-
fun fromJson(config: String): Config = wrapJNIExceptionAsZError {
94-
Config(io.zenoh.jni.config.zConfigFromJson(config))
95-
}
90+
fun fromJson(config: String): Config = Config(io.zenoh.jni.config.zConfigFromJson(config, throwZError))
9691

9792
/**
9893
* Loads the configuration from json5-formatted string.
@@ -105,9 +100,7 @@ class Config internal constructor(internal val zConfig: ZConfig) {
105100
*/
106101
@JvmStatic
107102
@Throws(ZError::class)
108-
fun fromJson5(config: String): Config = wrapJNIExceptionAsZError {
109-
Config(io.zenoh.jni.config.zConfigFromJson5(config))
110-
}
103+
fun fromJson5(config: String): Config = Config(io.zenoh.jni.config.zConfigFromJson5(config, throwZError))
111104

112105
/**
113106
* Loads the configuration from yaml-formatted string.
@@ -120,9 +113,7 @@ class Config internal constructor(internal val zConfig: ZConfig) {
120113
*/
121114
@JvmStatic
122115
@Throws(ZError::class)
123-
fun fromYaml(config: String): Config = wrapJNIExceptionAsZError {
124-
Config(io.zenoh.jni.config.zConfigFromYaml(config))
125-
}
116+
fun fromYaml(config: String): Config = Config(io.zenoh.jni.config.zConfigFromYaml(config, throwZError))
126117

127118
/**
128119
* Loads the configuration from the env variable [CONFIG_ENV].
@@ -145,15 +136,12 @@ class Config internal constructor(internal val zConfig: ZConfig) {
145136
* The json value associated to the [key].
146137
*/
147138
@Throws(ZError::class)
148-
fun getJson(key: String): String = wrapJNIExceptionAsZError {
149-
io.zenoh.jni.config.zConfigGetJson(zConfig, key)
150-
}
139+
fun getJson(key: String): String = io.zenoh.jni.config.zConfigGetJson(zConfig, key, throwZError0)
151140

152141
/**
153142
* Inserts a json5 value associated to the [key] into the Config.
154143
*/
155144
@Throws(ZError::class)
156-
fun insertJson5(key: String, value: String) = wrapJNIExceptionAsZError {
157-
io.zenoh.jni.config.zConfigInsertJson5(zConfig, key, value)
158-
}
145+
fun insertJson5(key: String, value: String) =
146+
io.zenoh.jni.config.zConfigInsertJson5(zConfig, key, value, throwZError)
159147
}

zenoh-java/src/commonMain/kotlin/io/zenoh/Logger.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package io.zenoh
1616

1717
import io.zenoh.exceptions.ZError
18-
import io.zenoh.exceptions.wrapJNIExceptionAsZError
18+
import io.zenoh.exceptions.throwZError0
1919
import io.zenoh.jni.logger.initAndroidLogs
2020

2121
/** Logger class to redirect the Rust logs from Zenoh to the kotlin environment. */
@@ -33,7 +33,7 @@ internal class Logger {
3333
*/
3434
@Throws(ZError::class)
3535
fun start(filter: String) {
36-
wrapJNIExceptionAsZError { initAndroidLogs(filter) }
36+
initAndroidLogs(filter, throwZError0)
3737
}
3838
}
3939
}

zenoh-java/src/commonMain/kotlin/io/zenoh/Session.kt

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import io.zenoh.bytes.ZBytes
2121
import io.zenoh.bytes.into
2222
import io.zenoh.config.ZenohId
2323
import io.zenoh.exceptions.ZError
24-
import io.zenoh.exceptions.wrapJNIExceptionAsZError
24+
import io.zenoh.exceptions.throwZError
25+
import io.zenoh.exceptions.throwZError0
2526
import io.zenoh.handlers.BlockingQueueHandler
2627
import io.zenoh.handlers.Callback
2728
import io.zenoh.handlers.Handler
@@ -387,8 +388,8 @@ class Session private constructor(private val config: Config) : AutoCloseable {
387388
@Throws(ZError::class)
388389
fun declareKeyExpr(keyExpr: String): KeyExpr {
389390
val zSession = zSession ?: throw sessionClosedException
390-
val keyexpr = wrapJNIExceptionAsZError {
391-
KeyExpr(io.zenoh.jni.session.zSessionDeclareKeyexpr(zSession, keyExpr))
391+
val keyexpr = run {
392+
KeyExpr(io.zenoh.jni.session.zSessionDeclareKeyexpr(zSession, keyExpr, throwZError))
392393
}
393394
strongDeclarations.add(keyexpr)
394395
return keyexpr
@@ -410,8 +411,8 @@ class Session private constructor(private val config: Config) : AutoCloseable {
410411
if (handle.isClosed()) {
411412
throw ZError("Attempting to undeclare a non declared key expression.")
412413
}
413-
wrapJNIExceptionAsZError {
414-
io.zenoh.jni.session.zSessionUndeclareKeyexpr(zSession, handle)
414+
run {
415+
io.zenoh.jni.session.zSessionUndeclareKeyexpr(zSession, handle, throwZError)
415416
}
416417
}
417418

@@ -586,14 +587,15 @@ class Session private constructor(private val config: Config) : AutoCloseable {
586587
@Throws(ZError::class)
587588
internal fun resolvePublisher(keyExpr: KeyExpr, options: PublisherOptions): Publisher {
588589
val zSession = zSession ?: throw sessionClosedException
589-
val publisher = wrapJNIExceptionAsZError {
590+
val publisher = run {
590591
val zPublisher = io.zenoh.jni.session.zSessionDeclarePublisher(
591592
zSession,
592593
keyExpr.exprSel, keyExpr.exprStr, keyExpr.exprHandleOwned(),
593594
options.congestionControl.jni,
594595
options.priority.jni,
595596
options.express,
596-
options.reliability.jni
597+
options.reliability.jni,
598+
throwZError
597599
)
598600
Publisher(
599601
keyExpr,
@@ -612,12 +614,13 @@ class Session private constructor(private val config: Config) : AutoCloseable {
612614
keyExpr: KeyExpr, handler: Handler<Sample, R>
613615
): HandlerSubscriber<R> {
614616
val zSession = zSession ?: throw sessionClosedException
615-
val subscriber = wrapJNIExceptionAsZError {
617+
val subscriber = run {
616618
val zSubscriber = io.zenoh.jni.session.zSessionDeclareSubscriber(
617619
zSession,
618620
keyExpr.exprSel, keyExpr.exprStr, keyExpr.exprHandleOwned(),
619621
sampleCallbackOf { handler.handle(it) },
620-
io.zenoh.jni.callbacks.Callback { handler.onClose() }
622+
io.zenoh.jni.callbacks.Callback { handler.onClose() },
623+
throwZError
621624
)
622625
HandlerSubscriber(keyExpr, zSubscriber, handler.receiver())
623626
}
@@ -630,12 +633,13 @@ class Session private constructor(private val config: Config) : AutoCloseable {
630633
keyExpr: KeyExpr, callback: Callback<Sample>
631634
): CallbackSubscriber {
632635
val zSession = zSession ?: throw sessionClosedException
633-
val subscriber = wrapJNIExceptionAsZError {
636+
val subscriber = run {
634637
val zSubscriber = io.zenoh.jni.session.zSessionDeclareSubscriber(
635638
zSession,
636639
keyExpr.exprSel, keyExpr.exprStr, keyExpr.exprHandleOwned(),
637640
sampleCallbackOf { callback.run(it) },
638-
io.zenoh.jni.callbacks.Callback { }
641+
io.zenoh.jni.callbacks.Callback { },
642+
throwZError
639643
)
640644
CallbackSubscriber(keyExpr, zSubscriber)
641645
}
@@ -648,13 +652,14 @@ class Session private constructor(private val config: Config) : AutoCloseable {
648652
keyExpr: KeyExpr, handler: Handler<Query, R>, options: QueryableOptions
649653
): HandlerQueryable<R> {
650654
val zSession = zSession ?: throw sessionClosedException
651-
val queryable = wrapJNIExceptionAsZError {
655+
val queryable = run {
652656
val zQueryable = io.zenoh.jni.session.zSessionDeclareQueryable(
653657
zSession,
654658
keyExpr.exprSel, keyExpr.exprStr, keyExpr.exprHandleOwned(),
655659
options.complete,
656660
queryCallbackOf { handler.handle(it) },
657-
io.zenoh.jni.callbacks.Callback { handler.onClose() }
661+
io.zenoh.jni.callbacks.Callback { handler.onClose() },
662+
throwZError
658663
)
659664
HandlerQueryable(keyExpr, zQueryable, handler.receiver())
660665
}
@@ -667,13 +672,14 @@ class Session private constructor(private val config: Config) : AutoCloseable {
667672
keyExpr: KeyExpr, callback: Callback<Query>, options: QueryableOptions
668673
): CallbackQueryable {
669674
val zSession = zSession ?: throw sessionClosedException
670-
val queryable = wrapJNIExceptionAsZError {
675+
val queryable = run {
671676
val zQueryable = io.zenoh.jni.session.zSessionDeclareQueryable(
672677
zSession,
673678
keyExpr.exprSel, keyExpr.exprStr, keyExpr.exprHandleOwned(),
674679
options.complete,
675680
queryCallbackOf { callback.run(it) },
676-
io.zenoh.jni.callbacks.Callback { }
681+
io.zenoh.jni.callbacks.Callback { },
682+
throwZError
677683
)
678684
CallbackQueryable(keyExpr, zQueryable)
679685
}
@@ -687,7 +693,7 @@ class Session private constructor(private val config: Config) : AutoCloseable {
687693
options: QuerierOptions
688694
): Querier {
689695
val zSession = zSession ?: throw sessionClosedException
690-
val querier = wrapJNIExceptionAsZError {
696+
val querier = run {
691697
val zQuerier = io.zenoh.jni.session.zSessionDeclareQuerier(
692698
zSession,
693699
keyExpr.exprSel, keyExpr.exprStr, keyExpr.exprHandleOwned(),
@@ -697,7 +703,8 @@ class Session private constructor(private val config: Config) : AutoCloseable {
697703
options.priority.jni,
698704
options.express,
699705
options.timeout.toMillis(),
700-
options.acceptReplies.toFlat()
706+
options.acceptReplies.toFlat(),
707+
throwZError
701708
)
702709
Querier(
703710
keyExpr,
@@ -720,7 +727,7 @@ class Session private constructor(private val config: Config) : AutoCloseable {
720727
options: GetOptions
721728
): R {
722729
val zSession = zSession ?: throw sessionClosedException
723-
return wrapJNIExceptionAsZError {
730+
return run {
724731
val sel = selector.into()
725732
io.zenoh.jni.session.zSessionGet(
726733
zSession,
@@ -739,7 +746,8 @@ class Session private constructor(private val config: Config) : AutoCloseable {
739746
(options.encoding ?: Encoding.defaultEncoding()).repr,
740747
options.attachment?.into()?.bytes,
741748
replyCallbackOf { handler.handle(it) },
742-
io.zenoh.jni.callbacks.Callback { handler.onClose() }
749+
io.zenoh.jni.callbacks.Callback { handler.onClose() },
750+
throwZError
743751
)
744752
handler.receiver()
745753
}
@@ -752,7 +760,7 @@ class Session private constructor(private val config: Config) : AutoCloseable {
752760
options: GetOptions
753761
) {
754762
val zSession = zSession ?: throw sessionClosedException
755-
wrapJNIExceptionAsZError {
763+
run {
756764
val sel = selector.into()
757765
io.zenoh.jni.session.zSessionGet(
758766
zSession,
@@ -771,15 +779,16 @@ class Session private constructor(private val config: Config) : AutoCloseable {
771779
(options.encoding ?: Encoding.defaultEncoding()).repr,
772780
options.attachment?.into()?.bytes,
773781
replyCallbackOf { callback.run(it) },
774-
io.zenoh.jni.callbacks.Callback { }
782+
io.zenoh.jni.callbacks.Callback { },
783+
throwZError
775784
)
776785
}
777786
}
778787

779788
@Throws(ZError::class)
780789
internal fun resolvePut(keyExpr: KeyExpr, payload: IntoZBytes, putOptions: PutOptions) {
781790
val zSession = zSession ?: return
782-
wrapJNIExceptionAsZError {
791+
run {
783792
io.zenoh.jni.session.zSessionPut(
784793
zSession,
785794
keyExpr.exprSel,
@@ -791,15 +800,16 @@ class Session private constructor(private val config: Config) : AutoCloseable {
791800
putOptions.priority.jni,
792801
putOptions.express,
793802
putOptions.attachment?.into()?.bytes,
794-
putOptions.reliability.jni
803+
putOptions.reliability.jni,
804+
throwZError
795805
)
796806
}
797807
}
798808

799809
@Throws(ZError::class)
800810
internal fun resolveDelete(keyExpr: KeyExpr, deleteOptions: DeleteOptions) {
801811
val zSession = zSession ?: return
802-
wrapJNIExceptionAsZError {
812+
run {
803813
io.zenoh.jni.session.zSessionDelete(
804814
zSession,
805815
keyExpr.exprSel,
@@ -809,38 +819,30 @@ class Session private constructor(private val config: Config) : AutoCloseable {
809819
deleteOptions.priority.jni,
810820
deleteOptions.express,
811821
deleteOptions.attachment?.into()?.bytes,
812-
deleteOptions.reliability.jni
822+
deleteOptions.reliability.jni,
823+
throwZError
813824
)
814825
}
815826
}
816827

817828
@Throws(ZError::class)
818829
internal fun zid(): ZenohId {
819830
val zSession = zSession ?: throw sessionClosedException
820-
return wrapJNIExceptionAsZError { ZenohId(io.zenoh.jni.session.zSessionZid(zSession)) }
831+
return ZenohId(io.zenoh.jni.session.zSessionZid(zSession, throwZError0))
821832
}
822833

823834
@Throws(ZError::class)
824835
internal fun getPeersId(): List<ZenohId> {
825836
val zSession = zSession ?: throw sessionClosedException
826-
// Output expansion (M4): the native side folds each ZZenohId into this
827-
// accumulator in one pass — the caller owns the list (no intermediate
828-
// List<ZZenohId>). `zid` is the typed ZZenohId value class.
829-
return wrapJNIExceptionAsZError {
830-
io.zenoh.jni.session.zSessionPeersZid(zSession, ArrayList<ZenohId>()) { list, zid ->
831-
list.add(ZenohId(zid)); list
832-
}
833-
}
837+
// Canonical model: `ZZenohId` is a value_blob (no canonical output), so
838+
// the native fn returns `List<ZZenohId>` directly; wrap each as ZenohId.
839+
return io.zenoh.jni.session.zSessionPeersZid(zSession, throwZError0).map { ZenohId(it) }
834840
}
835841

836842
@Throws(ZError::class)
837843
internal fun getRoutersId(): List<ZenohId> {
838844
val zSession = zSession ?: throw sessionClosedException
839-
return wrapJNIExceptionAsZError {
840-
io.zenoh.jni.session.zSessionRoutersZid(zSession, ArrayList<ZenohId>()) { list, zid ->
841-
list.add(ZenohId(zid)); list
842-
}
843-
}
845+
return io.zenoh.jni.session.zSessionRoutersZid(zSession, throwZError0).map { ZenohId(it) }
844846
}
845847

846848
/** Launches the session, returning the [Session] on success. */
@@ -849,9 +851,8 @@ class Session private constructor(private val config: Config) : AutoCloseable {
849851
ZenohLoad
850852
// `z_open` consumes the config by value; clone so the SDK [Config] stays
851853
// reusable for the caller (e.g. opening more than one session).
852-
this.zSession = wrapJNIExceptionAsZError {
853-
io.zenoh.jni.session.zOpen(io.zenoh.jni.config.zConfigClone(config.zConfig))
854-
}
854+
this.zSession =
855+
io.zenoh.jni.session.zOpen(io.zenoh.jni.config.zConfigClone(config.zConfig, throwZError0), throwZError)
855856
return this
856857
}
857858
}

0 commit comments

Comments
 (0)