mirror of https://github.com/apache/nifi.git
NIFI-2619: Added unit test showing bugs, Added logic to ClassLoaderUtils to trim module paths and accept URLs
Signed-off-by: Yolanda M. Davis <ymdavis@apache.org> This closes #907
This commit is contained in:
parent
7884d09948
commit
7123a1a276
|
@ -16,26 +16,42 @@
|
|||
*/
|
||||
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;
|
||||
// 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) {
|
||||
// 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()) {
|
||||
|
@ -56,6 +72,7 @@ public class ClassLoaderUtils {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return additionalClasspath.toArray(new URL[additionalClasspath.size()]);
|
||||
}
|
||||
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue