NIFI-1647 This closes #288. fixed validators and config resolution

Signed-off-by: joewitt <joewitt@apache.org>
This commit is contained in:
Oleg Zhurakousky 2016-03-18 09:26:54 -04:00 committed by joewitt
parent 3a4546c08a
commit bb738f978a
2 changed files with 32 additions and 30 deletions

View File

@ -66,8 +66,8 @@ final class SpringContextFactory {
* Thread.contextClassLoader can find such resources. * Thread.contextClassLoader can find such resources.
*/ */
static SpringDataExchanger createSpringContextDelegate(String classpath, String config) { static SpringDataExchanger createSpringContextDelegate(String classpath, String config) {
URL[] urls = gatherAdditionalClassPathUrls(classpath); List<URL> urls = gatherAdditionalClassPathUrls(classpath);
SpringContextClassLoader contextCl = new SpringContextClassLoader(urls, SpringContextClassLoader contextCl = new SpringContextClassLoader(urls.toArray(new URL[] {}),
SpringContextFactory.class.getClassLoader()); SpringContextFactory.class.getClassLoader());
ClassLoader tContextCl = Thread.currentThread().getContextClassLoader(); ClassLoader tContextCl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(contextCl); Thread.currentThread().setContextClassLoader(contextCl);
@ -98,28 +98,34 @@ final class SpringContextFactory {
/** /**
* *
*/ */
private static URL[] gatherAdditionalClassPathUrls(String path) { static List<URL> gatherAdditionalClassPathUrls(String classPathRoot) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Adding additional resources from '" + path + "' to the classpath."); logger.debug("Adding additional resources from '" + classPathRoot + "' to the classpath.");
} }
File libraryDir = new File(path); File classPathRootFile = new File(classPathRoot);
if (libraryDir.exists() && libraryDir.isDirectory()) { if (classPathRootFile.exists() && classPathRootFile.isDirectory()) {
String[] cpResourceNames = libraryDir.list(); String[] cpResourceNames = classPathRootFile.list();
try { try {
List<URL> urls = new ArrayList<>(); List<URL> urls = new ArrayList<>();
for (int i = 0; i < cpResourceNames.length; i++) { for (String resourceName : cpResourceNames) {
URL url = new File(libraryDir, cpResourceNames[i]).toURI().toURL(); File r = new File(classPathRootFile, resourceName);
if (r.getName().toLowerCase().endsWith(".jar") || r.isDirectory()) {
URL url = r.toURI().toURL();
urls.add(url); urls.add(url);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Identifying additional resource to the classpath: " + url); logger.debug("Identifying additional resource to the classpath: " + url);
} }
} }
return urls.toArray(new URL[] {}); }
urls.add(classPathRootFile.toURI().toURL());
return urls;
} catch (Exception e) { } catch (Exception e) {
throw new IllegalStateException("Failed to parse user libraries from '" + libraryDir.getAbsolutePath() + "'", e); throw new IllegalStateException(
"Failed to parse user libraries from '" + classPathRootFile.getAbsolutePath() + "'", e);
} }
} else { } else {
throw new IllegalArgumentException("Path '" + libraryDir.getAbsolutePath() throw new IllegalArgumentException("Path '" + classPathRootFile.getAbsolutePath()
+ "' is not valid because it doesn't exist or does not point to a directory."); + "' is not valid because it doesn't exist or does not point to a directory.");
} }
} }

View File

@ -20,11 +20,11 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -143,13 +143,13 @@ public class SpringContextProcessor extends AbstractProcessor {
.name("Application Context config path") .name("Application Context config path")
.description("The path to the Spring Application Context configuration file relative to the classpath") .description("The path to the Spring Application Context configuration file relative to the classpath")
.required(true) .required(true)
.addValidator(new SpringContextConfigValidator()) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.build(); .build();
public static final PropertyDescriptor CTX_LIB_PATH = new PropertyDescriptor.Builder() public static final PropertyDescriptor CTX_LIB_PATH = new PropertyDescriptor.Builder()
.name("Application Context class path") .name("Application Context class path")
.description("Path to the directory with resources (i.e., JARs, configuration files etc.) required to be on " .description("Path to the directory with resources (i.e., JARs, configuration files etc.) required to be on "
+ "the classpath of the ApplicationContext.") + "the classpath of the ApplicationContext.")
.addValidator(new SpringContextConfigValidator()) .addValidator(StandardValidators.createDirectoryExistsValidator(false, false))
.required(true) .required(true)
.build(); .build();
public static final PropertyDescriptor SEND_TIMEOUT = new PropertyDescriptor.Builder() public static final PropertyDescriptor SEND_TIMEOUT = new PropertyDescriptor.Builder()
@ -281,6 +281,12 @@ public class SpringContextProcessor extends AbstractProcessor {
this.receiveFromSpring(processSession); this.receiveFromSpring(processSession);
} }
@Override
protected Collection<ValidationResult> customValidate(final ValidationContext validationContext) {
SpringContextConfigValidator v = new SpringContextConfigValidator();
return Collections.singletonList(v.validate(CTX_CONFIG_PATH.getName(), null, validationContext));
}
/** /**
* *
*/ */
@ -425,18 +431,8 @@ public class SpringContextProcessor extends AbstractProcessor {
List<URL> urls = new ArrayList<>(); List<URL> urls = new ArrayList<>();
URLClassLoader parentLoader = (URLClassLoader) SpringContextProcessor.class.getClassLoader(); URLClassLoader parentLoader = (URLClassLoader) SpringContextProcessor.class.getClassLoader();
urls.addAll(Arrays.asList(parentLoader.getURLs())); urls.addAll(Arrays.asList(parentLoader.getURLs()));
String[] resourceNames = libDirPathFile.list();
try { urls.addAll(SpringContextFactory.gatherAdditionalClassPathUrls(libDirPathFile.getAbsolutePath()));
for (String resourceName : resourceNames) {
File r = new File(libDirPathFile, resourceName);
if (!r.isDirectory() && !r.getName().startsWith(".")) {
URL url = new File(libDirPathFile, resourceName).toURI().toURL();
urls.add(url);
}
}
} catch (MalformedURLException e) {
throw new IllegalStateException(e);
}
boolean resolvable = false; boolean resolvable = false;
try (URLClassLoader throwawayCl = new URLClassLoader(urls.toArray(new URL[] {}), null)) { try (URLClassLoader throwawayCl = new URLClassLoader(urls.toArray(new URL[] {}), null)) {
resolvable = throwawayCl.findResource(configPath) != null; resolvable = throwawayCl.findResource(configPath) != null;