Github user olegz commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1156#discussion_r86157718
  
    --- Diff: 
nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/file/classloader/ClassLoaderUtils.java
 ---
    @@ -24,23 +24,57 @@
     import java.net.URL;
     import java.net.URLClassLoader;
     import java.util.Arrays;
    +import java.util.LinkedHashSet;
     import java.util.LinkedList;
     import java.util.List;
    +import java.util.Set;
     import java.util.stream.Collectors;
     
     public class ClassLoaderUtils {
     
         public static ClassLoader getCustomClassLoader(String modulePath, 
ClassLoader parentClassLoader, FilenameFilter filenameFilter) throws 
MalformedURLException {
    -        // Split and trim the module path(s)
    -        List<String> modules = (modulePath == null)
    +        URL[] classpaths = getURLsForClasspath(modulePath, filenameFilter, 
false);
    +        return createModuleClassLoader(classpaths, parentClassLoader);
    +    }
    +
    +    /**
    +     *
    +     * @param modulePath a module path to get URLs from, the module path 
may be a comma-separated list of paths
    +     * @param filenameFilter a filter to apply when a module path is a 
directory and performs a listing, a null filter will return all matches
    +     * @return an array of URL instances representing all of the modules 
resolved from processing modulePath
    +     * @throws MalformedURLException if a module path does not exist
    +     */
    +    public static URL[] getURLsForClasspath(String modulePath, 
FilenameFilter filenameFilter, boolean suppressExceptions) throws 
MalformedURLException {
    +        Set<String> modules = (modulePath == null)
                     ? null
    -                : 
Arrays.stream(modulePath.split(",")).filter(StringUtils::isNotBlank).map(String::trim).collect(Collectors.toList());
    +                : 
Arrays.stream(modulePath.split(",")).filter(StringUtils::isNotBlank).map(String::trim).collect(Collectors.toSet());
     
    -        URL[] classpaths = getURLsForClasspath(modules, filenameFilter);
    -        return createModuleClassLoader(classpaths, parentClassLoader);
    +        return getURLsForClasspathHelper(modules, filenameFilter, 
suppressExceptions);
         }
     
    -    protected static URL[] getURLsForClasspath(List<String> modulePaths, 
FilenameFilter filenameFilter) throws MalformedURLException {
    +    /**
    +     *
    +     * @param modulePaths one or modules paths to get URLs from, each 
module path may be a comma-separated list of paths
    +     * @param filenameFilter a filter to apply when a module path is a 
directory and performs a listing, a null filter will return all matches
    +     * @param suppressExceptions if true then all modules will attempt to 
be resolved even if some throw an exception, if false the first exception will 
be thrown
    +     * @return an array of URL instances representing all of the modules 
resolved from processing modulePaths
    +     * @throws MalformedURLException if a module path does not exist
    +     */
    +    public static URL[] getURLsForClasspath(Set<String> modulePaths, 
FilenameFilter filenameFilter, boolean suppressExceptions) throws 
MalformedURLException {
    +        Set<String> modules = new LinkedHashSet<>();
    +        if (modulePaths != null) {
    +            for (String modulePath : modulePaths) {
    +                modules.addAll(Arrays.stream(modulePath.split(","))
    +                        .filter(StringUtils::isNotBlank)
    +                        .map(String::trim)
    +                        .collect(Collectors.toSet()));
    +            }
    +        }
    +
    +        return getURLsForClasspathHelper(modules, filenameFilter, 
suppressExceptions);
    +    }
    --- End diff --
    
    Since we're getting more into functional programming style and in spirit of 
removing duplication consider the following improvements to both versions of 
```getURLsForClasspath(..)``` methods.
    ```
    public static URL[] getURLsForClasspath(String modulePath, FilenameFilter 
filenameFilter, boolean suppressExceptions) throws MalformedURLException {
            return getURLsForClasspath(modulePath == null ? 
Collections.emptySet() : Collections.singleton(modulePath), filenameFilter, 
suppressExceptions);
    }
    
    public static URL[] getURLsForClasspath(Set<String> modulePaths, 
FilenameFilter filenameFilter, boolean suppressExceptions) throws 
MalformedURLException {
            Set<String> modules = modulePaths == null ? null : 
modulePaths.stream()
                    .flatMap(path -> Arrays.stream(path.split(",")))
                    .filter(StringUtils::isNotBlank)
                    .map(String::trim)
                    .collect(Collectors.toSet());
            return getURLsForClasspathHelper(modules, filenameFilter, 
suppressExceptions);
    }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to