-
Notifications
You must be signed in to change notification settings - Fork 1
Java SDK: All tests passing (294/294 unit, 138/138 cross-SDK) #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0cf6ccf
e399c35
419c47e
935a36b
07c067d
12365ed
38327cb
a7a3604
76e0293
e4b79c0
05a8039
61dd56c
b9d48f6
f66822a
980f8fb
4e1d396
ba6c3e5
2b2775c
c70c02d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| package com.absmartly.sdk; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java8.util.concurrent.CompletableFuture; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
|
|
||
| import com.absmartly.sdk.java.time.Clock; | ||
| import com.absmartly.sdk.json.ContextData; | ||
|
|
||
| public class ABsmartly implements Closeable { | ||
| public static ABsmartly create(@Nonnull ABsmartlyConfig config) { | ||
| return new ABsmartly(config); | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static class Builder { | ||
| private String endpoint; | ||
| private String apiKey; | ||
| private String application; | ||
| private String environment; | ||
| private ContextEventLogger eventLogger; | ||
|
|
||
| Builder() {} | ||
|
|
||
| public Builder endpoint(@Nonnull String endpoint) { | ||
| this.endpoint = endpoint; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder apiKey(@Nonnull String apiKey) { | ||
| this.apiKey = apiKey; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder application(@Nonnull String application) { | ||
| this.application = application; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder environment(@Nonnull String environment) { | ||
| this.environment = environment; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder eventLogger(@Nonnull ContextEventLogger eventLogger) { | ||
| this.eventLogger = eventLogger; | ||
| return this; | ||
| } | ||
|
|
||
| public ABsmartly build() { | ||
| if (endpoint == null) throw new IllegalArgumentException("endpoint is required"); | ||
| if (apiKey == null) throw new IllegalArgumentException("apiKey is required"); | ||
| if (application == null) throw new IllegalArgumentException("application is required"); | ||
| if (environment == null) throw new IllegalArgumentException("environment is required"); | ||
|
|
||
| final ClientConfig clientConfig = ClientConfig.create() | ||
| .setEndpoint(endpoint) | ||
| .setAPIKey(apiKey) | ||
| .setApplication(application) | ||
| .setEnvironment(environment); | ||
|
|
||
| final ABsmartlyConfig config = ABsmartlyConfig.create() | ||
| .setClient(Client.create(clientConfig)); | ||
|
|
||
| if (eventLogger != null) { | ||
| config.setContextEventLogger(eventLogger); | ||
| } | ||
|
|
||
| return create(config); | ||
| } | ||
| } | ||
|
|
||
| protected ABsmartly(@Nonnull ABsmartlyConfig config) { | ||
| contextDataProvider_ = config.getContextDataProvider(); | ||
| contextEventHandler_ = config.getContextPublisher(); | ||
| contextEventLogger_ = config.getContextEventLogger(); | ||
| variableParser_ = config.getVariableParser(); | ||
| audienceDeserializer_ = config.getAudienceDeserializer(); | ||
| scheduler_ = config.getScheduler(); | ||
|
|
||
| if ((contextDataProvider_ == null) || (contextEventHandler_ == null)) { | ||
| client_ = config.getClient(); | ||
| if (client_ == null) { | ||
| throw new IllegalArgumentException("Missing Client instance"); | ||
| } | ||
|
|
||
| if (contextDataProvider_ == null) { | ||
| contextDataProvider_ = new DefaultContextDataProvider(client_); | ||
| } | ||
|
|
||
| if (contextEventHandler_ == null) { | ||
| contextEventHandler_ = new DefaultContextPublisher(client_); | ||
| } | ||
| } | ||
|
|
||
| if (variableParser_ == null) { | ||
| variableParser_ = new DefaultVariableParser(); | ||
| } | ||
|
|
||
| if (audienceDeserializer_ == null) { | ||
| audienceDeserializer_ = new DefaultAudienceDeserializer(); | ||
| } | ||
|
|
||
| if (scheduler_ == null) { | ||
| scheduler_ = new ScheduledThreadPoolExecutor(1); | ||
| } | ||
| } | ||
|
|
||
| public Context createContext(@Nonnull ContextConfig config) { | ||
| checkNotClosed(); | ||
| return Context.create(Clock.systemUTC(), config, scheduler_, contextDataProvider_.getContextData(), | ||
| contextDataProvider_, contextEventHandler_, contextEventLogger_, variableParser_, | ||
| new AudienceMatcher(audienceDeserializer_)); | ||
| } | ||
|
|
||
| public Context createContextWith(@Nonnull ContextConfig config, ContextData data) { | ||
| checkNotClosed(); | ||
| return Context.create(Clock.systemUTC(), config, scheduler_, CompletableFuture.completedFuture(data), | ||
| contextDataProvider_, contextEventHandler_, contextEventLogger_, variableParser_, | ||
| new AudienceMatcher(audienceDeserializer_)); | ||
| } | ||
|
|
||
| public CompletableFuture<ContextData> getContextData() { | ||
| checkNotClosed(); | ||
| return contextDataProvider_.getContextData(); | ||
| } | ||
|
|
||
| private void checkNotClosed() { | ||
| if (closed_) { | ||
| throw new IllegalStateException("ABsmartly instance is closed"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| if (closed_) { | ||
| return; | ||
| } | ||
| closed_ = true; | ||
|
|
||
|
Comment on lines
+144
to
+148
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: wc -l core-api/src/main/java/com/absmartly/sdk/ABsmartly.javaRepository: absmartly/java-sdk Length of output: 121 🏁 Script executed: sed -n '140,175p' core-api/src/main/java/com/absmartly/sdk/ABsmartly.javaRepository: absmartly/java-sdk Length of output: 863 🏁 Script executed: grep -n "private.*closed_" core-api/src/main/java/com/absmartly/sdk/ABsmartly.javaRepository: absmartly/java-sdk Length of output: 100 🏁 Script executed: grep -n "synchronized\|Lock\|AtomicBoolean\|volatile" core-api/src/main/java/com/absmartly/sdk/ABsmartly.javaRepository: absmartly/java-sdk Length of output: 100 🏁 Script executed: grep -n "void close\|public.*close" core-api/src/main/java/com/absmartly/sdk/ABsmartly.javaRepository: absmartly/java-sdk Length of output: 107 Use AtomicBoolean to prevent concurrent double-close. The check-then-set sequence on lines 144-148 is not atomic; two threads entering Suggested fix+import java.util.concurrent.atomic.AtomicBoolean;
`@Override`
public void close() throws IOException {
- if (closed_) {
+ if (!closed_.compareAndSet(false, true)) {
return;
}
- closed_ = true;
if (client_ != null) {
client_.close();
}
if (scheduler_ != null) {
scheduler_.shutdown();
try {
if (!scheduler_.awaitTermination(5000, TimeUnit.MILLISECONDS)) {
scheduler_.shutdownNow();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
scheduler_.shutdownNow();
}
}
}
-private volatile boolean closed_;
+private final AtomicBoolean closed_ = new AtomicBoolean(false);🤖 Prompt for AI Agents |
||
| try { | ||
| if (client_ != null) { | ||
| client_.close(); | ||
| } | ||
| } finally { | ||
| if (scheduler_ != null) { | ||
| scheduler_.shutdown(); | ||
| try { | ||
| if (!scheduler_.awaitTermination(5000, TimeUnit.MILLISECONDS)) { | ||
| scheduler_.shutdownNow(); | ||
| } | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| scheduler_.shutdownNow(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private volatile boolean closed_; | ||
| private Client client_; | ||
| private ContextDataProvider contextDataProvider_; | ||
| private ContextPublisher contextEventHandler_; | ||
| private ContextEventLogger contextEventLogger_; | ||
| private VariableParser variableParser_; | ||
|
|
||
| private AudienceDeserializer audienceDeserializer_; | ||
| private ScheduledExecutorService scheduler_; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document the Java 6/7 → 8 bytecode break in the release notes.
Bumping
sourceCompatibilityandtargetCompatibilityfrom1.6to1.8changes the published JAR's minimum required JVM from Java 6 to Java 8. Any downstream consumer still running Java 6 or 7 will fail to load the SDK at class-loading time. This is a binary-breaking change and should be called out explicitly in the changelog or release notes alongside the version bump.🤖 Prompt for AI Agents