/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.flink.core.plugin;

import org.apache.flink.util.function.FunctionUtils;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.stream.Collectors;

/**
 * This class is used to create a collection of {@link PluginDescriptor} based on directory
 * structure for a given plugin root folder.
 *
 * <p>The expected structure is as follows: the given plugins root folder, containing the plugins
 * folder. One plugin folder contains all resources (jar files) belonging to a plugin. The name of
 * the plugin folder becomes the plugin id.
 *
 * <pre>
 * plugins-root-folder/
 *            |------------plugin-a/ (folder of plugin a)
 *            |                |-plugin-a-1.jar (the jars containing the classes of plugin a)
 *            |                |-plugin-a-2.jar
 *            |                |-...
 *            |
 *            |------------plugin-b/
 *            |                |-plugin-b-1.jar
 *           ...               |-...
 * </pre>
 */
public class DirectoryBasedPluginFinder implements PluginFinder {

    /** Pattern to match jar files in a directory. */
    private static final String JAR_MATCHER_PATTERN = "glob:**.jar";

    /** Root directory to the plugin folders. */
    private final Path pluginsRootDir;

    /** Matcher for jar files in the filesystem of the root folder. */
    private final PathMatcher jarFileMatcher;

    public DirectoryBasedPluginFinder(Path pluginsRootDir) {
        this.pluginsRootDir = pluginsRootDir;
        this.jarFileMatcher = pluginsRootDir.getFileSystem().getPathMatcher(JAR_MATCHER_PATTERN);
    }

    @Override
    public Collection<PluginDescriptor> findPlugins() throws IOException {

        if (!Files.isDirectory(pluginsRootDir)) {
            throw new IOException(
                    "Plugins root directory [" + pluginsRootDir + "] does not exist!");
        }

        return Files.list(pluginsRootDir)
                .filter((Path path) -> Files.isDirectory(path))
                .map(FunctionUtils.uncheckedFunction(this::createPluginDescriptorForSubDirectory))
                .collect(Collectors.toList());
    }

    private PluginDescriptor createPluginDescriptorForSubDirectory(Path subDirectory)
            throws IOException {
        URL[] urls = createJarURLsFromDirectory(subDirectory);
        Arrays.sort(urls, Comparator.comparing(URL::toString));
        // TODO: This class could be extended to parse exclude-pattern from a optional text files in
        // the plugin directories.
        return new PluginDescriptor(subDirectory.getFileName().toString(), urls, new String[0]);
    }

    private URL[] createJarURLsFromDirectory(Path subDirectory) throws IOException {
        URL[] urls =
                Files.list(subDirectory)
                        .filter((Path p) -> Files.isRegularFile(p) && jarFileMatcher.matches(p))
                        .map(FunctionUtils.uncheckedFunction((Path p) -> p.toUri().toURL()))
                        .toArray(URL[]::new);

        if (urls.length < 1) {
            throw new IOException(
                    "Cannot find any jar files for plugin in directory ["
                            + subDirectory
                            + "]."
                            + " Please provide the jar files for the plugin or delete the directory.");
        }

        return urls;
    }
}

@@ -1145,16 +1145,48 @@
 lectors;
+%0Aimport java.util.stream.Stream;
 %0A%0A/**%0A *
@@ -2868,33 +2868,32 @@
 ;%0A        %7D%0A
-%0A
         
 return Files
@@ -2880,22 +2880,42 @@
         
-return
+try (Stream%3CPath%3E stream =
  Files.l
@@ -2933,16 +2933,19 @@
 RootDir)
+) %7B
 %0A       
@@ -2941,36 +2941,45 @@
 ) %7B%0A            
-    
+return stream
 .filter((Path pa
@@ -3018,37 +3018,70 @@
                 
+    
 .map(
+%0A                            
 FunctionUtils.un
@@ -3096,16 +3096,53 @@
 unction(
+%0A                                    
 this::cr
@@ -3195,16 +3195,20 @@
         
+    
 .collect
@@ -3226,24 +3226,34 @@
 .toList());%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -3830,36 +3830,34 @@
     
-URL%5B%5D urls =%0A               
+try (Stream%3CPath%3E stream =
  Fil
@@ -3869,32 +3869,35 @@
 st(subDirectory)
+) %7B
 %0A               
@@ -3883,32 +3883,47 @@
 y)) %7B%0A          
+  URL%5B%5D urls =%0A
               .f
@@ -3920,16 +3920,28 @@
         
+      stream
 .filter(
@@ -4005,16 +4005,20 @@
 hes(p))%0A
+    
         
@@ -4126,16 +4126,20 @@
         
+    
 .toArray
@@ -4153,16 +4153,20 @@
 :new);%0A%0A
+    
         
@@ -4192,32 +4192,36 @@
 ) %7B%0A            
+    
 throw new IOExce
@@ -4247,16 +4247,20 @@
         
+   
+ 
 %22Cannot 
@@ -4333,16 +4333,20 @@
         
+    
 + subDir
@@ -4384,15 +4384,23 @@
     
+   
+ 
 + %22%5D.%22%0A
+    
     
@@ -4503,27 +4503,35 @@
 %22);%0A        
+   
+ 
 %7D%0A%0A
+    
         retu
@@ -4539,16 +4539,26 @@
 n urls;%0A
+        %7D%0A
     %7D%0A%7D%0A

Fix file stream resource leaks