Skip to content

Commit 7c9afdb

Browse files
SyncServer: check if closed before calling native methods.
1 parent aea6477 commit 7c9afdb

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

objectbox-java/src/main/java/io/objectbox/sync/server/SyncServerImpl.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ public class SyncServerImpl implements SyncServer {
2525
this.url = builder.url;
2626

2727
long storeHandle = InternalAccess.getHandle(builder.boxStore);
28-
handle = nativeCreate(storeHandle, url, builder.certificatePath);
28+
long handle = nativeCreate(storeHandle, url, builder.certificatePath);
2929
if (handle == 0) {
3030
throw new RuntimeException("Failed to create sync server: handle is zero.");
3131
}
32+
this.handle = handle;
3233

3334
for (SyncCredentials credentials : builder.credentials) {
3435
SyncCredentialsToken credentialsInternal = (SyncCredentialsToken) credentials;
@@ -46,40 +47,48 @@ public class SyncServerImpl implements SyncServer {
4647
}
4748
}
4849

50+
private long getHandle() {
51+
long handle = this.handle;
52+
if (handle == 0) {
53+
throw new IllegalStateException("SyncServer already closed");
54+
}
55+
return handle;
56+
}
57+
4958
@Override
5059
public String getUrl() {
5160
return url;
5261
}
5362

5463
@Override
5564
public int getPort() {
56-
return nativeGetPort(handle);
65+
return nativeGetPort(getHandle());
5766
}
5867

5968
@Override
6069
public boolean isRunning() {
61-
return nativeIsRunning(handle);
70+
return nativeIsRunning(getHandle());
6271
}
6372

6473
@Override
6574
public String getStatsString() {
66-
return nativeGetStatsString(handle);
75+
return nativeGetStatsString(getHandle());
6776
}
6877

6978
@Override
7079
public void setSyncChangeListener(@Nullable SyncChangeListener changesListener) {
7180
this.syncChangeListener = changesListener;
72-
nativeSetSyncChangesListener(handle, changesListener);
81+
nativeSetSyncChangesListener(getHandle(), changesListener);
7382
}
7483

7584
@Override
7685
public void start() {
77-
nativeStart(handle);
86+
nativeStart(getHandle());
7887
}
7988

8089
@Override
8190
public void stop() {
82-
nativeStop(handle);
91+
nativeStop(getHandle());
8392
}
8493

8594
@Override

0 commit comments

Comments
 (0)