Search in sources :

Example 1 with DefaultDataSourceDbKindBuildItem

use of io.quarkus.datasource.deployment.spi.DefaultDataSourceDbKindBuildItem in project quarkus by quarkusio.

the class JDBCMariaDBProcessor method registerDriver.

@BuildStep
void registerDriver(BuildProducer<JdbcDriverBuildItem> jdbcDriver, BuildProducer<DefaultDataSourceDbKindBuildItem> dbKind) {
    jdbcDriver.produce(new JdbcDriverBuildItem(DatabaseKind.MARIADB, "org.mariadb.jdbc.Driver", "org.mariadb.jdbc.MariaDbDataSource"));
    dbKind.produce(new DefaultDataSourceDbKindBuildItem(DatabaseKind.MARIADB));
}
Also used : JdbcDriverBuildItem(io.quarkus.agroal.spi.JdbcDriverBuildItem) DefaultDataSourceDbKindBuildItem(io.quarkus.datasource.deployment.spi.DefaultDataSourceDbKindBuildItem) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 2 with DefaultDataSourceDbKindBuildItem

use of io.quarkus.datasource.deployment.spi.DefaultDataSourceDbKindBuildItem in project quarkus by quarkusio.

the class DevServicesDatasourceProcessor method launchDatabases.

@BuildStep(onlyIfNot = IsNormal.class, onlyIf = GlobalDevServicesConfig.Enabled.class)
DevServicesDatasourceResultBuildItem launchDatabases(CurateOutcomeBuildItem curateOutcomeBuildItem, DockerStatusBuildItem dockerStatusBuildItem, List<DefaultDataSourceDbKindBuildItem> installedDrivers, List<DevServicesDatasourceProviderBuildItem> devDBProviders, DataSourcesBuildTimeConfig dataSourceBuildTimeConfig, LaunchModeBuildItem launchMode, List<DevServicesDatasourceConfigurationHandlerBuildItem> configurationHandlerBuildItems, BuildProducer<DevServicesResultBuildItem> devServicesResultBuildItemBuildProducer, Optional<ConsoleInstalledBuildItem> consoleInstalledBuildItem, CuratedApplicationShutdownBuildItem closeBuildItem, LoggingSetupBuildItem loggingSetupBuildItem, GlobalDevServicesConfig globalDevServicesConfig) {
    // if not and the DB's have already started we just return
    if (databases != null) {
        boolean restartRequired = false;
        if (!restartRequired) {
            for (Map.Entry<String, String> entry : cachedProperties.entrySet()) {
                if (!Objects.equals(entry.getValue(), trim(ConfigProvider.getConfig().getOptionalValue(entry.getKey(), String.class).orElse(null)))) {
                    restartRequired = true;
                    break;
                }
            }
        }
        if (!restartRequired) {
            // devservices properties may have been added
            for (var name : ConfigProvider.getConfig().getPropertyNames()) {
                if (name.startsWith("quarkus.datasource.") && name.contains(".devservices.") && !cachedProperties.containsKey(name)) {
                    restartRequired = true;
                    break;
                }
            }
        }
        if (!restartRequired) {
            for (RunningDevService database : databases) {
                devServicesResultBuildItemBuildProducer.produce(database.toBuildItem());
            }
            // keep the previous behaviour of producing DevServicesDatasourceResultBuildItem only when the devservice first starts.
            return null;
        }
        for (Closeable i : databases) {
            try {
                i.close();
            } catch (Throwable e) {
                log.error("Failed to stop database", e);
            }
        }
        databases = null;
        cachedProperties = null;
    }
    DevServicesDatasourceResultBuildItem.DbResult defaultResult;
    Map<String, DevServicesDatasourceResultBuildItem.DbResult> namedResults = new HashMap<>();
    // now we need to figure out if we need to launch some databases
    // note that because we run in dev and test mode only we know the runtime
    // config at build time, as they both execute in the same JVM
    // to keep things simpler for now we are only going to support this for the default datasource
    // support for named datasources will come later
    Map<String, String> propertiesMap = new HashMap<>();
    List<RunningDevService> runningDevServices = new ArrayList<>();
    Map<String, List<DevServicesDatasourceConfigurationHandlerBuildItem>> configHandlersByDbType = configurationHandlerBuildItems.stream().collect(Collectors.toMap(DevServicesDatasourceConfigurationHandlerBuildItem::getDbKind, Collections::singletonList, (configurationHandlerBuildItems1, configurationHandlerBuildItems2) -> {
        List<DevServicesDatasourceConfigurationHandlerBuildItem> ret = new ArrayList<>();
        ret.addAll(configurationHandlerBuildItems1);
        ret.addAll(configurationHandlerBuildItems2);
        return ret;
    }));
    Map<String, DevServicesDatasourceProvider> devDBProviderMap = devDBProviders.stream().collect(Collectors.toMap(DevServicesDatasourceProviderBuildItem::getDatabase, DevServicesDatasourceProviderBuildItem::getDevServicesProvider));
    RunningDevService defaultDevService = startDevDb(null, curateOutcomeBuildItem, installedDrivers, !dataSourceBuildTimeConfig.namedDataSources.isEmpty(), devDBProviderMap, dataSourceBuildTimeConfig.defaultDataSource, configHandlersByDbType, propertiesMap, dockerStatusBuildItem, launchMode.getLaunchMode(), consoleInstalledBuildItem, loggingSetupBuildItem, globalDevServicesConfig);
    if (defaultDevService != null) {
        runningDevServices.add(defaultDevService);
    }
    defaultResult = toDbResult(defaultDevService);
    for (Map.Entry<String, DataSourceBuildTimeConfig> entry : dataSourceBuildTimeConfig.namedDataSources.entrySet()) {
        RunningDevService namedDevService = startDevDb(entry.getKey(), curateOutcomeBuildItem, installedDrivers, true, devDBProviderMap, entry.getValue(), configHandlersByDbType, propertiesMap, dockerStatusBuildItem, launchMode.getLaunchMode(), consoleInstalledBuildItem, loggingSetupBuildItem, globalDevServicesConfig);
        if (namedDevService != null) {
            runningDevServices.add(namedDevService);
            namedResults.put(entry.getKey(), toDbResult(namedDevService));
        }
    }
    if (first) {
        first = false;
        Runnable closeTask = new Runnable() {

            @Override
            public void run() {
                if (databases != null) {
                    for (Closeable i : databases) {
                        try {
                            i.close();
                        } catch (Throwable t) {
                            log.error("Failed to stop database", t);
                        }
                    }
                }
                first = true;
                databases = null;
                cachedProperties = null;
            }
        };
        closeBuildItem.addCloseTask(closeTask, true);
    }
    databases = runningDevServices;
    cachedProperties = propertiesMap;
    for (RunningDevService database : databases) {
        devServicesResultBuildItemBuildProducer.produce(database.toBuildItem());
    }
    return new DevServicesDatasourceResultBuildItem(defaultResult, namedResults);
}
Also used : IsNormal(io.quarkus.deployment.IsNormal) DataSourcesBuildTimeConfig(io.quarkus.datasource.runtime.DataSourcesBuildTimeConfig) DevServicesDatasourceProvider(io.quarkus.datasource.deployment.spi.DevServicesDatasourceProvider) Logger(org.jboss.logging.Logger) LaunchMode(io.quarkus.runtime.LaunchMode) HashMap(java.util.HashMap) DevServicesDatasourceConfigurationHandlerBuildItem(io.quarkus.datasource.deployment.spi.DevServicesDatasourceConfigurationHandlerBuildItem) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService) ConsoleInstalledBuildItem(io.quarkus.deployment.console.ConsoleInstalledBuildItem) CurateOutcomeBuildItem(io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem) DevServicesDatasourceResultBuildItem(io.quarkus.datasource.deployment.spi.DevServicesDatasourceResultBuildItem) BuildProducer(io.quarkus.deployment.annotations.BuildProducer) ArrayList(java.util.ArrayList) BuildStep(io.quarkus.deployment.annotations.BuildStep) CuratedApplicationShutdownBuildItem(io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem) Map(java.util.Map) LoggingSetupBuildItem(io.quarkus.deployment.logging.LoggingSetupBuildItem) DevServicesResultBuildItem(io.quarkus.deployment.builditem.DevServicesResultBuildItem) StartupLogCompressor(io.quarkus.deployment.console.StartupLogCompressor) GlobalDevServicesConfig(io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig) DataSourceBuildTimeConfig(io.quarkus.datasource.runtime.DataSourceBuildTimeConfig) Collectors(java.util.stream.Collectors) DevServicesDatasourceProviderBuildItem(io.quarkus.datasource.deployment.spi.DevServicesDatasourceProviderBuildItem) Objects(java.util.Objects) List(java.util.List) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) LaunchModeBuildItem(io.quarkus.deployment.builditem.LaunchModeBuildItem) Closeable(java.io.Closeable) DefaultDataSourceDbKindBuildItem(io.quarkus.datasource.deployment.spi.DefaultDataSourceDbKindBuildItem) Optional(java.util.Optional) DockerStatusBuildItem(io.quarkus.deployment.builditem.DockerStatusBuildItem) Collections(java.util.Collections) HashMap(java.util.HashMap) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) DevServicesDatasourceProvider(io.quarkus.datasource.deployment.spi.DevServicesDatasourceProvider) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService) ArrayList(java.util.ArrayList) List(java.util.List) DevServicesDatasourceResultBuildItem(io.quarkus.datasource.deployment.spi.DevServicesDatasourceResultBuildItem) DataSourceBuildTimeConfig(io.quarkus.datasource.runtime.DataSourceBuildTimeConfig) HashMap(java.util.HashMap) Map(java.util.Map) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

DefaultDataSourceDbKindBuildItem (io.quarkus.datasource.deployment.spi.DefaultDataSourceDbKindBuildItem)2 BuildStep (io.quarkus.deployment.annotations.BuildStep)2 JdbcDriverBuildItem (io.quarkus.agroal.spi.JdbcDriverBuildItem)1 DevServicesDatasourceConfigurationHandlerBuildItem (io.quarkus.datasource.deployment.spi.DevServicesDatasourceConfigurationHandlerBuildItem)1 DevServicesDatasourceProvider (io.quarkus.datasource.deployment.spi.DevServicesDatasourceProvider)1 DevServicesDatasourceProviderBuildItem (io.quarkus.datasource.deployment.spi.DevServicesDatasourceProviderBuildItem)1 DevServicesDatasourceResultBuildItem (io.quarkus.datasource.deployment.spi.DevServicesDatasourceResultBuildItem)1 DataSourceBuildTimeConfig (io.quarkus.datasource.runtime.DataSourceBuildTimeConfig)1 DataSourcesBuildTimeConfig (io.quarkus.datasource.runtime.DataSourcesBuildTimeConfig)1 IsNormal (io.quarkus.deployment.IsNormal)1 BuildProducer (io.quarkus.deployment.annotations.BuildProducer)1 CuratedApplicationShutdownBuildItem (io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem)1 DevServicesResultBuildItem (io.quarkus.deployment.builditem.DevServicesResultBuildItem)1 RunningDevService (io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService)1 DockerStatusBuildItem (io.quarkus.deployment.builditem.DockerStatusBuildItem)1 LaunchModeBuildItem (io.quarkus.deployment.builditem.LaunchModeBuildItem)1 ConsoleInstalledBuildItem (io.quarkus.deployment.console.ConsoleInstalledBuildItem)1 StartupLogCompressor (io.quarkus.deployment.console.StartupLogCompressor)1 GlobalDevServicesConfig (io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig)1 LoggingSetupBuildItem (io.quarkus.deployment.logging.LoggingSetupBuildItem)1