org.eclipse.microprofile.config.spi.ConfigSourceProvider Java Examples

The following examples show how to use org.eclipse.microprofile.config.spi.ConfigSourceProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SmallRyeConfigBuilder.java    From smallrye-config with Apache License 2.0 6 votes vote down vote up
List<ConfigSource> discoverSources() {
    List<ConfigSource> discoveredSources = new ArrayList<>();
    ServiceLoader<ConfigSource> configSourceLoader = ServiceLoader.load(ConfigSource.class, classLoader);
    configSourceLoader.forEach(discoveredSources::add);

    // load all ConfigSources from ConfigSourceProviders
    ServiceLoader<ConfigSourceProvider> configSourceProviderLoader = ServiceLoader.load(ConfigSourceProvider.class,
            classLoader);
    configSourceProviderLoader.forEach(configSourceProvider -> configSourceProvider.getConfigSources(classLoader)
            .forEach(discoveredSources::add));

    ServiceLoader<ConfigSourceFactory> configSourceFactoryLoader = ServiceLoader.load(ConfigSourceFactory.class,
            classLoader);
    configSourceFactoryLoader.forEach(factory -> discoveredSources.add(new ConfigurableConfigSource(factory)));

    return discoveredSources;
}
 
Example #2
Source File: CustomConfigSourceTest.java    From microprofile-config with Apache License 2.0 6 votes vote down vote up
@Deployment
public static WebArchive deploy() {
    JavaArchive testJar = ShrinkWrap
            .create(JavaArchive.class, "customConfigSourceTest.jar")
            .addClasses(CustomConfigSourceTest.class, CustomDbConfigSource.class, CustomConfigSourceProvider.class)
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
            .addAsServiceProvider(ConfigSource.class, CustomDbConfigSource.class)
            .addAsServiceProvider(ConfigSourceProvider.class, CustomConfigSourceProvider.class)
            .as(JavaArchive.class);

    addFile(testJar, "META-INF/microprofile-config.properties");

    WebArchive war = ShrinkWrap
            .create(WebArchive.class, "customConfigSourceTest.war")
            .addAsLibrary(testJar);
    return war;
}
 
Example #3
Source File: SpringCloudConfigClientRecorder.java    From quarkus with Apache License 2.0 6 votes vote down vote up
public RuntimeValue<ConfigSourceProvider> create(SpringCloudConfigClientConfig springCloudConfigClientConfig,
        ApplicationConfig applicationConfig) {
    if (!springCloudConfigClientConfig.enabled) {
        log.debug(
                "No attempt will be made to obtain configuration from the Spring Cloud Config Server because the functionality has been disabled via configuration");
        return emptyRuntimeValue();
    }

    if (!applicationConfig.name.isPresent()) {
        log.warn(
                "No attempt will be made to obtain configuration from the Spring Cloud Config Server because the application name has not been set. Consider setting it via 'quarkus.application.name'");
        return emptyRuntimeValue();
    }

    return new RuntimeValue<>(new SpringCloudConfigServerClientConfigSourceProvider(
            springCloudConfigClientConfig, applicationConfig.name.get(), ProfileManager.getActiveProfile()));
}
 
Example #4
Source File: ConverterTest.java    From microprofile-config with Apache License 2.0 6 votes vote down vote up
@Deployment
public static WebArchive deploy() {
    JavaArchive testJar = ShrinkWrap
            .create(JavaArchive.class, "converterTest.jar")
            .addClass(ConverterTest.class)
            .addPackage(CustomDbConfigSource.class.getPackage())
            .addClasses(DuckConverter.class, Duck.class, Donald.class, UpperCaseDuckConverter.class)
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
            .addAsServiceProvider(ConfigSource.class, CustomDbConfigSource.class)
            .addAsServiceProvider(ConfigSourceProvider.class, CustomConfigSourceProvider.class)
            .addAsServiceProvider(Converter.class, DuckConverter.class)
            .as(JavaArchive.class);

    AbstractTest.addFile(testJar, "META-INF/microprofile-config.properties");
    AbstractTest.addFile(testJar, "sampleconfig.yaml");

    WebArchive war = ShrinkWrap
            .create(WebArchive.class, "converterTest.war")
            .addAsLibrary(testJar);
    return war;
}
 
Example #5
Source File: ConfigBuildSteps.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@BuildStep
void nativeServiceProviders(
        final BuildProducer<ServiceProviderBuildItem> providerProducer) throws IOException {
    providerProducer.produce(new ServiceProviderBuildItem(ConfigProviderResolver.class.getName(),
            SmallRyeConfigProviderResolver.class.getName()));
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    classLoader.getResources(SERVICES_PREFIX + ConfigSourceProvider.class.getName());
    for (Class<?> serviceClass : Arrays.asList(
            ConfigSource.class,
            ConfigSourceProvider.class,
            Converter.class)) {
        final String serviceName = serviceClass.getName();
        final Set<String> names = ServiceUtil.classNamesNamedIn(classLoader, SERVICES_PREFIX + serviceName);
        final List<String> list = names.stream()
                // todo: see https://github.com/quarkusio/quarkus/issues/5492
                .filter(s -> !s.startsWith("org.jboss.resteasy.microprofile.config.")).collect(Collectors.toList());
        if (!list.isEmpty()) {
            providerProducer.produce(new ServiceProviderBuildItem(serviceName, list));
        }
    }
}
 
Example #6
Source File: DummyBootstrapRecorder2.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public RuntimeValue<ConfigSourceProvider> create() {
    return new RuntimeValue<>(new ConfigSourceProvider() {
        @Override
        public Iterable<ConfigSource> getConfigSources(ClassLoader forClassLoader) {
            return Collections.emptyList();
        }
    });
}
 
Example #7
Source File: ConfigUtils.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Add a configuration source provider to the builder.
 *
 * @param builder the builder
 * @param provider the provider to add
 */
public static void addSourceProvider(SmallRyeConfigBuilder builder, ConfigSourceProvider provider) {
    final Iterable<ConfigSource> sources = provider.getConfigSources(Thread.currentThread().getContextClassLoader());
    for (ConfigSource source : sources) {
        builder.withSources(source);
    }
}
 
Example #8
Source File: KubernetesConfigRecorder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public RuntimeValue<ConfigSourceProvider> configSources(KubernetesConfigSourceConfig kubernetesConfigSourceConfig,
        KubernetesClientBuildConfig clientConfig) {
    if (!kubernetesConfigSourceConfig.enabled) {
        log.debug(
                "No attempt will be made to obtain configuration from the Kubernetes API server because the functionality has been disabled via configuration");
        return emptyRuntimeValue();
    }

    return new RuntimeValue<>(new KubernetesConfigSourceProvider(kubernetesConfigSourceConfig,
            KubernetesClientUtils.createClient(clientConfig)));
}
 
Example #9
Source File: ConsulConfigRecorder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public RuntimeValue<ConfigSourceProvider> configSources(ConsulConfig consulConfig) {
    if (!consulConfig.enabled) {
        log.debug(
                "No attempt will be made to obtain configuration from Consul because the functionality has been disabled via configuration");
        return emptyRuntimeValue();
    }

    return new RuntimeValue<>(
            new ConsulConfigSourceProvider(consulConfig));
}
 
Example #10
Source File: KubernetesConfigRecorder.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private RuntimeValue<ConfigSourceProvider> emptyRuntimeValue() {
    return new RuntimeValue<>(new EmptyConfigSourceProvider());
}
 
Example #11
Source File: RunTimeConfigurationSourceValueBuildItem.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public RunTimeConfigurationSourceValueBuildItem(RuntimeValue<ConfigSourceProvider> configSourcesValue) {
    this.configSourcesValue = configSourcesValue;
}
 
Example #12
Source File: RunTimeConfigurationSourceValueBuildItem.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public RuntimeValue<ConfigSourceProvider> getConfigSourcesValue() {
    return configSourcesValue;
}
 
Example #13
Source File: ConsulConfigRecorder.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private RuntimeValue<ConfigSourceProvider> emptyRuntimeValue() {
    return new RuntimeValue<>(new EmptyConfigSourceProvider());
}
 
Example #14
Source File: RuntimeConfigSetupBuildStep.java    From quarkus with Apache License 2.0 4 votes vote down vote up
/**
 * Generates a StartupTask that sets up the final runtime configuration and thus runs before any StartupTask that uses
 * runtime configuration.
 * If there are recorders that produce a ConfigSourceProvider, these objects are used to set up the final runtime
 * configuration
 */
