IsolatingClassLoaderFactory { @VisibleForTesting static URL[] getClasspathAsURLs(File jarsLocation) { validateCanAccess(jarsLocation); File[] filesInJarsLocation = jarsLocation.listFiles(); if (filesInJarsLocation == null) { throw new SamzaException( String.format("Could not find any files inside %s, probably because it is not a directory", jarsLocation.getPath())); } URL[] urls = Stream.of(filesInJarsLocation) .filter(file -> file.getName().endsWith(".jar") || file.getName().endsWith(".war")) .map(IsolatingClassLoaderFactory::fileURL) .toArray(URL[]::new); LOG.info("Found {} items to load into classpath from {}", urls.length, jarsLocation); Stream.of(urls).forEach(url -> LOG.debug("Found {} from {}", url, jarsLocation)); return urls; }  ClassLoader buildClassLoader();  }
@Test public void testGetClasspathAsURLs() throws URISyntaxException { File classpathDirectory = Paths.get(getClass().getResource("/classloader/classpath").toURI()).toFile(); URL[] classpath = IsolatingClassLoaderFactory.getClasspathAsURLs(classpathDirectory); assertEquals(2, classpath.length); Set<URL> classpathSet = ImmutableSet.copyOf(classpath); URL jarUrl = getClass().getResource("/classloader/classpath/placeholder-jar.jar"); assertTrue(classpathSet.contains(jarUrl)); URL warUrl = getClass().getResource("/classloader/classpath/placeholder-war.war"); assertTrue(classpathSet.contains(warUrl)); }
@Test(expected = SamzaException.class) public void testGetClasspathAsURLsDirectoryDoesNotExist() throws URISyntaxException { File nonExistentDirectory = new File(Paths.get(getClass().getResource("/classloader").toURI()).toFile(), "doesNotExist"); IsolatingClassLoaderFactory.getClasspathAsURLs(nonExistentDirectory); }