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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.api.migration.JavaMigration;
import org.flywaydb.core.extensibility.ConfigurationExtension; import org.flywaydb.core.extensibility.ConfigurationExtension;
import org.flywaydb.core.internal.database.postgresql.PostgreSQLConfigurationExtension; 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.oracle.OracleConfigurationExtension;
import org.flywaydb.database.sqlserver.SQLServerConfigurationExtension; import org.flywaydb.database.sqlserver.SQLServerConfigurationExtension;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar; import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.beans.factory.ObjectProvider; 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.JdbcUtils;
import org.springframework.jdbc.support.MetaDataAccessException; import org.springframework.jdbc.support.MetaDataAccessException;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -461,6 +464,9 @@ public class FlywayAutoConfiguration {
@Override @Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) { public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.resources().registerPattern("db/migration/*"); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,17 +17,15 @@
package org.springframework.boot.autoconfigure.flyway; package org.springframework.boot.autoconfigure.flyway;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays; import java.util.Arrays;
import org.flywaydb.core.api.configuration.Configuration;
import org.flywaydb.core.api.configuration.FluentConfiguration; import org.flywaydb.core.api.configuration.FluentConfiguration;
import org.flywaydb.core.api.migration.JavaMigration; import org.flywaydb.core.api.migration.JavaMigration;
import org.flywaydb.core.internal.scanner.LocationScannerCache; import org.flywaydb.core.internal.scanner.LocationScannerCache;
import org.flywaydb.core.internal.scanner.ResourceNameCache; import org.flywaydb.core.internal.scanner.ResourceNameCache;
import org.flywaydb.core.internal.scanner.Scanner; import org.flywaydb.core.internal.scanner.Scanner;
import org.springframework.util.ClassUtils;
/** /**
* Registers {@link NativeImageResourceProvider} as a Flyway * Registers {@link NativeImageResourceProvider} as a Flyway
* {@link org.flywaydb.core.api.ResourceProvider}. * {@link org.flywaydb.core.api.ResourceProvider}.
@ -40,7 +38,7 @@ class NativeImageResourceProviderCustomizer extends ResourceProviderCustomizer {
@Override @Override
public void customize(FluentConfiguration configuration) { public void customize(FluentConfiguration configuration) {
if (configuration.getResourceProvider() == null) { if (configuration.getResourceProvider() == null) {
final var scanner = getFlyway9OrFallbackTo10ScannerObject(configuration); Scanner<?> scanner = createScanner(configuration);
NativeImageResourceProvider resourceProvider = new NativeImageResourceProvider(scanner, NativeImageResourceProvider resourceProvider = new NativeImageResourceProvider(scanner,
configuration.getClassLoader(), Arrays.asList(configuration.getLocations()), configuration.getClassLoader(), Arrays.asList(configuration.getLocations()),
configuration.getEncoding(), configuration.isFailOnMissingLocations()); configuration.getEncoding(), configuration.isFailOnMissingLocations());
@ -48,41 +46,29 @@ class NativeImageResourceProviderCustomizer extends ResourceProviderCustomizer {
} }
} }
private static Scanner getFlyway9OrFallbackTo10ScannerObject(FluentConfiguration configuration) { private static Scanner<?> createScanner(FluentConfiguration configuration) {
Scanner scanner;
try { 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) { catch (NoSuchMethodError ex) {
// happens when scanner is flyway version 10, which the constructor accepts // Flyway 10
// different number of parameters. return createFlyway10Scanner(configuration);
scanner = getFlyway10Scanner(configuration);
} }
return scanner;
} }
private static Scanner getFlyway10Scanner(FluentConfiguration configuration) { private static Scanner<?> createFlyway10Scanner(FluentConfiguration configuration) throws LinkageError {
final Constructor<?> scannerConstructor;
final Scanner scanner;
try { try {
scannerConstructor = ClassUtils.forName("org.flywaydb.core.internal.scanner.Scanner", null) Constructor<?> scannerConstructor = Scanner.class.getDeclaredConstructor(Class.class, boolean.class,
.getDeclaredConstructors()[0]; ResourceNameCache.class, LocationScannerCache.class, Configuration.class);
scanner = (Scanner) scannerConstructor.newInstance(JavaMigration.class, false, new ResourceNameCache(), return (Scanner<?>) scannerConstructor.newInstance(JavaMigration.class, false, new ResourceNameCache(),
new LocationScannerCache(), configuration); new LocationScannerCache(), configuration);
} }
catch (ClassNotFoundException | InstantiationException | IllegalAccessException catch (Exception ex) {
| InvocationTargetException ex) {
throw new RuntimeException(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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 Moritz Halbritter
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Maziz * @author Maziz Esa
*/ */
@ClassPathOverrides("org.flywaydb:flyway-core:10.12.0") @ClassPathOverrides("org.flywaydb:flyway-core:10.12.0")
class Flyway10NativeImageResourceProviderCustomizerTests { class Flyway10xNativeImageResourceProviderCustomizerTests {
private final NativeImageResourceProviderCustomizer customizer = new NativeImageResourceProviderCustomizer(); private final NativeImageResourceProviderCustomizer customizer = new NativeImageResourceProviderCustomizer();