tech.jhipster.config.liquibase.SpringLiquibaseUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jhipster-framework Show documentation
Show all versions of jhipster-framework Show documentation
Server-side library used by applications created with the JHipster generator, see https://www.jhipster.tech/ for more information on JHipster
/*
* Copyright 2016-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* 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
*
* http://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 tech.jhipster.config.liquibase;
import java.util.Optional;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
import javax.sql.DataSource;
import liquibase.integration.spring.SpringLiquibase;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.liquibase.DataSourceClosingSpringLiquibase;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.core.env.Environment;
/**
* Utility class for handling SpringLiquibase.
*
*
* It follows implementation of
* LiquibaseAutoConfiguration.
*/
public final class SpringLiquibaseUtil {
private SpringLiquibaseUtil() {}
/**
*
createSpringLiquibase.
*
* @param liquibaseDatasource a {@link javax.sql.DataSource} object.
* @param liquibaseProperties a {@link org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties} object.
* @param dataSource a {@link javax.sql.DataSource} object.
* @param dataSourceProperties a {@link org.springframework.boot.autoconfigure.jdbc.DataSourceProperties} object.
* @return a {@link liquibase.integration.spring.SpringLiquibase} object.
*/
public static SpringLiquibase createSpringLiquibase(
DataSource liquibaseDatasource,
LiquibaseProperties liquibaseProperties,
DataSource dataSource,
DataSourceProperties dataSourceProperties
) {
SpringLiquibase liquibase;
DataSource liquibaseDataSource = getDataSource(liquibaseDatasource, liquibaseProperties, dataSource);
if (liquibaseDataSource != null) {
liquibase = new SpringLiquibase();
liquibase.setDataSource(liquibaseDataSource);
return liquibase;
}
liquibase = new DataSourceClosingSpringLiquibase();
liquibase.setDataSource(createNewDataSource(liquibaseProperties, dataSourceProperties));
return liquibase;
}
/**
* createAsyncSpringLiquibase.
*
* @param env a {@link org.springframework.core.env.Environment} object.
* @param executor a {@link java.util.concurrent.Executor} object.
* @param liquibaseDatasource a {@link javax.sql.DataSource} object.
* @param liquibaseProperties a {@link org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties} object.
* @param dataSource a {@link javax.sql.DataSource} object.
* @param dataSourceProperties a {@link org.springframework.boot.autoconfigure.jdbc.DataSourceProperties} object.
* @return a {@link AsyncSpringLiquibase} object.
*/
public static AsyncSpringLiquibase createAsyncSpringLiquibase(
Environment env,
Executor executor,
DataSource liquibaseDatasource,
LiquibaseProperties liquibaseProperties,
DataSource dataSource,
DataSourceProperties dataSourceProperties
) {
AsyncSpringLiquibase liquibase = new AsyncSpringLiquibase(executor, env);
DataSource liquibaseDataSource = getDataSource(liquibaseDatasource, liquibaseProperties, dataSource);
if (liquibaseDataSource != null) {
liquibase.setCloseDataSourceOnceMigrated(false);
liquibase.setDataSource(liquibaseDataSource);
} else {
liquibase.setDataSource(createNewDataSource(liquibaseProperties, dataSourceProperties));
}
return liquibase;
}
private static DataSource getDataSource(
DataSource liquibaseDataSource,
LiquibaseProperties liquibaseProperties,
DataSource dataSource
) {
if (liquibaseDataSource != null) {
return liquibaseDataSource;
}
if (liquibaseProperties.getUrl() == null && liquibaseProperties.getUser() == null) {
return dataSource;
}
return null;
}
private static DataSource createNewDataSource(LiquibaseProperties liquibaseProperties, DataSourceProperties dataSourceProperties) {
String url = getProperty(liquibaseProperties::getUrl, dataSourceProperties::determineUrl);
String user = getProperty(liquibaseProperties::getUser, dataSourceProperties::determineUsername);
String password = getProperty(liquibaseProperties::getPassword, dataSourceProperties::determinePassword);
return DataSourceBuilder.create().url(url).username(user).password(password).build();
}
private static String getProperty(Supplier property, Supplier defaultValue) {
return Optional.of(property).map(Supplier::get).orElseGet(defaultValue);
}
}