Skip to content

Commit 2729b0b

Browse files
committed
Add missing obxAdminUser credentials, minor cleanup objectbox-java#252
Throw FeatureNotAvailableException if sync is unavailable Add docs to new credentials builder methods
1 parent ffbc9c6 commit 2729b0b

File tree

8 files changed

+73
-36
lines changed

8 files changed

+73
-36
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import io.objectbox.BoxStore;
2626
import io.objectbox.annotation.apihint.Internal;
27+
import io.objectbox.exception.FeatureNotAvailableException;
2728
import io.objectbox.sync.internal.Platform;
2829
import io.objectbox.sync.listener.SyncChangeListener;
2930
import io.objectbox.sync.listener.SyncCompletedListener;
@@ -85,15 +86,20 @@ public enum RequestUpdatesMode {
8586
AUTO_NO_PUSHES
8687
}
8788

88-
@Internal
89-
public SyncBuilder(BoxStore boxStore, SyncCredentials credentials) {
90-
checkNotNull(boxStore, "BoxStore is required.");
91-
checkNotNull(credentials, "Sync credentials are required.");
89+
private static void checkSyncFeatureAvailable() {
9290
if (!BoxStore.isSyncAvailable()) {
93-
throw new IllegalStateException(
91+
throw new FeatureNotAvailableException(
9492
"This library does not include ObjectBox Sync. " +
9593
"Please visit https://objectbox.io/sync/ for options.");
9694
}
95+
}
96+
97+
98+
@Internal
99+
public SyncBuilder(BoxStore boxStore, SyncCredentials credentials) {
100+
checkNotNull(boxStore, "BoxStore is required.");
101+
checkNotNull(credentials, "Sync credentials are required.");
102+
checkSyncFeatureAvailable();
97103
this.platform = Platform.findPlatform();
98104
this.boxStore = boxStore;
99105
this.credentials = Collections.singletonList(credentials);
@@ -105,11 +111,7 @@ public SyncBuilder(BoxStore boxStore, SyncCredentials[] multipleCredentials) {
105111
if (multipleCredentials.length == 0) {
106112
throw new IllegalArgumentException("At least one Sync credential is required.");
107113
}
108-
if (!BoxStore.isSyncAvailable()) {
109-
throw new IllegalStateException(
110-
"This library does not include ObjectBox Sync. " +
111-
"Please visit https://objectbox.io/sync/ for options.");
112-
}
114+
checkSyncFeatureAvailable();
113115
this.platform = Platform.findPlatform();
114116
this.boxStore = boxStore;
115117
this.credentials = Arrays.asList(multipleCredentials);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public void setLoginCredentials(SyncCredentials credentials) {
206206
public void setLoginCredentials(SyncCredentials[] multipleCredentials) {
207207
for (int i = 0; i < multipleCredentials.length; i++) {
208208
SyncCredentials credentials = multipleCredentials[i];
209-
boolean isLast = i == multipleCredentials.length - 1;
209+
boolean isLast = i == (multipleCredentials.length - 1);
210210
if (credentials instanceof SyncCredentialsToken) {
211211
SyncCredentialsToken credToken = (SyncCredentialsToken) credentials;
212212
nativeAddLoginCredentials(getHandle(), credToken.getTypeId(), credToken.getTokenBytes(), isLast);

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,44 @@ public static SyncCredentials google(String idToken) {
4848
return new SyncCredentialsToken(CredentialsType.GOOGLE, idToken);
4949
}
5050

51+
/**
52+
* ObjectBox admin users (username/password)
53+
*/
54+
public static SyncCredentials obxAdminUser(String user, String password) {
55+
return new SyncCredentialsUserPassword(CredentialsType.OBX_ADMIN_USER, user, password);
56+
}
57+
58+
/**
59+
* Generic credential type suitable for ObjectBox admin (and possibly others in the future)
60+
*/
5161
public static SyncCredentials userAndPassword(String user, String password) {
52-
return new SyncCredentialsUserPassword(user, password);
62+
return new SyncCredentialsUserPassword(CredentialsType.USER_PASSWORD, user, password);
5363
}
5464

65+
/**
66+
* JSON Web Token (JWT): an ID token that typically provides identity information about the authenticated user.
67+
*/
5568
public static SyncCredentials jwtIdToken(String jwtIdToken) {
5669
return new SyncCredentialsToken(CredentialsType.JWT_ID_TOKEN, jwtIdToken);
5770
}
5871

72+
/**
73+
* JSON Web Token (JWT): an access token that is used to access resources.
74+
*/
5975
public static SyncCredentials jwtAccessToken(String jwtAccessToken) {
6076
return new SyncCredentialsToken(CredentialsType.JWT_ACCESS_TOKEN, jwtAccessToken);
6177
}
6278

79+
/**
80+
* JSON Web Token (JWT): a refresh token that is used to obtain a new access token.
81+
*/
6382
public static SyncCredentials jwtRefreshToken(String jwtRefreshToken) {
6483
return new SyncCredentialsToken(CredentialsType.JWT_REFRESH_TOKEN, jwtRefreshToken);
6584
}
6685

86+
/**
87+
* JSON Web Token (JWT): a token that is neither an ID, access, nor refresh token.
88+
*/
6789
public static SyncCredentials jwtCustomToken(String jwtCustomToken) {
6890
return new SyncCredentialsToken(CredentialsType.JWT_CUSTOM_TOKEN, jwtCustomToken);
6991
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public final class SyncCredentialsUserPassword extends SyncCredentials {
2828
private final String username;
2929
private final String password;
3030

31-
SyncCredentialsUserPassword(String username, String password) {
32-
super(CredentialsType.USER_PASSWORD);
31+
SyncCredentialsUserPassword(CredentialsType type, String username, String password) {
32+
super(type);
3333
this.username = username;
3434
this.password = password;
3535
}
@@ -44,6 +44,6 @@ public String getPassword() {
4444

4545
@Override
4646
SyncCredentials createClone() {
47-
return new SyncCredentialsUserPassword(this.username, this.password);
47+
return new SyncCredentialsUserPassword(getType(), this.username, this.password);
4848
}
4949
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
// automatically generated by the FlatBuffers compiler, do not modify
218

319
package io.objectbox.sync.server;

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import io.objectbox.BoxStore;
2727
import io.objectbox.annotation.apihint.Internal;
28+
import io.objectbox.exception.FeatureNotAvailableException;
2829
import io.objectbox.flatbuffers.FlatBufferBuilder;
2930
import io.objectbox.sync.Credentials;
3031
import io.objectbox.sync.Sync;
@@ -59,6 +60,14 @@ public final class SyncServerBuilder {
5960
private String claimIss;
6061
private String claimAud;
6162

63+
private static void checkFeatureSyncServerAvailable() {
64+
if (!BoxStore.isSyncServerAvailable()) {
65+
throw new FeatureNotAvailableException(
66+
"This library does not include ObjectBox Sync Server. " +
67+
"Please visit https://objectbox.io/sync/ for options.");
68+
}
69+
}
70+
6271
/**
6372
* Use {@link Sync#server(BoxStore, String, SyncCredentials)} instead.
6473
*/
@@ -67,11 +76,7 @@ public SyncServerBuilder(BoxStore boxStore, String url, SyncCredentials authenti
6776
checkNotNull(boxStore, "BoxStore is required.");
6877
checkNotNull(url, "Sync server URL is required.");
6978
checkNotNull(authenticatorCredentials, "Authenticator credentials are required.");
70-
if (!BoxStore.isSyncServerAvailable()) {
71-
throw new IllegalStateException(
72-
"This library does not include ObjectBox Sync Server. " +
73-
"Please visit https://objectbox.io/sync/ for options.");
74-
}
79+
checkFeatureSyncServerAvailable();
7580
this.boxStore = boxStore;
7681
try {
7782
this.url = new URI(url);
@@ -89,11 +94,7 @@ public SyncServerBuilder(BoxStore boxStore, String url, SyncCredentials[] multip
8994
checkNotNull(boxStore, "BoxStore is required.");
9095
checkNotNull(url, "Sync server URL is required.");
9196
checkNotNull(multipleAuthenticatorCredentials, "Authenticator credentials are required.");
92-
if (!BoxStore.isSyncServerAvailable()) {
93-
throw new IllegalStateException(
94-
"This library does not include ObjectBox Sync Server. " +
95-
"Please visit https://objectbox.io/sync/ for options.");
96-
}
97+
checkFeatureSyncServerAvailable();
9798
this.boxStore = boxStore;
9899
try {
99100
this.url = new URI(url);
@@ -179,7 +180,7 @@ public SyncServerBuilder peer(String url) {
179180
}
180181

181182
/**
182-
* @deprecated Use {@link #clusterPeer(String,SyncCredentials)} instead.
183+
* @deprecated Use {@link #clusterPeer(String, SyncCredentials)} instead.
183184
*/
184185
@Deprecated
185186
public SyncServerBuilder peer(String url, SyncCredentials credentials) {
@@ -263,7 +264,7 @@ public SyncServerBuilder syncServerFlags(int syncServerFlags) {
263264

264265
/**
265266
* Sets the number of workers for the main task pool.
266-
*
267+
* <p>
267268
* If not set or set to 0, this uses a hardware-dependant default, e.g. 3 * CPU "cores".
268269
*/
269270
public SyncServerBuilder workerThreads(int workerThreads) {

tests/objectbox-java-test/src/test/java/io/objectbox/sync/ConnectivityMonitorTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,10 @@ public void setSyncTimeListener(@Nullable SyncTimeListener timeListener) {
155155

156156
@Override
157157
public void setLoginCredentials(SyncCredentials credentials) {
158-
159158
}
160159

161160
@Override
162161
public void setLoginCredentials(SyncCredentials[] multipleCredentials) {
163-
164162
}
165163

166164
@Override
@@ -170,17 +168,14 @@ public boolean awaitFirstLogin(long millisToWait) {
170168

171169
@Override
172170
public void start() {
173-
174171
}
175172

176173
@Override
177174
public void stop() {
178-
179175
}
180176

181177
@Override
182178
public void close() {
183-
184179
}
185180

186181
@Override

tests/objectbox-java-test/src/test/java/io/objectbox/sync/SyncTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.objectbox.sync;
1818

19+
import io.objectbox.exception.FeatureNotAvailableException;
1920
import org.junit.Test;
2021

2122
import io.objectbox.AbstractObjectBoxTest;
@@ -53,8 +54,8 @@ public void serverIsNotAvailable() {
5354

5455
@Test
5556
public void creatingSyncClient_throws() {
56-
IllegalStateException exception = assertThrows(
57-
IllegalStateException.class,
57+
FeatureNotAvailableException exception = assertThrows(
58+
FeatureNotAvailableException.class,
5859
() -> Sync.client(store, "wss://127.0.0.1", SyncCredentials.none())
5960
);
6061
String message = exception.getMessage();
@@ -64,8 +65,8 @@ public void creatingSyncClient_throws() {
6465

6566
@Test
6667
public void creatingSyncServer_throws() {
67-
IllegalStateException exception = assertThrows(
68-
IllegalStateException.class,
68+
FeatureNotAvailableException exception = assertThrows(
69+
FeatureNotAvailableException.class,
6970
() -> Sync.server(store, "wss://127.0.0.1", SyncCredentials.none())
7071
);
7172
String message = exception.getMessage();

0 commit comments

Comments
 (0)