|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.flyway; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.UncheckedIOException; |
| 21 | +import java.nio.charset.Charset; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.locks.Lock; |
| 26 | +import java.util.concurrent.locks.ReentrantLock; |
| 27 | + |
| 28 | +import org.flywaydb.core.api.FlywayException; |
| 29 | +import org.flywaydb.core.api.Location; |
| 30 | +import org.flywaydb.core.api.ResourceProvider; |
| 31 | +import org.flywaydb.core.api.resource.LoadableResource; |
| 32 | +import org.flywaydb.core.internal.resource.classpath.ClassPathResource; |
| 33 | +import org.flywaydb.core.internal.scanner.Scanner; |
| 34 | +import org.flywaydb.core.internal.util.StringUtils; |
| 35 | + |
| 36 | +import org.springframework.core.NativeDetector; |
| 37 | +import org.springframework.core.io.Resource; |
| 38 | +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
| 39 | + |
| 40 | +/** |
| 41 | + * A Flyway {@link ResourceProvider} which supports GraalVM native-image. |
| 42 | + * <p> |
| 43 | + * It delegates work to Flyways {@link Scanner}, and additionally uses |
| 44 | + * {@link PathMatchingResourcePatternResolver} to find migration files in a native image. |
| 45 | + * |
| 46 | + * @author Moritz Halbritter |
| 47 | + */ |
| 48 | +class NativeImageResourceProvider implements ResourceProvider { |
| 49 | + |
| 50 | + private final Scanner<?> scanner; |
| 51 | + |
| 52 | + private final ClassLoader classLoader; |
| 53 | + |
| 54 | + private final Collection<Location> locations; |
| 55 | + |
| 56 | + private final Charset encoding; |
| 57 | + |
| 58 | + private final boolean failOnMissingLocations; |
| 59 | + |
| 60 | + private final List<ResourceWithLocation> resources = new ArrayList<>(); |
| 61 | + |
| 62 | + private final Lock lock = new ReentrantLock(); |
| 63 | + |
| 64 | + private boolean initialized; |
| 65 | + |
| 66 | + NativeImageResourceProvider(Scanner<?> scanner, ClassLoader classLoader, Collection<Location> locations, |
| 67 | + Charset encoding, boolean failOnMissingLocations) { |
| 68 | + this.scanner = scanner; |
| 69 | + this.classLoader = classLoader; |
| 70 | + this.locations = locations; |
| 71 | + this.encoding = encoding; |
| 72 | + this.failOnMissingLocations = failOnMissingLocations; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public LoadableResource getResource(String name) { |
| 77 | + if (!NativeDetector.inNativeImage()) { |
| 78 | + return this.scanner.getResource(name); |
| 79 | + } |
| 80 | + LoadableResource resource = this.scanner.getResource(name); |
| 81 | + if (resource != null) { |
| 82 | + return resource; |
| 83 | + } |
| 84 | + if (this.classLoader.getResource(name) == null) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + return new ClassPathResource(null, name, this.classLoader, this.encoding); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Collection<LoadableResource> getResources(String prefix, String[] suffixes) { |
| 92 | + if (!NativeDetector.inNativeImage()) { |
| 93 | + return this.scanner.getResources(prefix, suffixes); |
| 94 | + } |
| 95 | + ensureInitialized(); |
| 96 | + List<LoadableResource> result = new ArrayList<>(this.scanner.getResources(prefix, suffixes)); |
| 97 | + this.resources.stream().filter((r) -> StringUtils.startsAndEndsWith(r.resource.getFilename(), prefix, suffixes)) |
| 98 | + .map((r) -> (LoadableResource) new ClassPathResource(r.location(), |
| 99 | + r.location().getPath() + "/" + r.resource().getFilename(), this.classLoader, this.encoding)) |
| 100 | + .forEach(result::add); |
| 101 | + return result; |
| 102 | + } |
| 103 | + |
| 104 | + private void ensureInitialized() { |
| 105 | + this.lock.lock(); |
| 106 | + try { |
| 107 | + if (!this.initialized) { |
| 108 | + initialize(); |
| 109 | + this.initialized = true; |
| 110 | + } |
| 111 | + } |
| 112 | + finally { |
| 113 | + this.lock.unlock(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private void initialize() { |
| 118 | + PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); |
| 119 | + for (Location location : this.locations) { |
| 120 | + if (!location.isClassPath()) { |
| 121 | + continue; |
| 122 | + } |
| 123 | + Resource root = resolver.getResource(location.getDescriptor()); |
| 124 | + if (!root.exists()) { |
| 125 | + if (this.failOnMissingLocations) { |
| 126 | + throw new FlywayException("Location " + location.getDescriptor() + " doesn't exist"); |
| 127 | + } |
| 128 | + continue; |
| 129 | + } |
| 130 | + Resource[] resources; |
| 131 | + try { |
| 132 | + resources = resolver.getResources(root.getURI() + "/*"); |
| 133 | + } |
| 134 | + catch (IOException ex) { |
| 135 | + throw new UncheckedIOException("Failed to list resources for " + location.getDescriptor(), ex); |
| 136 | + } |
| 137 | + for (Resource resource : resources) { |
| 138 | + this.resources.add(new ResourceWithLocation(resource, location)); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private record ResourceWithLocation(Resource resource, Location location) { |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments