Skip to content

Commit fd6c29b

Browse files
committed
Merge branch '123-sync-closed-checks' into dev
2 parents 2e7d37d + 7c9afdb commit fd6c29b

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
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() {

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)