1313import io .vavr .Lazy ;
1414import java .util .HashMap ;
1515import java .util .List ;
16- import java .util .concurrent .atomic .AtomicBoolean ;
1716import javax .annotation .Nonnull ;
1817import lombok .AllArgsConstructor ;
1918import lombok .extern .slf4j .Slf4j ;
2726@ AllArgsConstructor
2827class 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