|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.flyway;
|
18 | 18 |
|
| 19 | +import java.lang.reflect.Constructor; |
| 20 | +import java.lang.reflect.InvocationTargetException; |
19 | 21 | import java.util.Arrays;
|
20 | 22 |
|
21 | 23 | import org.flywaydb.core.api.configuration.FluentConfiguration;
|
|
24 | 26 | import org.flywaydb.core.internal.scanner.ResourceNameCache;
|
25 | 27 | import org.flywaydb.core.internal.scanner.Scanner;
|
26 | 28 |
|
| 29 | +import org.springframework.util.ClassUtils; |
| 30 | + |
27 | 31 | /**
|
28 | 32 | * Registers {@link NativeImageResourceProvider} as a Flyway
|
29 | 33 | * {@link org.flywaydb.core.api.ResourceProvider}.
|
30 | 34 | *
|
31 | 35 | * @author Moritz Halbritter
|
| 36 | + * @author Maziz Esa |
32 | 37 | */
|
33 | 38 | class NativeImageResourceProviderCustomizer extends ResourceProviderCustomizer {
|
34 | 39 |
|
35 | 40 | @Override
|
36 | 41 | public void customize(FluentConfiguration configuration) {
|
37 | 42 | if (configuration.getResourceProvider() == null) {
|
38 |
| - Scanner<JavaMigration> scanner = new Scanner<>(JavaMigration.class, |
39 |
| - Arrays.asList(configuration.getLocations()), configuration.getClassLoader(), |
40 |
| - configuration.getEncoding(), configuration.isDetectEncoding(), false, new ResourceNameCache(), |
41 |
| - new LocationScannerCache(), configuration.isFailOnMissingLocations()); |
| 43 | + final var scanner = getFlyway9OrFallbackTo10ScannerObject(configuration); |
42 | 44 | NativeImageResourceProvider resourceProvider = new NativeImageResourceProvider(scanner,
|
43 | 45 | configuration.getClassLoader(), Arrays.asList(configuration.getLocations()),
|
44 | 46 | configuration.getEncoding(), configuration.isFailOnMissingLocations());
|
45 | 47 | configuration.resourceProvider(resourceProvider);
|
46 | 48 | }
|
47 | 49 | }
|
48 | 50 |
|
| 51 | + private static Scanner getFlyway9OrFallbackTo10ScannerObject(FluentConfiguration configuration) { |
| 52 | + Scanner scanner; |
| 53 | + try { |
| 54 | + scanner = getFlyway9Scanner(configuration); |
| 55 | + } |
| 56 | + catch (NoSuchMethodError noSuchMethodError) { |
| 57 | + // happens when scanner is flyway version 10, which the constructor accepts |
| 58 | + // different number of parameters. |
| 59 | + scanner = getFlyway10Scanner(configuration); |
| 60 | + } |
| 61 | + return scanner; |
| 62 | + } |
| 63 | + |
| 64 | + private static Scanner getFlyway10Scanner(FluentConfiguration configuration) { |
| 65 | + final Constructor<?> scannerConstructor; |
| 66 | + final Scanner scanner; |
| 67 | + try { |
| 68 | + scannerConstructor = ClassUtils.forName("org.flywaydb.core.internal.scanner.Scanner", null) |
| 69 | + .getDeclaredConstructors()[0]; |
| 70 | + scanner = (Scanner) scannerConstructor.newInstance(JavaMigration.class, false, new ResourceNameCache(), |
| 71 | + new LocationScannerCache(), configuration); |
| 72 | + } |
| 73 | + catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
| 74 | + | InvocationTargetException ex) { |
| 75 | + throw new RuntimeException(ex); |
| 76 | + } |
| 77 | + return scanner; |
| 78 | + } |
| 79 | + |
| 80 | + private static Scanner getFlyway9Scanner(FluentConfiguration configuration) { |
| 81 | + Scanner scanner; |
| 82 | + scanner = new Scanner<>(JavaMigration.class, Arrays.asList(configuration.getLocations()), |
| 83 | + configuration.getClassLoader(), configuration.getEncoding(), configuration.isDetectEncoding(), false, |
| 84 | + new ResourceNameCache(), new LocationScannerCache(), configuration.isFailOnMissingLocations()); |
| 85 | + return scanner; |
| 86 | + } |
| 87 | + |
49 | 88 | }
|
0 commit comments