|
| 1 | +package io.a2a.integrations.microprofile; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | + |
| 5 | +import jakarta.enterprise.context.ApplicationScoped; |
| 6 | +import jakarta.enterprise.inject.Alternative; |
| 7 | +import jakarta.annotation.Priority; |
| 8 | +import jakarta.inject.Inject; |
| 9 | + |
| 10 | +import io.a2a.server.config.A2AConfigProvider; |
| 11 | +import io.a2a.server.config.DefaultValuesConfigProvider; |
| 12 | +import org.eclipse.microprofile.config.Config; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +/** |
| 17 | + * MicroProfile Config-based implementation of {@link A2AConfigProvider}. |
| 18 | + * <p> |
| 19 | + * This provider integrates with MicroProfile Config (used by Quarkus and other Jakarta EE runtimes) |
| 20 | + * to allow configuration via standard sources: |
| 21 | + * <ul> |
| 22 | + * <li>System properties (-D flags)</li> |
| 23 | + * <li>Environment variables</li> |
| 24 | + * <li>application.properties</li> |
| 25 | + * <li>Custom ConfigSources</li> |
| 26 | + * </ul> |
| 27 | + * <p> |
| 28 | + * Falls back to {@link DefaultValuesConfigProvider} when a configuration value is not found |
| 29 | + * in MicroProfile Config, ensuring that default values from {@code META-INF/a2a-defaults.properties} |
| 30 | + * are always available. |
| 31 | + * <p> |
| 32 | + * This provider is automatically enabled with {@code @Priority(50)}, but can be overridden by |
| 33 | + * custom providers with higher priority. |
| 34 | + * <p> |
| 35 | + * To use this provider, add the {@code a2a-java-sdk-microprofile-config} dependency to your project. |
| 36 | + */ |
| 37 | +@ApplicationScoped |
| 38 | +@Alternative |
| 39 | +@Priority(50) |
| 40 | +public class MicroProfileConfigProvider implements A2AConfigProvider { |
| 41 | + |
| 42 | + private static final Logger LOGGER = LoggerFactory.getLogger(MicroProfileConfigProvider.class); |
| 43 | + |
| 44 | + @Inject |
| 45 | + Config mpConfig; |
| 46 | + |
| 47 | + @Inject |
| 48 | + DefaultValuesConfigProvider defaultValues; |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getValue(String name) { |
| 52 | + Optional<String> value = mpConfig.getOptionalValue(name, String.class); |
| 53 | + if (value.isPresent()) { |
| 54 | + LOGGER.trace("Config value '{}' = '{}' (from MicroProfile Config)", name, value.get()); |
| 55 | + return value.get(); |
| 56 | + } |
| 57 | + |
| 58 | + // Fallback to defaults |
| 59 | + String defaultValue = defaultValues.getValue(name); |
| 60 | + LOGGER.trace("Config value '{}' = '{}' (from DefaultValuesConfigProvider)", name, defaultValue); |
| 61 | + return defaultValue; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Optional<String> getOptionalValue(String name) { |
| 66 | + Optional<String> value = mpConfig.getOptionalValue(name, String.class); |
| 67 | + if (value.isPresent()) { |
| 68 | + LOGGER.trace("Optional config value '{}' = '{}' (from MicroProfile Config)", name, value.get()); |
| 69 | + return value; |
| 70 | + } |
| 71 | + |
| 72 | + // Fallback to defaults |
| 73 | + Optional<String> defaultValue = defaultValues.getOptionalValue(name); |
| 74 | + LOGGER.trace("Optional config value '{}' = '{}' (from DefaultValuesConfigProvider)", |
| 75 | + name, defaultValue.orElse("<absent>")); |
| 76 | + return defaultValue; |
| 77 | + } |
| 78 | +} |
0 commit comments