Skip to content

Commit 7a37220

Browse files
service bindings static
1 parent 32bb886 commit 7a37220

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

core/src/main/java/com/sap/ai/sdk/core/AiCoreServiceKeyAccessor.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import io.vavr.Lazy;
1414
import java.util.HashMap;
1515
import java.util.List;
16-
import java.util.concurrent.atomic.AtomicBoolean;
1716
import javax.annotation.Nonnull;
1817
import lombok.AllArgsConstructor;
1918
import lombok.extern.slf4j.Slf4j;
@@ -27,10 +26,11 @@
2726
@AllArgsConstructor
2827
class AiCoreServiceKeyAccessor implements ServiceBindingAccessor {
2928
static final String ENV_VAR_KEY = "AICORE_SERVICE_KEY";
30-
private static final AtomicBoolean INFO_LOG_EMITTED = new AtomicBoolean(false);
3129

3230
private final Lazy<Dotenv> dotenv;
3331

32+
private static List<ServiceBinding> serviceBindings;
33+
3434
AiCoreServiceKeyAccessor() {
3535
this(Dotenv.configure().ignoreIfMissing().ignoreIfMalformed());
3636
}
@@ -42,6 +42,14 @@ class AiCoreServiceKeyAccessor implements ServiceBindingAccessor {
4242
@Nonnull
4343
@Override
4444
public List<ServiceBinding> getServiceBindings() throws ServiceBindingAccessException {
45+
// service bindings are immutable for the lifetime of the application
46+
if (serviceBindings == null) {
47+
serviceBindings = fetchServiceBindings();
48+
}
49+
return serviceBindings;
50+
}
51+
52+
private List<ServiceBinding> fetchServiceBindings() throws ServiceBindingAccessException {
4553
final String serviceKey;
4654
try {
4755
serviceKey = dotenv.get().get(ENV_VAR_KEY);
@@ -52,15 +60,13 @@ public List<ServiceBinding> getServiceBindings() throws ServiceBindingAccessExce
5260
log.debug("No service key found in environment variable {}", ENV_VAR_KEY);
5361
return List.of();
5462
}
55-
if (INFO_LOG_EMITTED.compareAndSet(false, true)) {
56-
log.info(
57-
"""
58-
Found a service key in environment variable {}.
59-
Using a service key is recommended for local testing only.
60-
Bind the AI Core service to the application for productive usage.
61-
""",
62-
ENV_VAR_KEY);
63-
}
63+
log.info(
64+
"""
65+
Found a service key in environment variable {}.
66+
Using a service key is recommended for local testing only.
67+
Bind the AI Core service to the application for productive usage.
68+
""",
69+
ENV_VAR_KEY);
6470

6571
val binding = createServiceBinding(serviceKey);
6672
return List.of(binding);

0 commit comments

Comments
 (0)