/*
 * Copyright 2010-2020 Redgate Software Ltd
 *
 * 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 org.flywaydb.core.internal.scanner;

import org.flywaydb.core.api.Location;
import org.flywaydb.core.api.logging.Log;
import org.flywaydb.core.api.logging.LogFactory;
import org.flywaydb.core.api.ClassProvider;
import org.flywaydb.core.internal.resource.LoadableResource;
import org.flywaydb.core.api.ResourceProvider;
import org.flywaydb.core.internal.scanner.android.AndroidScanner;
import org.flywaydb.core.internal.scanner.classpath.ClassPathScanner;
import org.flywaydb.core.internal.scanner.classpath.ResourceAndClassScanner;
import org.flywaydb.core.internal.scanner.filesystem.FileSystemScanner;
import org.flywaydb.core.internal.util.FeatureDetector;
import org.flywaydb.core.internal.util.StringUtils;

import java.nio.charset.Charset;
import java.util.*;

/**
 * Scanner for Resources and Classes.
 */
public class Scanner<I> implements ResourceProvider, ClassProvider<I> {
    private static final Log LOG = LogFactory.getLog(Scanner.class);

    private final List<LoadableResource> resources = new ArrayList<>();
    private final List<Class<? extends I>> classes = new ArrayList<>();

    /*
     * Constructor. Scans the given locations for resources, and classes implementing the specified interface.
     */
    public Scanner(Class<I> implementedInterface, Collection<Location> locations, ClassLoader classLoader, Charset encoding



            , ResourceNameCache resourceNameCache
            , LocationScannerCache locationScannerCache
    ) {
        FileSystemScanner fileSystemScanner = new FileSystemScanner(encoding



        );

        boolean android = new FeatureDetector(classLoader).isAndroidAvailable();

        for (Location location : locations) {
            if (location.isFileSystem()) {
                resources.addAll(fileSystemScanner.scanForResources(location));
            } else {
                ResourceAndClassScanner<I> resourceAndClassScanner = android
                        ? new AndroidScanner<>(implementedInterface, classLoader, encoding, location)
                        : new ClassPathScanner<>(implementedInterface, classLoader, encoding, location, resourceNameCache, locationScannerCache);
                resources.addAll(resourceAndClassScanner.scanForResources());
                classes.addAll(resourceAndClassScanner.scanForClasses());
            }
        }
    }

    @Override
    public LoadableResource getResource(String name) {
        for (LoadableResource resource : resources) {
            String relativePath = resource.getRelativePath();
            if (relativePath.equalsIgnoreCase(name) || resource.getAbsolutePathOnDisk().equalsIgnoreCase(name)) {
                return resource;
            }
        }
        return null;
    }

    /**
     * Returns all known resources starting with the specified prefix and ending with any of the specified suffixes.
     *
     * @param prefix   The prefix of the resource names to match.
     * @param suffixes The suffixes of the resource names to match.
     * @return The resources that were found.
     */
    public Collection<LoadableResource> getResources(String prefix, String... suffixes) {
        List<LoadableResource> result = new ArrayList<>();
        for (LoadableResource resource : resources) {
            String fileName = resource.getFilename();
            if (StringUtils.startsAndEndsWith(fileName, prefix, suffixes)) {
                result.add(resource);
            } else {
                LOG.debug("Filtering out resource: " + resource.getAbsolutePath() + " (filename: " + fileName + ")");
            }
        }
        return result;
    }

    /**
     * Scans the classpath for concrete classes under the specified package implementing the specified interface.
     * Non-instantiable abstract classes are filtered out.
     *
     * @return The non-abstract classes that were found.
     */
    public Collection<Class<? extends I>> getClasses() {
        return Collections.unmodifiableCollection(classes);
    }
}
@@ -1360,16 +1360,44 @@
 harset;%0A
+import java.nio.file.Paths;%0A
 import j
@@ -1738,24 +1738,233 @@
 yList%3C%3E();%0A%0A
+    // Lookup maps to speed up getResource%0A    private final HashMap%3CString, LoadableResource%3E relativeResourceMap = new HashMap%3C%3E();%0A    private HashMap%3CString, LoadableResource%3E absoluteResourceMap = null;%0A%0A
     /*%0A     
@@ -3178,24 +3178,178 @@
  %7D%0A        %7D
+%0A%0A        for (LoadableResource resource : resources) %7B%0A            relativeResourceMap.put(resource.getRelativePath().toLowerCase(), resource);%0A        %7D
 %0A    %7D%0A%0A    
@@ -3413,37 +3413,32 @@
 name) %7B%0A        
-for (
 LoadableResource
@@ -3434,33 +3434,39 @@
 bleResource 
-r
+loadedR
 esource 
 : resources)
@@ -3457,195 +3457,783 @@
 rce 
-: resources) %7B%0A            String relativePath = resource.getRelativePath();%0A            if (relativePath.equalsIgnoreCase(name) %7C%7C resource.getAbsolutePathOnDisk().equalsIgnoreCase(name)
+= relativeResourceMap.get(name.toLowerCase());%0A%0A        if (loadedResource != null) %7B%0A            return loadedResource;%0A        %7D%0A%0A        // Only build the HashMap and resolve the absolute paths if an%0A        // absolute path is requested as this is really slow%0A        // Should only ever be required for sqlplus @%0A        if (Paths.get(name).isAbsolute()) %7B%0A            if (absoluteResourceMap == null) %7B%0A                absoluteResourceMap = new HashMap%3C%3E();%0A                for (LoadableResource resource : resources) %7B%0A                    absoluteResourceMap.put(resource.getAbsolutePathOnDisk().toLowerCase(), resource);%0A                %7D%0A            %7D%0A%0A            loadedResource = absoluteResourceMap.get(name.toLowerCase());%0A%0A            if (loadedResource != null
 ) %7B%0A
@@ -4255,17 +4255,23 @@
  return 
-r
+loadedR
 esource;
@@ -4287,32 +4287,33 @@
     %7D%0A        %7D%0A
+%0A
         return n

Fix #2903 Fix resource lookup logic to be more performant