@BuildStep
@Consume(BootstrapConfigSetupCompleteBuildItem.class)
@Produce(RuntimeConfigSetupCompleteBuildItem.class)
void setupRuntimeConfig(List<RunTimeConfigurationSourceValueBuildItem> runTimeConfigurationSourceValues,
        BuildProducer<GeneratedClassBuildItem> generatedClass,
        BuildProducer<MainBytecodeRecorderBuildItem> mainBytecodeRecorder) {
    ClassOutput classOutput = new GeneratedClassGizmoAdaptor(generatedClass, true);

    try (ClassCreator clazz = ClassCreator.builder().classOutput(classOutput)
            .className(RUNTIME_CONFIG_STARTUP_TASK_CLASS_NAME)
            .interfaces(StartupTask.class).build()) {

        try (MethodCreator method = clazz.getMethodCreator("deploy", void.class, StartupContext.class)) {
            ResultHandle config = method.readStaticField(C_INSTANCE);

            if (runTimeConfigurationSourceValues.isEmpty()) {
                method.invokeVirtualMethod(RunTimeConfigurationGenerator.C_READ_CONFIG, config,
                        method.invokeStaticMethod(ofMethod(Collections.class, "emptyList", List.class)));
            } else {
                ResultHandle startupContext = method.getMethodParam(0);
                ResultHandle configSourcesProvidersList = method.newInstance(ofConstructor(ArrayList.class, int.class),
                        method.load(runTimeConfigurationSourceValues.size()));
                for (RunTimeConfigurationSourceValueBuildItem runTimeConfigurationSourceValue : runTimeConfigurationSourceValues) {
                    RuntimeValue<ConfigSourceProvider> runtimeValue = runTimeConfigurationSourceValue
                            .getConfigSourcesValue();
                    if (runtimeValue instanceof BytecodeRecorderImpl.ReturnedProxy) {
                        String proxyId = ((BytecodeRecorderImpl.ReturnedProxy) runtimeValue).__returned$proxy$key();
                        ResultHandle value = method.invokeVirtualMethod(
                                ofMethod(StartupContext.class, "getValue", Object.class, String.class),
                                startupContext, method.load(proxyId));
                        ResultHandle configSourceProvider = method
                                .invokeVirtualMethod(ofMethod(RuntimeValue.class, "getValue", Object.class),
                                        method.checkCast(value, RuntimeValue.class));
                        method.invokeVirtualMethod(
                                MethodDescriptor.ofMethod(ArrayList.class, "add", boolean.class, Object.class),
                                configSourcesProvidersList,
                                method.checkCast(configSourceProvider, ConfigSourceProvider.class));
                    } else {
                        log.warn("RuntimeValue " + runtimeValue
                                + " was not produced by a recorder and it will thus be ignored");
                    }
                }
                method.invokeVirtualMethod(RunTimeConfigurationGenerator.C_READ_CONFIG, config,
                        configSourcesProvidersList);
            }

            method.returnValue(null);
        }
    }

    mainBytecodeRecorder.produce(new MainBytecodeRecorderBuildItem(RUNTIME_CONFIG_STARTUP_TASK_CLASS_NAME));
}
 
Example #15
Source File: SpringCloudConfigClientRecorder.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private RuntimeValue<ConfigSourceProvider> emptyRuntimeValue() {
    return new RuntimeValue<>(new EmptyConfigSourceProvider());
}
 
Example #16
Source File: DummyBootstrapRecorder.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public RuntimeValue<ConfigSourceProvider> create(DummyConfig dummyConfig, ApplicationConfig applicationConfig) {
    return new RuntimeValue<>(new DummyConfigSourceProvider(dummyConfig, applicationConfig));
}
 
Example #17
Source File: ConfigUtils.java    From quarkus with Apache License 2.0 2 votes vote down vote up
/**
 * Add a configuration source providers to the builder.
 *
 * @param builder the builder
 * @param providers the providers to add
 */
public static void addSourceProviders(SmallRyeConfigBuilder builder, Collection<ConfigSourceProvider> providers) {
    for (ConfigSourceProvider provider : providers) {
        addSourceProvider(builder, provider);
    }
}