All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.coherence.discovery.CoherenceConfigurationClient Maven / Gradle / Ivy

/*
 * Copyright 2017-2023 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.coherence.discovery;

import com.tangosol.net.NamedMap;
import com.tangosol.net.Session;
import io.micronaut.context.BeanContext;
import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.env.Environment;
import io.micronaut.context.env.EnvironmentPropertySource;
import io.micronaut.context.env.PropertySource;
import io.micronaut.discovery.config.ConfigurationClient;
import io.micronaut.runtime.ApplicationConfiguration;
import jakarta.inject.Singleton;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;

import java.util.*;

/**
 * A {@link ConfigurationClient} that works with Coherence as a config source.
 */
@Singleton
@BootstrapContextCompatible
@Requires(beans = CoherenceClientConfiguration.class)
public class CoherenceConfigurationClient implements ConfigurationClient {

    private static final Logger LOG = LoggerFactory.getLogger(CoherenceConfigurationClient.class);
    private static final String DEFAULT_APPLICATION = "application";

    private final ApplicationConfiguration applicationConfiguration;
    private final List> propertySources = new ArrayList<>();
    private final CoherenceClientConfiguration coherenceClientConfiguration;
    private final BeanContext beanContext;

    /**
     * Construct a new {@code CoherenceConfigurationClient}.
     *
     * @param applicationConfiguration the {@link ApplicationConfiguration application configuration}
     * @param coherenceClientConfiguration the {@link CoherenceClientConfiguration client configuration}
     * @param beanContext the {@link BeanContext bean context}
     */
    @SuppressWarnings("MnInjectionPoints")
    public CoherenceConfigurationClient(ApplicationConfiguration applicationConfiguration,
                                        CoherenceClientConfiguration coherenceClientConfiguration,
                                        BeanContext beanContext) {
        this.applicationConfiguration = applicationConfiguration;
        this.coherenceClientConfiguration = coherenceClientConfiguration;
        this.beanContext = beanContext;
        if (LOG.isDebugEnabled()) {
            LOG.debug("Coherence Client configuration: {}" , coherenceClientConfiguration);
        }
    }

    @Override
    public Publisher getPropertySources(Environment environment) {
        if (!coherenceClientConfiguration.isEnabled()) {
            return Flux.empty();
        }
        Session session = beanContext.createBean(Session.class, coherenceClientConfiguration.getSession());

        Map keys = buildSourceNames(applicationConfiguration, environment);
        for (Map.Entry entry : keys.entrySet()) {
            final Integer priority = entry.getKey();
            final String propertySource = entry.getValue();
            NamedMap configMap = session.getMap(propertySource);
            Flux propertySourceFlux = Flux.just(PropertySource.of(propertySource, configMap, priority));
            propertySources.add(propertySourceFlux);
        }
        return Flux.merge(propertySources);
    }

    /**
     * Build a map of config source names.
     *
     * @param applicationConfiguration  the application configuration
     * @param environment               the current environment
     *
     * @return a map of config source names
     */
    protected Map buildSourceNames(ApplicationConfiguration applicationConfiguration, Environment environment) {
        Optional configuredApplicationName = applicationConfiguration.getName();
        String applicationName = configuredApplicationName.orElse(null);
        Set environmentNames = environment.getActiveNames();

        Map configKeys = new HashMap<>();
        int baseOrder = EnvironmentPropertySource.POSITION + 100;
        configKeys.put(++baseOrder, DEFAULT_APPLICATION);
        if (applicationName != null) {
            configKeys.put(++baseOrder,  applicationName);
        }

        int envOrder = baseOrder + 50;
        for (String activeName : environmentNames) {
            configKeys.put(++envOrder, DEFAULT_APPLICATION + "-" + activeName);
            if (applicationName != null) {
                configKeys.put(++envOrder,  applicationName + "-" + activeName);
            }
        }
        return configKeys;
    }

    @Override
    public String getDescription() {
        return "Reads configuration from Oracle Coherence";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy