/** * Parse a set of URLs from a comma-separated list of URLs. If the URL points to a directory all * jar files within that directory will be returned as well. * * @param urlString Comma-separated list of URLs (relative or absolute) * @return List of URLs resolved from {@code urlString} */ protected List<URL> parseURLs(FileObject root, String urlString) { if (urlString == null || urlString.trim().isEmpty()) { return Collections.emptyList(); } String[] paths = urlString.split(","); List<URL> urls = new ArrayList<URL>(); for (String path : paths) { try { FileObject file = root.resolveFile(path.trim()); if (!file.exists()) { file = defaultFsm.resolveFile(path.trim()); } if (FileType.FOLDER.equals(file.getType())) { // Add directories with a trailing / so the URL ClassLoader interprets // them as directories urls.add(new URL(file.getURL().toExternalForm() + "/")); // Also add all jars within this directory urls.addAll(findJarsIn(file, 1, new HashSet<String>())); } else { urls.add(file.getURL()); } } catch (Exception e) { // Log invalid path logger.error(BaseMessages.getString(PKG, "Error.InvalidClasspathEntry", path)); } } return urls; }
/** * Create a ClassLoader to load resources for a {@code HadoopConfiguration}. * * @param root Configuration root directory * @param parent Parent class loader to delegate to if resources cannot be found in the * configuration's directory or provided classpath * @param classpathUrls Additional URLs to add to the class loader. These will be added before any * internal resources. * @param ignoredClasses Classes (or packages) that should not be loaded by the class loader * @return A class loader capable of loading a Hadoop configuration located at {@code root}. * @throws ConfigurationException Error creating a class loader for the Hadoop configuration * located at {@code root} */ protected ClassLoader createConfigurationLoader( FileObject root, ClassLoader parent, List<URL> classpathUrls, ShimProperties configurationProperties, String... ignoredClasses) throws ConfigurationException { try { if (root == null || !FileType.FOLDER.equals(root.getType())) { throw new IllegalArgumentException("root must be a folder: " + root); } // Find all jar files in the configuration, at most 2 folders deep List<URL> jars = findJarsIn(root, 3, configurationProperties.getConfigSet(SHIM_CLASSPATH_IGNORE)); // Add the root of the configuration jars.add(0, new URL(root.getURL().toExternalForm() + "/")); // Inject any overriding URLs before all other paths if (classpathUrls != null) { jars.addAll(0, classpathUrls); } return new HadoopConfigurationClassLoader( jars.toArray(EMPTY_URL_ARRAY), parent, ignoredClasses); } catch (Exception ex) { throw new ConfigurationException( BaseMessages.getString(PKG, "Error.CreatingClassLoader"), ex); } }