Repository: nifi
Updated Branches:
  refs/heads/master 7884d0994 -> 7123a1a27


NIFI-2619: Added unit test showing bugs, Added logic to ClassLoaderUtils to 
trim module paths and accept URLs

Signed-off-by: Yolanda M. Davis <[email protected]>

This closes #907


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/7123a1a2
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/7123a1a2
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/7123a1a2

Branch: refs/heads/master
Commit: 7123a1a2761d238b2d1e300e33f2f3ade54347e1
Parents: 7884d09
Author: Matt Burgess <[email protected]>
Authored: Mon Aug 22 11:09:14 2016 -0400
Committer: Yolanda M. Davis <[email protected]>
Committed: Mon Aug 22 15:29:03 2016 -0400

----------------------------------------------------------------------
 .../util/file/classloader/ClassLoaderUtils.java | 47 +++++++++++++-------
 .../file/classloader/TestClassLoaderUtils.java  | 28 ++++++++----
 2 files changed, 52 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/7123a1a2/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/file/classloader/ClassLoaderUtils.java
----------------------------------------------------------------------
diff --git 
a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/file/classloader/ClassLoaderUtils.java
 
b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/file/classloader/ClassLoaderUtils.java
index 4d084ec..bc6728c 100644
--- 
a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/file/classloader/ClassLoaderUtils.java
+++ 
b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/file/classloader/ClassLoaderUtils.java
@@ -16,50 +16,67 @@
  */
 package org.apache.nifi.util.file.classloader;
 
+import org.apache.commons.lang3.StringUtils;
+
 import java.io.File;
 import java.io.FilenameFilter;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.stream.Collectors;
 
 public class ClassLoaderUtils {
 
     public static ClassLoader getCustomClassLoader(String modulePath, 
ClassLoader parentClassLoader, FilenameFilter filenameFilter) throws 
MalformedURLException {
-        String[] modules = modulePath != null? modulePath.split(",") : null;
-        URL[] classpaths = getURLsForClasspath(modules,filenameFilter);
-        return createModuleClassLoader(classpaths,parentClassLoader);
+        // Split and trim the module path(s)
+        List<String> modules = (modulePath == null)
+                ? null
+                : 
Arrays.stream(modulePath.split(",")).filter(StringUtils::isNotBlank).map(String::trim).collect(Collectors.toList());
+
+        URL[] classpaths = getURLsForClasspath(modules, filenameFilter);
+        return createModuleClassLoader(classpaths, parentClassLoader);
     }
 
-    protected static URL[] getURLsForClasspath(String[] modulePaths, 
FilenameFilter filenameFilter) throws  MalformedURLException {
+    protected static URL[] getURLsForClasspath(List<String> modulePaths, 
FilenameFilter filenameFilter) throws MalformedURLException {
         List<URL> additionalClasspath = new LinkedList<>();
         if (modulePaths != null) {
             for (String modulePathString : modulePaths) {
-                File modulePath = new File(modulePathString);
+                // If the path is already a URL, just add it (but don't check 
if it exists, too expensive and subject to network availability)
+                boolean isUrl = true;
+                try {
+                    additionalClasspath.add(new URL(modulePathString));
+                } catch (MalformedURLException mue) {
+                    isUrl = false;
+                }
+                if (!isUrl) {
+                    File modulePath = new File(modulePathString);
 
-                if (modulePath.exists()) {
+                    if (modulePath.exists()) {
 
-                    additionalClasspath.add(modulePath.toURI().toURL());
+                        additionalClasspath.add(modulePath.toURI().toURL());
 
-                    if (modulePath.isDirectory()) {
-                        File[] files = modulePath.listFiles(filenameFilter);
+                        if (modulePath.isDirectory()) {
+                            File[] files = 
modulePath.listFiles(filenameFilter);
 
-                        if (files != null) {
-                            for (File jarFile : files) {
-                                
additionalClasspath.add(jarFile.toURI().toURL());
+                            if (files != null) {
+                                for (File jarFile : files) {
+                                    
additionalClasspath.add(jarFile.toURI().toURL());
+                                }
                             }
                         }
+                    } else {
+                        throw new MalformedURLException("Path specified does 
not exist");
                     }
-                } else {
-                    throw new MalformedURLException("Path specified does not 
exist");
                 }
             }
         }
         return additionalClasspath.toArray(new 
URL[additionalClasspath.size()]);
     }
 
-    protected static ClassLoader createModuleClassLoader(URL[] 
modules,ClassLoader parentClassLoader) {
+    protected static ClassLoader createModuleClassLoader(URL[] modules, 
ClassLoader parentClassLoader) {
         return new URLClassLoader(modules, parentClassLoader);
     }
 

http://git-wip-us.apache.org/repos/asf/nifi/blob/7123a1a2/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/classloader/TestClassLoaderUtils.java
----------------------------------------------------------------------
diff --git 
a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/classloader/TestClassLoaderUtils.java
 
b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/classloader/TestClassLoaderUtils.java
index cf47770..d2826e3 100644
--- 
a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/classloader/TestClassLoaderUtils.java
+++ 
b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/classloader/TestClassLoaderUtils.java
@@ -16,12 +16,12 @@
  */
 package org.apache.nifi.util.file.classloader;
 
-import java.io.File;
 import java.io.FilenameFilter;
 import java.net.MalformedURLException;
 
 import org.junit.Test;
 
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -61,13 +61,25 @@ public class TestClassLoaderUtils {
         fail("exception did not occur, path should not exist");
     }
 
-    protected FilenameFilter getJarFilenameFilter(){
-        return  new FilenameFilter() {
-            @Override
-            public boolean accept(File dir, String name) {
-                return (name != null && name.endsWith(".jar"));
-            }
-        };
+    @Test
+    public void testGetCustomClassLoaderWithMultipleLocations() throws 
Exception {
+        final String jarFilePath = " 
src/test/resources/TestClassLoaderUtils/TestSuccess.jar, 
http://nifi.apache.org/Test.jar";;
+        assertNotNull(ClassLoaderUtils.getCustomClassLoader(jarFilePath, 
this.getClass().getClassLoader(), getJarFilenameFilter()));
     }
 
+    @Test
+    public void testGetCustomClassLoaderWithEmptyLocations() throws Exception {
+        String jarFilePath = "";
+        assertNotNull(ClassLoaderUtils.getCustomClassLoader(jarFilePath, 
this.getClass().getClassLoader(), getJarFilenameFilter()));
+
+        jarFilePath = ",";
+        assertNotNull(ClassLoaderUtils.getCustomClassLoader(jarFilePath, 
this.getClass().getClassLoader(), getJarFilenameFilter()));
+
+        jarFilePath = 
",src/test/resources/TestClassLoaderUtils/TestSuccess.jar, ";
+        assertNotNull(ClassLoaderUtils.getCustomClassLoader(jarFilePath, 
this.getClass().getClassLoader(), getJarFilenameFilter()));
+    }
+
+    protected FilenameFilter getJarFilenameFilter(){
+        return  (dir, name) -> name != null && name.endsWith(".jar");
+    }
 }

Reply via email to