Skip to content

Commit aea6477

Browse files
SyncClient: check if closed before calling native methods.
1 parent d3b96d0 commit aea6477

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

objectbox-java/src/main/java/io/objectbox/sync/SyncClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public interface SyncClient extends Closeable {
132132
void start();
133133

134134
/**
135-
* Stops the client. Does nothing if the sync client is already stopped or closed.
135+
* Stops the client. Does nothing if the sync client is already stopped.
136136
*/
137137
void stop();
138138

objectbox-java/src/main/java/io/objectbox/sync/SyncClientImpl.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ public class SyncClientImpl implements SyncClient {
4848
this.connectivityMonitor = builder.platform.getConnectivityMonitor();
4949

5050
long boxStoreHandle = InternalAccess.getHandle(builder.boxStore);
51-
this.handle = nativeCreate(boxStoreHandle, serverUrl, builder.trustedCertPaths);
51+
long handle = nativeCreate(boxStoreHandle, serverUrl, builder.trustedCertPaths);
5252
if (handle == 0) {
5353
throw new RuntimeException("Failed to create sync client: handle is zero.");
5454
}
55+
this.handle = handle;
5556

5657
// Only change setting if not default (automatic sync updates and push subscription enabled).
5758
if (builder.requestUpdatesMode != RequestUpdatesMode.AUTO) {
@@ -84,6 +85,14 @@ public class SyncClientImpl implements SyncClient {
8485
InternalAccess.setSyncClient(builder.boxStore, this);
8586
}
8687

88+
private long getHandle() {
89+
long handle = this.handle;
90+
if (handle == 0) {
91+
throw new IllegalStateException("SyncClient already closed");
92+
}
93+
return handle;
94+
}
95+
8796
@Override
8897
public String getServerUrl() {
8998
return serverUrl;
@@ -101,24 +110,24 @@ public boolean isLoggedIn() {
101110

102111
@Override
103112
public long getServerTimeNanos() {
104-
return nativeServerTime(handle);
113+
return nativeServerTime(getHandle());
105114
}
106115

107116
@Override
108117
public long getServerTimeDiffNanos() {
109-
return nativeServerTimeDiff(handle);
118+
return nativeServerTimeDiff(getHandle());
110119
}
111120

112121
@Override
113122
public long getRoundtripTimeNanos() {
114-
return nativeRoundtripTime(handle);
123+
return nativeRoundtripTime(getHandle());
115124
}
116125

117126
/**
118127
* Gets the current state of this sync client. Throws if {@link #close()} was called.
119128
*/
120129
public SyncState getSyncState() {
121-
return SyncState.fromId(nativeGetState(handle));
130+
return SyncState.fromId(nativeGetState(getHandle()));
122131
}
123132

124133
@Override
@@ -133,7 +142,7 @@ public void setSyncCompletedListener(@Nullable SyncCompletedListener listener) {
133142

134143
@Override
135144
public void setSyncChangeListener(@Nullable SyncChangeListener changesListener) {
136-
nativeSetSyncChangesListener(handle, changesListener);
145+
nativeSetSyncChangesListener(getHandle(), changesListener);
137146
}
138147

139148
@Override
@@ -158,7 +167,7 @@ public void setSyncListener(@Nullable SyncListener listener) {
158167
@Override
159168
public void setLoginCredentials(SyncCredentials credentials) {
160169
SyncCredentialsToken credentialsInternal = (SyncCredentialsToken) credentials;
161-
nativeSetLoginInfo(handle, credentialsInternal.getTypeId(), credentialsInternal.getTokenBytes());
170+
nativeSetLoginInfo(getHandle(), credentialsInternal.getTypeId(), credentialsInternal.getTokenBytes());
162171
credentialsInternal.clear(); // Clear immediately, not needed anymore.
163172
}
164173

@@ -172,7 +181,7 @@ public boolean awaitFirstLogin(long millisToWait) {
172181

173182
@Override
174183
public synchronized void start() {
175-
nativeStart(handle);
184+
nativeStart(getHandle());
176185
started = true;
177186
if (connectivityMonitor != null) {
178187
connectivityMonitor.setObserver(this);
@@ -189,11 +198,7 @@ public synchronized void stop() {
189198
if (connectivityMonitor != null) {
190199
connectivityMonitor.removeObserver();
191200
}
192-
193-
long handleToStop = this.handle;
194-
if (handleToStop != 0) {
195-
nativeStop(handleToStop);
196-
}
201+
nativeStop(getHandle());
197202
started = false;
198203
}
199204

@@ -240,35 +245,35 @@ protected void finalize() throws Throwable {
240245
@Override
241246
@Experimental
242247
public boolean requestFullSync() {
243-
return nativeRequestFullSync(handle, false);
248+
return nativeRequestFullSync(getHandle(), false);
244249
}
245250

246251
/**
247252
* Temporary only, try not to use it.
248253
*/
249254
@Experimental
250255
public boolean requestFullSyncAndUpdates() {
251-
return nativeRequestFullSync(handle, true);
256+
return nativeRequestFullSync(getHandle(), true);
252257
}
253258

254259
@Override
255260
public boolean requestUpdates() {
256-
return nativeRequestUpdates(handle, true);
261+
return nativeRequestUpdates(getHandle(), true);
257262
}
258263

259264
@Override
260265
public boolean requestUpdatesOnce() {
261-
return nativeRequestUpdates(handle, false);
266+
return nativeRequestUpdates(getHandle(), false);
262267
}
263268

264269
@Override
265270
public boolean cancelUpdates() {
266-
return nativeCancelUpdates(handle);
271+
return nativeCancelUpdates(getHandle());
267272
}
268273

269274
@Override
270275
public void notifyConnectionAvailable() {
271-
nativeTriggerReconnect(handle);
276+
nativeTriggerReconnect(getHandle());
272277
}
273278

274279
@Override
@@ -452,7 +457,7 @@ public boolean send() {
452457
}
453458
checkNotSent();
454459
sent = true;
455-
return syncClient.nativeObjectsMessageSend(syncClient.handle, builderHandle);
460+
return syncClient.nativeObjectsMessageSend(syncClient.getHandle(), builderHandle);
456461
}
457462

458463
private void checkNotSent() {

0 commit comments

Comments
 (0)