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

io.quarkus.reactive.datasource.runtime.ReactiveDataSourceUtil Maven / Gradle / Ivy

There is a newer version: 3.17.0.CR1
Show newest version
package io.quarkus.reactive.datasource.runtime;

import java.lang.annotation.Annotation;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;

import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.spi.Bean;

import io.quarkus.arc.Arc;
import io.quarkus.arc.InjectableInstance;
import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.reactive.datasource.ReactiveDataSource;
import io.vertx.sqlclient.Pool;

public final class ReactiveDataSourceUtil {
    private ReactiveDataSourceUtil() {
    }

    public static InjectableInstance dataSourceInstance(String dataSourceName) {
        return dataSourceInstance(Pool.class, dataSourceName);
    }

    public static  InjectableInstance dataSourceInstance(Class type, String dataSourceName) {
        return Arc.container().select(type, qualifier(dataSourceName));
    }

    public static Optional dataSourceIfActive(String dataSourceName) {
        return dataSourceIfActive(Pool.class, dataSourceName);
    }

    public static  Optional dataSourceIfActive(Class type, String dataSourceName) {
        var instance = dataSourceInstance(type, dataSourceName);
        // We want to call get() and throw an exception if the name points to an undefined datasource.
        if (!instance.isResolvable() || instance.getHandle().getBean().isActive()) {
            return Optional.ofNullable(instance.get());
        } else {
            return Optional.empty();
        }
    }

    public static Set activeDataSourceNames() {
        Set activeNames = new LinkedHashSet<>();
        for (var handle : Arc.container().select(Pool.class).handles()) {
            var bean = handle.getBean();
            if (bean != null && bean.isActive()) {
                String name = dataSourceName(bean);
                if (name != null) { // There may be custom beans, these will have a null name and will be ignored.
                    activeNames.add(name);
                }
            }
        }
        return activeNames;
    }

    public static String dataSourceName(Bean bean) {
        for (Object qualifier : bean.getQualifiers()) {
            if (qualifier instanceof ReactiveDataSource) {
                return ((ReactiveDataSource) qualifier).value();
            }
        }
        return DataSourceUtil.DEFAULT_DATASOURCE_NAME;
    }

    public static Annotation qualifier(String dataSourceName) {
        if (DataSourceUtil.isDefault(dataSourceName)) {
            return Default.Literal.INSTANCE;
        } else {
            return new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy