Polish "Fix Flyway 10 in a GraalVM native image"

See gh-40821
This commit is contained in:
Andy Wilkinson 2024-05-29 14:17:50 +01:00
parent 345edb1301
commit 10e23b8f35
3 changed files with 26 additions and 34 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -36,9 +36,11 @@ import org.flywaydb.core.api.configuration.FluentConfiguration;
import org.flywaydb.core.api.migration.JavaMigration;
import org.flywaydb.core.extensibility.ConfigurationExtension;
import org.flywaydb.core.internal.database.postgresql.PostgreSQLConfigurationExtension;
import org.flywaydb.core.internal.scanner.Scanner;
import org.flywaydb.database.oracle.OracleConfigurationExtension;
import org.flywaydb.database.sqlserver.SQLServerConfigurationExtension;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.beans.factory.ObjectProvider;
@ -78,6 +80,7 @@ import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.jdbc.support.MetaDataAccessException;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@ -461,6 +464,9 @@ public class FlywayAutoConfiguration {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.resources().registerPattern("db/migration/*");
if (ClassUtils.isPresent("org.flywaydb.core.extensibility.Tier", classLoader)) {
hints.reflection().registerType(Scanner.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,17 +17,15 @@
package org.springframework.boot.autoconfigure.flyway;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import org.flywaydb.core.api.configuration.Configuration;
import org.flywaydb.core.api.configuration.FluentConfiguration;
import org.flywaydb.core.api.migration.JavaMigration;
import org.flywaydb.core.internal.scanner.LocationScannerCache;
import org.flywaydb.core.internal.scanner.ResourceNameCache;
import org.flywaydb.core.internal.scanner.Scanner;
import org.springframework.util.ClassUtils;
/**
* Registers {@link NativeImageResourceProvider} as a Flyway
* {@link org.flywaydb.core.api.ResourceProvider}.
@ -40,7 +38,7 @@ class NativeImageResourceProviderCustomizer extends ResourceProviderCustomizer {
@Override
public void customize(FluentConfiguration configuration) {
if (configuration.getResourceProvider() == null) {
final var scanner = getFlyway9OrFallbackTo10ScannerObject(configuration);
Scanner<?> scanner = createScanner(configuration);
NativeImageResourceProvider resourceProvider = new NativeImageResourceProvider(scanner,
configuration.getClassLoader(), Arrays.asList(configuration.getLocations()),
configuration.getEncoding(), configuration.isFailOnMissingLocations());
@ -48,41 +46,29 @@ class NativeImageResourceProviderCustomizer extends ResourceProviderCustomizer {
}
}
private static Scanner getFlyway9OrFallbackTo10ScannerObject(FluentConfiguration configuration) {
Scanner scanner;
private static Scanner<?> createScanner(FluentConfiguration configuration) {
try {
scanner = getFlyway9Scanner(configuration);
return new Scanner<>(JavaMigration.class, Arrays.asList(configuration.getLocations()),
configuration.getClassLoader(), configuration.getEncoding(), configuration.isDetectEncoding(),
false, new ResourceNameCache(), new LocationScannerCache(),
configuration.isFailOnMissingLocations());
}
catch (NoSuchMethodError noSuchMethodError) {
// happens when scanner is flyway version 10, which the constructor accepts
// different number of parameters.
scanner = getFlyway10Scanner(configuration);
catch (NoSuchMethodError ex) {
// Flyway 10
return createFlyway10Scanner(configuration);
}
return scanner;
}
private static Scanner getFlyway10Scanner(FluentConfiguration configuration) {
final Constructor<?> scannerConstructor;
final Scanner scanner;
private static Scanner<?> createFlyway10Scanner(FluentConfiguration configuration) throws LinkageError {
try {
scannerConstructor = ClassUtils.forName("org.flywaydb.core.internal.scanner.Scanner", null)
.getDeclaredConstructors()[0];
scanner = (Scanner) scannerConstructor.newInstance(JavaMigration.class, false, new ResourceNameCache(),
Constructor<?> scannerConstructor = Scanner.class.getDeclaredConstructor(Class.class, boolean.class,
ResourceNameCache.class, LocationScannerCache.class, Configuration.class);
return (Scanner<?>) scannerConstructor.newInstance(JavaMigration.class, false, new ResourceNameCache(),
new LocationScannerCache(), configuration);
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| InvocationTargetException ex) {
catch (Exception ex) {
throw new RuntimeException(ex);
}
return scanner;
}
private static Scanner getFlyway9Scanner(FluentConfiguration configuration) {
Scanner scanner;
scanner = new Scanner<>(JavaMigration.class, Arrays.asList(configuration.getLocations()),
configuration.getClassLoader(), configuration.getEncoding(), configuration.isDetectEncoding(), false,
new ResourceNameCache(), new LocationScannerCache(), configuration.isFailOnMissingLocations());
return scanner;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -32,10 +32,10 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Maziz
* @author Maziz Esa
*/
@ClassPathOverrides("org.flywaydb:flyway-core:10.12.0")
class Flyway10NativeImageResourceProviderCustomizerTests {
class Flyway10xNativeImageResourceProviderCustomizerTests {
private final NativeImageResourceProviderCustomizer customizer = new NativeImageResourceProviderCustomizer();